Skip to content

Commit

Permalink
Merge pull request #2955 in NEXT/platform from ntr/add-request-bag-pa…
Browse files Browse the repository at this point in the history
…yment-handler to 6.0-dp

* commit '770e6bb47ff97e94cc5cbfefc4f46877daaa6dd4':
  NTR - Add request data bag to payment handler
  • Loading branch information
janbuecker committed May 15, 2019
2 parents c0db484 + 770e6bb commit 69abed8
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 20 deletions.
Expand Up @@ -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()]);
Expand Down
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Expand Up @@ -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
Expand All @@ -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;
}
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function __construct(
*/
public function process(
string $orderId,
RequestDataBag $dataBag,
SalesChannelContext $salesChannelContext,
?string $finishUrl = null
): ?RedirectResponse {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Checkout/Payment/PaymentService.php
Expand Up @@ -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;
Expand Down Expand Up @@ -82,6 +83,7 @@ public function __construct(
*/
public function handlePaymentByOrder(
string $orderId,
RequestDataBag $dataBag,
SalesChannelContext $context,
?string $finishUrl = null
): ?RedirectResponse {
Expand All @@ -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;
Expand Down
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Expand Up @@ -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
Expand All @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions src/Core/Checkout/Test/Payment/PaymentServiceTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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());

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down
@@ -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.
6 changes: 4 additions & 2 deletions src/Docs/_new/4-how-to/010-payment-plugin.md
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Storefront/PageController/CheckoutPageController.php
Expand Up @@ -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;
Expand Down

0 comments on commit 69abed8

Please sign in to comment.