-
Notifications
You must be signed in to change notification settings - Fork 978
/
PaymentController.php
73 lines (61 loc) · 2.64 KB
/
PaymentController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Payment\Controller;
use Shopware\Core\Checkout\Payment\Exception\AsyncPaymentFinalizeException;
use Shopware\Core\Checkout\Payment\Exception\CustomerCanceledAsyncPaymentException;
use Shopware\Core\Checkout\Payment\Exception\InvalidTransactionException;
use Shopware\Core\Checkout\Payment\Exception\PaymentProcessException;
use Shopware\Core\Checkout\Payment\Exception\TokenExpiredException;
use Shopware\Core\Checkout\Payment\Exception\UnknownPaymentMethodException;
use Shopware\Core\Checkout\Payment\PaymentService;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class PaymentController extends AbstractController
{
/**
* @var PaymentService
*/
private $paymentService;
public function __construct(PaymentService $paymentService)
{
$this->paymentService = $paymentService;
}
/**
* @Since("6.0.0.0")
* @RouteScope(scopes={"storefront"})
* @Route("/payment/finalize-transaction", defaults={"auth_required"=false}, name="payment.finalize.transaction", methods={"GET", "POST"})
*
* @throws AsyncPaymentFinalizeException
* @throws CustomerCanceledAsyncPaymentException
* @throws InvalidTransactionException
* @throws TokenExpiredException
* @throws UnknownPaymentMethodException
*/
public function finalizeTransaction(Request $request, SalesChannelContext $salesChannelContext): Response
{
$paymentToken = $request->get('_sw_payment_token');
$result = $this->paymentService->finalizeTransaction(
$paymentToken,
$request,
$salesChannelContext
);
$exception = $result->getException();
if ($exception !== null) {
$url = $result->getErrorUrl();
if ($url !== null && $exception instanceof PaymentProcessException) {
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'error-code=' . $exception->getErrorCode();
return new RedirectResponse($url);
}
}
if ($result->getFinishUrl()) {
return new RedirectResponse($result->getFinishUrl());
}
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}
}