Skip to content

Commit

Permalink
Merge pull request #43 from logeecom/PISYL-267/implement-sylius-api
Browse files Browse the repository at this point in the history
Add support for Sylius API
  • Loading branch information
hwysoszynski authored Jan 15, 2025
2 parents 1448b7b + e4b9523 commit 21cc78c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 11 deletions.
39 changes: 39 additions & 0 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


79 changes: 79 additions & 0 deletions src/Action/ApiPlatform/MolliePayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace SyliusMolliePlugin\Action\ApiPlatform;

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
*
* @return bool
*/
public function supports(PaymentMethodInterface $paymentMethod): bool
{
/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();

return $gatewayConfig->getFactoryName() === self::MOLLIE_PAYMENT_METHOD_NAME;
}

/**
* @param PaymentInterface $payment
*
* @return array
*/
public function provideConfiguration(PaymentInterface $payment): array
{
/** @var OrderInterface $order */
$order = $payment->getOrder();

$details = $payment->getDetails();
$methodName = $details['molliePaymentMethods'] ?? null;

if (!$methodName) {
if (isset($details['metadata']['molliePaymentMethods'])) {
$methodName = $details['metadata']['molliePaymentMethods'];
}
}

$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,
"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" => $redirectUrl,
"webhookUrl" => $webhookUrl,
"metadata" => isset($details['metadata']) ? $details['metadata'] : null,
"locale" => isset($details['locale']) ? $details['locale'] : null
];
}
}
6 changes: 3 additions & 3 deletions src/Action/ConvertMolliePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
20 changes: 12 additions & 8 deletions src/Repository/MollieSubscriptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services/action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<argument type="service" id="sylius_mollie_plugin.logger.mollie_logger_action"/>
<tag name="payum.action" factory="mollie" alias="payum.action.create_order"/>
</service>
<service id="sylius_mollie_plugin.action.mollie_payment" class="SyliusMolliePlugin\Action\ApiPlatform\MolliePayment">
<argument type="service" id="router" />
<tag name="sylius.api.payment_method_handler" />
</service>
<service class="SyliusMolliePlugin\Action\Api\CreateOnDemandPaymentAction" id="sylius_mollie_plugin.action.api.create_on_demand_payment_action">
<argument id="sylius_mollie_plugin.logger.mollie_logger_action" type="service"/>
<argument id="sylius_mollie_plugin.parser.response.guzzle_negative_response_parser" type="service"/>
Expand Down

0 comments on commit 21cc78c

Please sign in to comment.