1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Controller\Action\Shop; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Provider\Apple\ApplePayDirectProviderInterface; |
15
|
|
|
use Sylius\Bundle\OrderBundle\Controller\OrderController as BaseOrderController; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
17
|
|
|
use Sylius\Component\Payment\Model\PaymentInterface; |
18
|
|
|
use Sylius\Component\Resource\Exception\UpdateHandlingException; |
19
|
|
|
use Sylius\Component\Resource\ResourceActions; |
20
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
24
|
|
|
|
25
|
|
|
final class OrderController extends BaseOrderController |
26
|
|
|
{ |
27
|
|
|
public function updateAppleOrderAction(Request $request): Response |
28
|
|
|
{ |
29
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
30
|
|
|
|
31
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
32
|
|
|
$resource = $this->findOr404($configuration); |
33
|
|
|
|
34
|
|
|
/** @var ResourceControllerEvent $event */ |
35
|
|
|
$event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); |
36
|
|
|
|
37
|
|
|
if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
38
|
|
|
throw new HttpException($event->getErrorCode(), $event->getMessage()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($event->isStopped()) { |
42
|
|
|
$eventResponse = $event->getResponse(); |
43
|
|
|
if (!empty($eventResponse)) { |
44
|
|
|
return $eventResponse; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return new JsonResponse([], Response::HTTP_BAD_REQUEST); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$this->getApplePayProviderService()->provideOrder($this->getCurrentCart(), $request); |
52
|
|
|
|
53
|
|
|
/** @var PaymentInterface $payment */ |
54
|
|
|
$payment = $this->getCurrentCart()->getLastPayment(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($payment->getState() !== PaymentInterface::STATE_COMPLETED) { |
57
|
|
|
$response = [ |
58
|
|
|
'status' => 1, |
59
|
|
|
'errors' => 'Payment not created', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
return new JsonResponse($response, Response::HTTP_BAD_REQUEST); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->resourceUpdateHandler->handle($resource, $configuration, $this->manager); |
66
|
|
|
} catch (UpdateHandlingException $exception) { |
67
|
|
|
return new JsonResponse([], Response::HTTP_BAD_REQUEST); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); |
71
|
|
|
|
72
|
|
|
$postEventResponse = $postEvent->getResponse(); |
73
|
|
|
|
74
|
|
|
if (!empty($postEventResponse)) { |
75
|
|
|
return $postEventResponse; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource); |
79
|
|
|
|
80
|
|
|
$initializeEventResponse = $initializeEvent->getResponse(); |
81
|
|
|
|
82
|
|
|
if (!empty($initializeEventResponse)) { |
83
|
|
|
return $initializeEventResponse; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$redirect = $this->redirectToRoute('sylius_shop_order_thank_you'); |
87
|
|
|
$dataResponse['returnUrl'] = $redirect->getTargetUrl(); |
|
|
|
|
88
|
|
|
$dataResponse['responseToApple'] = ['status' => 0]; |
89
|
|
|
|
90
|
|
|
$response = [ |
91
|
|
|
'success' => true, |
92
|
|
|
'data' => $dataResponse, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
return new JsonResponse($response, Response::HTTP_OK); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getApplePayProviderService(): ApplePayDirectProviderInterface |
99
|
|
|
{ |
100
|
|
|
return $this->get('bitbag_sylius_mollie_plugin.provider.apple.apple_pay_direct_provider'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|