From cae5c8d92cfc68ac6af643e9f090205023d53ff2 Mon Sep 17 00:00:00 2001 From: SergejSavic Date: Thu, 26 Dec 2024 13:53:28 +0100 Subject: [PATCH 1/5] add support for Sylius API --- src/Action/ApiPlatform/MolliePayment.php | 57 ++++++++++++++++++++++++ src/Resources/config/services/action.xml | 3 ++ 2 files changed, 60 insertions(+) create mode 100644 src/Action/ApiPlatform/MolliePayment.php diff --git a/src/Action/ApiPlatform/MolliePayment.php b/src/Action/ApiPlatform/MolliePayment.php new file mode 100644 index 00000000..e39e7f3b --- /dev/null +++ b/src/Action/ApiPlatform/MolliePayment.php @@ -0,0 +1,57 @@ +getGatewayConfig(); + + return $gatewayConfig->getFactoryName() === self::MOLLIE_PAYMENT_METHOD_NAME; + } + + /** + * @param PaymentInterface $payment + * + * @return array + */ + public function provideConfiguration(PaymentInterface $payment): array + { + $details = $payment->getDetails(); + $methodName = isset($details['molliePaymentMethods']) ? $details['molliePaymentMethods'] : null; + + if (!$methodName) { + if (isset($details['metadata']['molliePaymentMethods'])) { + $methodName = $details['metadata']['molliePaymentMethods']; + } + } + + return [ + "method" => $methodName, + "issuer" => isset($details['selected_issuer']) ? $details['selected_issuer'] : null, + "cardToken" => isset($details['metadata']['cartToken']) ? $details['metadata']['cartToken'] : null, + "amount" => isset($details['amount']) ? $details['amount'] : null, + "customerId" => isset($details['customerId']) ? $details['customerId'] : null, + "description" => isset($details['description']) ? $details['description'] : null, + "redirectUrl" => isset($details['backurl']) ? $details['backurl'] : null, + "webhookUrl" => isset($details['webhookUrl']) ? $details['webhookUrl'] : null, + "metadata" => isset($details['metadata']) ? $details['metadata'] : null, + "locale" => isset($details['locale']) ? $details['locale'] : null + ]; + } +} diff --git a/src/Resources/config/services/action.xml b/src/Resources/config/services/action.xml index cf5034ba..eefb2d5d 100644 --- a/src/Resources/config/services/action.xml +++ b/src/Resources/config/services/action.xml @@ -17,6 +17,9 @@ + + + From 241acfb2e48562cfd4ab2784247059704b5891e8 Mon Sep 17 00:00:00 2001 From: SergejSavic Date: Thu, 26 Dec 2024 16:53:18 +0100 Subject: [PATCH 2/5] fix bug with redirecting to thank you page after completing payment --- src/Action/ApiPlatform/MolliePayment.php | 28 +++++++++++++++++++++--- src/Resources/config/services/action.xml | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Action/ApiPlatform/MolliePayment.php b/src/Action/ApiPlatform/MolliePayment.php index e39e7f3b..0499af46 100644 --- a/src/Action/ApiPlatform/MolliePayment.php +++ b/src/Action/ApiPlatform/MolliePayment.php @@ -7,11 +7,25 @@ use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\Component\Core\Model\PaymentMethodInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Sylius\Component\Core\Model\OrderInterface; class MolliePayment { private const MOLLIE_PAYMENT_METHOD_NAME = 'mollie'; + /** @var UrlGeneratorInterface */ + private $urlGenerator; + + /** + * MolliePayment constructor + */ + public function __construct( + UrlGeneratorInterface $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + /** * @param PaymentMethodInterface $paymentMethod * @@ -32,8 +46,11 @@ public function supports(PaymentMethodInterface $paymentMethod): bool */ public function provideConfiguration(PaymentInterface $payment): array { + /** @var OrderInterface $order */ + $order = $payment->getOrder(); + $details = $payment->getDetails(); - $methodName = isset($details['molliePaymentMethods']) ? $details['molliePaymentMethods'] : null; + $methodName = $details['molliePaymentMethods'] ?? null; if (!$methodName) { if (isset($details['metadata']['molliePaymentMethods'])) { @@ -41,6 +58,11 @@ public function provideConfiguration(PaymentInterface $payment): array } } + $redirectUrl = $this->urlGenerator->generate('sylius_mollie_plugin_payum', [], UrlGeneratorInterface::ABSOLUTE_URL); + $webhookUrl = $this->urlGenerator->generate('sylius_mollie_plugin_payment_webhook', [], UrlGeneratorInterface::ABSOLUTE_URL); + $redirectUrl .= '?orderId=' . $order->getId(); + $webhookUrl .= '?orderId=' . $order->getId(); + return [ "method" => $methodName, "issuer" => isset($details['selected_issuer']) ? $details['selected_issuer'] : null, @@ -48,8 +70,8 @@ public function provideConfiguration(PaymentInterface $payment): array "amount" => isset($details['amount']) ? $details['amount'] : null, "customerId" => isset($details['customerId']) ? $details['customerId'] : null, "description" => isset($details['description']) ? $details['description'] : null, - "redirectUrl" => isset($details['backurl']) ? $details['backurl'] : null, - "webhookUrl" => isset($details['webhookUrl']) ? $details['webhookUrl'] : null, + "redirectUrl" => $redirectUrl, + "webhookUrl" => $webhookUrl, "metadata" => isset($details['metadata']) ? $details['metadata'] : null, "locale" => isset($details['locale']) ? $details['locale'] : null ]; diff --git a/src/Resources/config/services/action.xml b/src/Resources/config/services/action.xml index eefb2d5d..e6a384d5 100644 --- a/src/Resources/config/services/action.xml +++ b/src/Resources/config/services/action.xml @@ -18,6 +18,7 @@ + From 914f915fe6f57399194413632d456a6f61f4aa30 Mon Sep 17 00:00:00 2001 From: SergejSavic Date: Thu, 26 Dec 2024 16:58:41 +0100 Subject: [PATCH 3/5] refactor ConvertMolliePaymentAction --- src/Action/ConvertMolliePaymentAction.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Action/ConvertMolliePaymentAction.php b/src/Action/ConvertMolliePaymentAction.php index 41f73beb..a572c522 100644 --- a/src/Action/ConvertMolliePaymentAction.php +++ b/src/Action/ConvertMolliePaymentAction.php @@ -108,9 +108,9 @@ public function execute($request): void $useSavedCards = $paymentOptions['metadata']['useSavedCards']; } else { $paymentMethod = $paymentOptions['molliePaymentMethods'] ?? null; - $cartToken = $paymentOptions['cartToken']; - $saveCardInfo = $paymentOptions['saveCardInfo']; - $useSavedCards = $paymentOptions['useSavedCards']; + $cartToken = $paymentOptions['cartToken'] ?? null; + $saveCardInfo = $paymentOptions['saveCardInfo'] ?? null; + $useSavedCards = $paymentOptions['useSavedCards'] ?? null; } /** @var MollieGatewayConfigInterface $method */ From da4490a07e9ed4f54d944f2f4bb810de9f7052f0 Mon Sep 17 00:00:00 2001 From: SergejSavic Date: Fri, 27 Dec 2024 14:49:13 +0100 Subject: [PATCH 4/5] add Sylius API instructions to installation file --- doc/installation.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/doc/installation.md b/doc/installation.md index 2440142c..936f038e 100644 --- a/doc/installation.md +++ b/doc/installation.md @@ -932,3 +932,42 @@ If you are missing translations, just clear the cache: ```bash php bin/console cache:clear ``` + +# Sylius API +In order to create Mollie payment with Sylius API, the following steps must be followed: + +- send the following request to the Sylius API in order to retrieve Mollie payment method configuration: /api/v2/shop/orders/{tokenValue}/payments/{paymentId}/configuration +- tokenValue represents order token which is saved in the sylius_order DB table +- response from this endpoint should be in the following format: + +```json +{ + "method": "ideal", + "issuer": "ideal_ABNANL2A", + "cardToken": null, + "amount": {"value":"18.75","currency":"EUR"}, + "customerId": null, + "description": "000000157", + "redirectUrl": "{redirect_url}", + "webhookUrl": "{webhook_url}", + "metadata": {"order_id":170,"customer_id":22,"molliePaymentMethods":"ideal","cartToken":null,"saveCardInfo":null,"useSavedCards":null,"selected_issuer":"ideal_ABNANL2A","methodType":"Payments API","refund_token":"{token}"}, + "locale": "en_US" +} +``` +- create the payment on Mollie, using Mollie API. Response from the above-mentioned step should be put in the request body. +Request should be sent to the POST: https://api.mollie.com/v2/payments. Bearer token should be sent in the request authorization header. +Token can be copied from the Mollie admin configuration page. + +- after payment has been created, API response will contain checkout field. User should enter this url in the browser. + +```json +{ + "checkout": + { + "href": "https://www.mollie.com/checkout/test-mode?method=ideal&token=6.voklib", + "type": "text/html" +}} +``` +- open checkout url in the browser and complete the payment + + From e4b95234d3815874011e6f5bf37074c92af3d9a6 Mon Sep 17 00:00:00 2001 From: SergejSavic Date: Fri, 27 Dec 2024 16:56:54 +0100 Subject: [PATCH 5/5] update subscription repository --- .../MollieSubscriptionRepository.php | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Repository/MollieSubscriptionRepository.php b/src/Repository/MollieSubscriptionRepository.php index 4826707e..6ded5f1a 100644 --- a/src/Repository/MollieSubscriptionRepository.php +++ b/src/Repository/MollieSubscriptionRepository.php @@ -13,14 +13,18 @@ final class MollieSubscriptionRepository extends EntityRepository implements Mol { public function findOneByOrderId(int $orderId): ?MollieSubscriptionInterface { - $qb = $this->createQueryBuilder('q'); - - $qb->leftJoin('q.orders', 'o'); - $qb->andWhere('o.id = :orderId'); - $qb->setParameter('orderId', $orderId); - - return $qb->getQuery()->getOneOrNullResult() - ; + try { + $qb = $this->createQueryBuilder('q'); + + $qb->leftJoin('q.orders', 'o'); + $qb->andWhere('o.id = :orderId'); + $qb->setParameter('orderId', $orderId); + + return $qb->getQuery()->getOneOrNullResult() + ; + } catch (\Exception $exception) { + return null; + } } public function findByOrderId(int $orderId): array