From 770e6bb47ff97e94cc5cbfefc4f46877daaa6dd4 Mon Sep 17 00:00:00 2001 From: Michael Telgmann Date: Wed, 15 May 2019 08:16:56 +0200 Subject: [PATCH] NTR - Add request data bag to payment handler --- .../SalesChannel/SalesChannelCheckoutController.php | 6 +++--- .../AsynchronousPaymentHandlerInterface.php | 3 ++- .../Payment/Cart/PaymentHandler/DefaultPayment.php | 3 ++- .../SynchronousPaymentHandlerInterface.php | 3 ++- .../Cart/PaymentTransactionChainProcessor.php | 6 ++++-- src/Core/Checkout/Payment/PaymentService.php | 4 +++- .../Payment/Handler/AsyncTestPaymentHandler.php | 3 ++- .../Test/Payment/Handler/SyncTestPaymentHandler.php | 3 ++- .../Checkout/Test/Payment/PaymentServiceTest.php | 13 +++++++------ .../2019-05-13-change-payment-handler-interface.md | 6 ++++++ src/Docs/_new/4-how-to/010-payment-plugin.md | 6 ++++-- .../PageController/CheckoutPageController.php | 2 +- 12 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 src/Docs/Resources/platform-updates/2019-05-13-change-payment-handler-interface.md diff --git a/src/Core/Checkout/Cart/SalesChannel/SalesChannelCheckoutController.php b/src/Core/Checkout/Cart/SalesChannel/SalesChannelCheckoutController.php index e9fc579f9d4..fece41f0535 100644 --- a/src/Core/Checkout/Cart/SalesChannel/SalesChannelCheckoutController.php +++ b/src/Core/Checkout/Cart/SalesChannel/SalesChannelCheckoutController.php @@ -157,10 +157,10 @@ public function getDeepLinkOrder(string $id, Request $request, Context $context) * @throws SyncPaymentProcessException * @throws UnknownPaymentMethodException */ - public function payOrder(string $orderId, Request $request, SalesChannelContext $context): Response + public function payOrder(string $orderId, RequestDataBag $dataBag, SalesChannelContext $context): Response { - $finishUrl = $request->request->get('finishUrl'); - $response = $this->paymentService->handlePaymentByOrder($orderId, $context, $finishUrl); + $finishUrl = $dataBag->get('finishUrl'); + $response = $this->paymentService->handlePaymentByOrder($orderId, $dataBag, $context, $finishUrl); if ($response) { return new JsonResponse(['paymentUrl' => $response->getTargetUrl()]); diff --git a/src/Core/Checkout/Payment/Cart/PaymentHandler/AsynchronousPaymentHandlerInterface.php b/src/Core/Checkout/Payment/Cart/PaymentHandler/AsynchronousPaymentHandlerInterface.php index 0affbbb1dce..f3af7aaa059 100644 --- a/src/Core/Checkout/Payment/Cart/PaymentHandler/AsynchronousPaymentHandlerInterface.php +++ b/src/Core/Checkout/Payment/Cart/PaymentHandler/AsynchronousPaymentHandlerInterface.php @@ -6,6 +6,7 @@ use Shopware\Core\Checkout\Payment\Exception\AsyncPaymentFinalizeException; use Shopware\Core\Checkout\Payment\Exception\AsyncPaymentProcessException; use Shopware\Core\Checkout\Payment\Exception\CustomerCanceledAsyncPaymentException; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -22,7 +23,7 @@ interface AsynchronousPaymentHandlerInterface * * @throws AsyncPaymentProcessException */ - public function pay(AsyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): RedirectResponse; + public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse; /** * The finalize function will be called when the user is redirected back to shop from the payment gateway. diff --git a/src/Core/Checkout/Payment/Cart/PaymentHandler/DefaultPayment.php b/src/Core/Checkout/Payment/Cart/PaymentHandler/DefaultPayment.php index 845792a0e38..73efaa2c2fb 100644 --- a/src/Core/Checkout/Payment/Cart/PaymentHandler/DefaultPayment.php +++ b/src/Core/Checkout/Payment/Cart/PaymentHandler/DefaultPayment.php @@ -4,6 +4,7 @@ use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler; use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; class DefaultPayment implements SynchronousPaymentHandlerInterface @@ -18,7 +19,7 @@ public function __construct(OrderTransactionStateHandler $transactionStateHandle $this->transactionStateHandler = $transactionStateHandler; } - public function pay(SyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): void + public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void { $context = $salesChannelContext->getContext(); $this->transactionStateHandler->open($transaction->getOrderTransaction()->getId(), $context); diff --git a/src/Core/Checkout/Payment/Cart/PaymentHandler/SynchronousPaymentHandlerInterface.php b/src/Core/Checkout/Payment/Cart/PaymentHandler/SynchronousPaymentHandlerInterface.php index d6a192289a4..88f7d93c9bb 100644 --- a/src/Core/Checkout/Payment/Cart/PaymentHandler/SynchronousPaymentHandlerInterface.php +++ b/src/Core/Checkout/Payment/Cart/PaymentHandler/SynchronousPaymentHandlerInterface.php @@ -4,6 +4,7 @@ use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct; use Shopware\Core\Checkout\Payment\Exception\SyncPaymentProcessException; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; interface SynchronousPaymentHandlerInterface @@ -16,5 +17,5 @@ interface SynchronousPaymentHandlerInterface * * @throws SyncPaymentProcessException */ - public function pay(SyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): void; + public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void; } diff --git a/src/Core/Checkout/Payment/Cart/PaymentTransactionChainProcessor.php b/src/Core/Checkout/Payment/Cart/PaymentTransactionChainProcessor.php index 36c69176c14..0eba92bb1cb 100644 --- a/src/Core/Checkout/Payment/Cart/PaymentTransactionChainProcessor.php +++ b/src/Core/Checkout/Payment/Cart/PaymentTransactionChainProcessor.php @@ -15,6 +15,7 @@ use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -69,6 +70,7 @@ public function __construct( */ public function process( string $orderId, + RequestDataBag $dataBag, SalesChannelContext $salesChannelContext, ?string $finishUrl = null ): ?RedirectResponse { @@ -103,7 +105,7 @@ public function process( try { $paymentHandler = $this->paymentHandlerRegistry->getSync($paymentMethod->getHandlerIdentifier()); $paymentTransaction = new SyncPaymentTransactionStruct($transaction, $order); - $paymentHandler->pay($paymentTransaction, $salesChannelContext); + $paymentHandler->pay($paymentTransaction, $dataBag, $salesChannelContext); return null; } catch (UnknownPaymentMethodException $e) { @@ -116,7 +118,7 @@ public function process( $paymentHandler = $this->paymentHandlerRegistry->getAsync($paymentMethod->getHandlerIdentifier()); - return $paymentHandler->pay($paymentTransaction, $salesChannelContext); + return $paymentHandler->pay($paymentTransaction, $dataBag, $salesChannelContext); } return null; diff --git a/src/Core/Checkout/Payment/PaymentService.php b/src/Core/Checkout/Payment/PaymentService.php index c8f1409ee24..0ed80db0d2b 100644 --- a/src/Core/Checkout/Payment/PaymentService.php +++ b/src/Core/Checkout/Payment/PaymentService.php @@ -22,6 +22,7 @@ use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\Uuid\Uuid; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -82,6 +83,7 @@ public function __construct( */ public function handlePaymentByOrder( string $orderId, + RequestDataBag $dataBag, SalesChannelContext $context, ?string $finishUrl = null ): ?RedirectResponse { @@ -90,7 +92,7 @@ public function handlePaymentByOrder( } try { - return $this->paymentProcessor->process($orderId, $context, $finishUrl); + return $this->paymentProcessor->process($orderId, $dataBag, $context, $finishUrl); } catch (AsyncPaymentProcessException | SyncPaymentProcessException $e) { $this->cancelOrderTransaction($e->getOrderTransactionId(), $context->getContext()); throw $e; diff --git a/src/Core/Checkout/Test/Payment/Handler/AsyncTestPaymentHandler.php b/src/Core/Checkout/Test/Payment/Handler/AsyncTestPaymentHandler.php index a85f9df3af5..f1d8c955e44 100644 --- a/src/Core/Checkout/Test/Payment/Handler/AsyncTestPaymentHandler.php +++ b/src/Core/Checkout/Test/Payment/Handler/AsyncTestPaymentHandler.php @@ -6,6 +6,7 @@ use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct; use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface; use Shopware\Core\Checkout\Payment\Exception\CustomerCanceledAsyncPaymentException; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -24,7 +25,7 @@ public function __construct(OrderTransactionStateHandler $transactionStateHandle $this->transactionStateHandler = $transactionStateHandler; } - public function pay(AsyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): RedirectResponse + public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse { return new RedirectResponse(self::REDIRECT_URL); } diff --git a/src/Core/Checkout/Test/Payment/Handler/SyncTestPaymentHandler.php b/src/Core/Checkout/Test/Payment/Handler/SyncTestPaymentHandler.php index 8d40211da3f..6cb4b0aacee 100644 --- a/src/Core/Checkout/Test/Payment/Handler/SyncTestPaymentHandler.php +++ b/src/Core/Checkout/Test/Payment/Handler/SyncTestPaymentHandler.php @@ -6,6 +6,7 @@ use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface; use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct; use Shopware\Core\Checkout\Payment\Exception\SyncPaymentProcessException; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; class SyncTestPaymentHandler implements SynchronousPaymentHandlerInterface @@ -20,7 +21,7 @@ public function __construct(OrderTransactionStateHandler $transactionStateHandle $this->transactionStateHandler = $transactionStateHandler; } - public function pay(SyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): void + public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void { $transactionId = $transaction->getOrderTransaction()->getId(); $order = $transaction->getOrder(); diff --git a/src/Core/Checkout/Test/Payment/PaymentServiceTest.php b/src/Core/Checkout/Test/Payment/PaymentServiceTest.php index 4e9892234cf..1d38b4a6686 100644 --- a/src/Core/Checkout/Test/Payment/PaymentServiceTest.php +++ b/src/Core/Checkout/Test/Payment/PaymentServiceTest.php @@ -27,6 +27,7 @@ use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour; use Shopware\Core\Framework\Uuid\Uuid; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Shopware\Core\System\StateMachine\StateMachineRegistry; use Symfony\Component\HttpFoundation\Request; @@ -93,7 +94,7 @@ public function testHandlePaymentByOrderWithInvalidOrderId(): void $salesChannelContext = Generator::createSalesChannelContext(); $this->expectException(InvalidOrderException::class); $this->expectExceptionMessage(sprintf('The order with id %s is invalid or could not be found.', $orderId)); - $this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext); + $this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext); } public function testHandlePaymentByOrderSyncPayment(): void @@ -105,7 +106,7 @@ public function testHandlePaymentByOrderSyncPayment(): void $salesChannelContext = $this->getSalesChannelContext($paymentMethodId); - static::assertNull($this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext)); + static::assertNull($this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext)); } public function testHandlePaymentByOrderAsyncPayment(): void @@ -117,7 +118,7 @@ public function testHandlePaymentByOrderAsyncPayment(): void $salesChannelContext = $this->getSalesChannelContext($paymentMethodId); - $response = $this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext); + $response = $this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext); static::assertEquals(AsyncTestPaymentHandler::REDIRECT_URL, $response->getTargetUrl()); } @@ -131,7 +132,7 @@ public function testHandlePaymentByOrderAsyncPaymentWithFinalize(): void $salesChannelContext = $this->getSalesChannelContext($paymentMethodId); - $response = $this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext); + $response = $this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext); static::assertEquals(AsyncTestPaymentHandler::REDIRECT_URL, $response->getTargetUrl()); @@ -162,7 +163,7 @@ public function testHandlePaymentByOrderDefaultPayment(): void $salesChannelContext = $this->getSalesChannelContext($paymentMethodId); - static::assertNull($this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext)); + static::assertNull($this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext)); } public function testFinalizeTransactionWithInvalidToken(): void @@ -193,7 +194,7 @@ public function testFinalizeTransactionCustomerCanceled(): void $salesChannelContext = $this->getSalesChannelContext($paymentMethodId); - $response = $this->paymentService->handlePaymentByOrder($orderId, $salesChannelContext); + $response = $this->paymentService->handlePaymentByOrder($orderId, new RequestDataBag(), $salesChannelContext); static::assertEquals(AsyncTestPaymentHandler::REDIRECT_URL, $response->getTargetUrl()); diff --git a/src/Docs/Resources/platform-updates/2019-05-13-change-payment-handler-interface.md b/src/Docs/Resources/platform-updates/2019-05-13-change-payment-handler-interface.md new file mode 100644 index 00000000000..fa55ecae666 --- /dev/null +++ b/src/Docs/Resources/platform-updates/2019-05-13-change-payment-handler-interface.md @@ -0,0 +1,6 @@ +[titleEn]: <>(Added RequestDataBag to interface of payment handler) + +The `\Shopware\Core\Framework\Validation\DataBag\RequestDataBag` was added to the pay methods of + `\Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface` and +`\Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface`. +With this, you are able to send custom parameter into the payment handling. diff --git a/src/Docs/_new/4-how-to/010-payment-plugin.md b/src/Docs/_new/4-how-to/010-payment-plugin.md index 72839a3aa69..0f0b9cbe285 100644 --- a/src/Docs/_new/4-how-to/010-payment-plugin.md +++ b/src/Docs/_new/4-how-to/010-payment-plugin.md @@ -74,6 +74,7 @@ use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandle use Shopware\Core\Checkout\Payment\Exception\AsyncPaymentProcessException; use Shopware\Core\Checkout\Payment\Exception\CustomerCanceledAsyncPaymentException; use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -92,7 +93,7 @@ class ExamplePayment implements AsynchronousPaymentHandlerInterface /** * @throws AsyncPaymentProcessException */ - public function pay(AsyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): RedirectResponse + public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse { // Method that sends the return URL to the external gateway and gets a redirect URL back try { @@ -160,6 +161,7 @@ namespace PaymentPlugin\Service; use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface; use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct; use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler; +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; use Shopware\Core\System\SalesChannel\SalesChannelContext; class ExamplePayment implements SynchronousPaymentHandlerInterface @@ -174,7 +176,7 @@ class ExamplePayment implements SynchronousPaymentHandlerInterface $this->transactionStateHandler = $transactionStateHandler; } - public function pay(SyncPaymentTransactionStruct $transaction, SalesChannelContext $salesChannelContext): void + public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void { $context = $salesChannelContext->getContext(); $this->transactionStateHandler->complete($transaction->getOrderTransaction()->getId(), $context); diff --git a/src/Storefront/PageController/CheckoutPageController.php b/src/Storefront/PageController/CheckoutPageController.php index 0b218a0107c..027fe86b25c 100644 --- a/src/Storefront/PageController/CheckoutPageController.php +++ b/src/Storefront/PageController/CheckoutPageController.php @@ -249,7 +249,7 @@ public function finishOrder(RequestDataBag $data, SalesChannelContext $context): 'orderId' => $orderId, ]); - $response = $this->paymentService->handlePaymentByOrder($orderId, $context, $finishUrl); + $response = $this->paymentService->handlePaymentByOrder($orderId, $data, $context, $finishUrl); if ($response !== null) { return $response;