-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from synolia/feature/payment-integrated
Feature: integrated payment
- Loading branch information
Showing
51 changed files
with
1,455 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sylius_refund: | ||
resource: "@SyliusRefundPlugin/Resources/config/routing.yml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayPlug\SyliusPayPlugPlugin\Controller; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactory; | ||
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface; | ||
use PayPlug\SyliusPayPlugPlugin\Creator\PayPlugPaymentDataCreator; | ||
use PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory; | ||
use Psr\Log\LoggerInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Order\Context\CartContextInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
final class IntegratedPaymentController extends AbstractController | ||
{ | ||
private CartContextInterface $cartContext; | ||
/** | ||
* @var RepositoryInterface<\Sylius\Component\Core\Model\PaymentMethodInterface> | ||
*/ | ||
private RepositoryInterface $paymentMethodRepository; | ||
private OrderRepositoryInterface $orderRepository; | ||
private PayPlugPaymentDataCreator $paymentDataCreator; | ||
private PayPlugApiClientFactory $apiClientFactory; | ||
private EntityManagerInterface $entityManager; | ||
private LoggerInterface $logger; | ||
|
||
/** | ||
* @param RepositoryInterface<\Sylius\Component\Core\Model\PaymentMethodInterface> $paymentMethodRepository | ||
*/ | ||
public function __construct( | ||
CartContextInterface $cartContext, | ||
RepositoryInterface $paymentMethodRepository, | ||
OrderRepositoryInterface $orderRepository, | ||
PayPlugPaymentDataCreator $paymentDataCreator, | ||
PayPlugApiClientFactory $apiClientFactory, | ||
EntityManagerInterface $entityManager, | ||
LoggerInterface $logger | ||
) { | ||
$this->cartContext = $cartContext; | ||
$this->paymentMethodRepository = $paymentMethodRepository; | ||
$this->orderRepository = $orderRepository; | ||
$this->paymentDataCreator = $paymentDataCreator; | ||
$this->apiClientFactory = $apiClientFactory; | ||
$this->entityManager = $entityManager; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* The InitPayment action is called when the user clicks on the "Pay" button from the integratedPayment iframe. | ||
* | ||
* The actual payment (ie latest in cart state) of the order is sent to Payplug, | ||
* specifying the IntegratedPayment integration. | ||
* | ||
* @see https://docs.payplug.com/api/integratedref.html#trigger-a-payment | ||
*/ | ||
public function initPaymentAction(Request $request, int $paymentMethodId): Response | ||
{ | ||
$paymentMethod = $this->paymentMethodRepository->find($paymentMethodId); | ||
if (!$paymentMethod instanceof PaymentMethodInterface) { | ||
throw $this->createNotFoundException(); | ||
} | ||
|
||
$order = null; | ||
if (\is_string($orderToken = $request->query->get('orderToken'))) { | ||
$order = $this->orderRepository->findOneByTokenValue($orderToken); | ||
} | ||
if (null === $order) { | ||
$order = $this->cartContext->getCart(); | ||
} | ||
if (!$order instanceof OrderInterface) { | ||
throw $this->createNotFoundException('No order found'); | ||
} | ||
|
||
$payment = $order->getLastPayment(); | ||
if (!$payment instanceof PaymentInterface) { | ||
throw $this->createNotFoundException('No payment available'); | ||
} | ||
|
||
$payment->setMethod($paymentMethod); | ||
$factoryName = $paymentMethod->getGatewayConfig()?->getFactoryName(); | ||
if (PayPlugGatewayFactory::FACTORY_NAME !== $factoryName) { | ||
throw new BadRequestHttpException('Unsupported payment method of Integrated Payment'); | ||
} | ||
|
||
$paymentData = $this->paymentDataCreator->create($payment, $factoryName); | ||
// Mandatory | ||
$paymentData['integration'] = PayPlugApiClientInterface::INTEGRATED_PAYMENT_INTEGRATION; | ||
$this->logger->debug('Payplug Payment data for creation', $paymentData->getArrayCopy()); | ||
|
||
$apiClient = $this->apiClientFactory->create($factoryName); | ||
$payplugPayment = $apiClient->createPayment($paymentData->getArrayCopy()); | ||
$this->logger->debug('PayPlug payment created', (array) $payplugPayment); | ||
|
||
$paymentData['payment_id'] = $payplugPayment->id; | ||
$paymentData['is_live'] = $payplugPayment->is_live; | ||
$payment->setDetails($paymentData->getArrayCopy()); | ||
|
||
$this->entityManager->flush(); | ||
|
||
return new JsonResponse([ | ||
'payment_id' => $payplugPayment->id, | ||
], Response::HTTP_CREATED); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
src/EventSubscriber/PostPaymentSelectEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayPlug\SyliusPayPlugPlugin\EventSubscriber; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface; | ||
use SM\Factory\FactoryInterface; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\OrderCheckoutTransitions; | ||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class PostPaymentSelectEventSubscriber implements EventSubscriberInterface | ||
{ | ||
private const CHECKOUT_ROUTE = 'sylius_shop_checkout_select_payment'; | ||
private const UPDATE_ORDER_PAYMENT_ROUTE = 'sylius_shop_order_show'; | ||
|
||
private const TOKEN_FIELD = 'payplug_integrated_payment_token'; | ||
|
||
public function __construct( | ||
private RequestStack $requestStack, | ||
private EntityManagerInterface $entityManager, | ||
private FactoryInterface $stateMachineFactory, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RequestEvent::class => 'alterRequestConfigurationForIntegratedPayment', | ||
'sylius.order.post_payment' => 'handle', | ||
'sylius.order.post_update' => 'handle', | ||
]; | ||
} | ||
|
||
public function alterRequestConfigurationForIntegratedPayment(RequestEvent $event): void | ||
{ | ||
$request = $event->getRequest(); | ||
if (!$this->hasToken($request) || self::CHECKOUT_ROUTE !== $request->attributes->get('_route')) { | ||
return; | ||
} | ||
if (!$request->attributes->has('_sylius')) { | ||
return; | ||
} | ||
|
||
$syliusRequestConfig = $request->attributes->get('_sylius'); | ||
if (!\is_array($syliusRequestConfig)) { | ||
return; | ||
} | ||
|
||
$syliusRequestConfig['redirect'] = [ | ||
'route' => 'sylius_shop_order_pay', | ||
'parameters' => ['tokenValue' => 'resource.tokenValue'], | ||
]; | ||
|
||
$request->attributes->set('_sylius', $syliusRequestConfig); | ||
} | ||
public function handle(ResourceControllerEvent $resourceControllerEvent): void | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
if (null === $request) { | ||
return; | ||
} | ||
|
||
if (!\in_array($request->attributes->get('_route'), [self::CHECKOUT_ROUTE, self::UPDATE_ORDER_PAYMENT_ROUTE], true)) { | ||
return; | ||
} | ||
|
||
/** @var \Sylius\Component\Core\Model\OrderInterface $order */ | ||
$order = $resourceControllerEvent->getSubject(); | ||
$lastPayment = $order->getLastPayment(); | ||
if (null === $lastPayment) { | ||
return; | ||
} | ||
|
||
if (!$this->hasToken($request)) { | ||
return; | ||
|
||
} | ||
$this->handleToken($resourceControllerEvent, $request, $lastPayment); | ||
} | ||
|
||
private function handleToken( | ||
ResourceControllerEvent $resourceControllerEvent, | ||
Request $request, | ||
PaymentInterface $lastPayment, | ||
): void { | ||
$token = $this->getToken($request); | ||
|
||
$lastPayment->setDetails(\array_merge( | ||
$lastPayment->getDetails(), | ||
[ | ||
'payment_id' => $token, | ||
'status' => PaymentInterface::STATE_PROCESSING, | ||
] | ||
)); | ||
|
||
$resource = $resourceControllerEvent->getSubject(); | ||
Assert::isInstanceOf($resource, ResourceInterface::class); | ||
|
||
$this->applyToComplete($lastPayment->getOrder() ?? throw new \LogicException('Order not found for payment')); | ||
} | ||
|
||
private function hasToken(Request $request): bool | ||
{ | ||
if (!$request->request->has(self::TOKEN_FIELD)) { | ||
return false; | ||
} | ||
|
||
$token = $this->getToken($request); | ||
|
||
return '' !== $token; | ||
} | ||
|
||
private function getToken(Request $request): string | ||
{ | ||
$token = $request->request->get(self::TOKEN_FIELD); | ||
Assert::string($token); | ||
|
||
return $token; | ||
} | ||
|
||
private function applyToComplete(OrderInterface $order): void | ||
{ | ||
$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH); | ||
if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE)) { | ||
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE); | ||
} | ||
|
||
$this->entityManager->flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.