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
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusAdyenPlugin\Action; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusAdyenPlugin\Bridge\AdyenBridgeInterface; |
16
|
|
|
use Payum\Core\Action\ActionInterface; |
17
|
|
|
use Payum\Core\ApiAwareInterface; |
18
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
19
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
20
|
|
|
use Payum\Core\Exception\UnsupportedApiException; |
21
|
|
|
use Payum\Core\GatewayAwareInterface; |
22
|
|
|
use Payum\Core\GatewayAwareTrait; |
23
|
|
|
use Payum\Core\Request\GetHttpRequest; |
24
|
|
|
use Payum\Core\Request\Notify; |
25
|
|
|
|
26
|
|
|
final class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface |
27
|
|
|
{ |
28
|
|
|
use GatewayAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AdyenBridgeInterface |
32
|
|
|
*/ |
33
|
|
|
protected $api; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function setApi($api): void |
39
|
|
|
{ |
40
|
|
|
if (false === $api instanceof AdyenBridgeInterface) { |
41
|
|
|
throw new UnsupportedApiException(sprintf('Not supported. Expected %s instance to be set as api.', AdyenBridgeInterface::class)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->api = $api; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
* |
50
|
|
|
* @param Notify $request |
51
|
|
|
*/ |
52
|
|
|
public function execute($request): void |
53
|
|
|
{ |
54
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
55
|
|
|
|
56
|
|
|
$details = ArrayObject::ensureArrayObject($request->getModel()); |
57
|
|
|
|
58
|
|
|
$this->gateway->execute($httpRequest = new GetHttpRequest()); |
59
|
|
|
|
60
|
|
|
if (!isset($httpRequest->request['merchantReference']) || empty($httpRequest->request['merchantReference'])) { |
61
|
|
|
$details['response_status'] = 401; |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!isset($details['merchantReference']) || ($details['merchantReference'] !== $httpRequest->request['merchantReference'])) { |
66
|
|
|
$details['response_status'] = 402; |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (false === $this->api->verifyNotification($httpRequest->request)) { |
71
|
|
|
$details['response_status'] = 403; |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($httpRequest->request['eventCode'])) { |
76
|
|
|
$httpRequest->request['authResult'] = $httpRequest->request['eventCode']; |
77
|
|
|
|
78
|
|
|
if (AdyenBridgeInterface::AUTHORISATION === $httpRequest->request['eventCode']) { |
79
|
|
|
if (true === filter_var($httpRequest->request['success'], FILTER_VALIDATE_BOOLEAN)) { |
80
|
|
|
$httpRequest->request['authResult'] = AdyenBridgeInterface::AUTHORISED; |
81
|
|
|
} elseif (!empty($httpRequest->request['reason'])) { |
82
|
|
|
$httpRequest->request['authResult'] = AdyenBridgeInterface::REFUSED; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (AdyenBridgeInterface::REFUND === $httpRequest->request['eventCode']) { |
87
|
|
|
if (true === filter_var($httpRequest->request['success'], FILTER_VALIDATE_BOOLEAN)) { |
88
|
|
|
$httpRequest->request['authResult'] = AdyenBridgeInterface::REFUND; |
89
|
|
|
} elseif (!empty($httpRequest->request['reason'])) { |
90
|
|
|
$httpRequest->request['authResult'] = AdyenBridgeInterface::REFUSED; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$details['authResult'] = $httpRequest->request['authResult']; |
96
|
|
|
|
97
|
|
|
$details['pspReference'] = $httpRequest->request['pspReference']; |
98
|
|
|
|
99
|
|
|
$details['response_status'] = 200; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function supports($request): bool |
106
|
|
|
{ |
107
|
|
|
return |
108
|
|
|
$request instanceof Notify && |
109
|
|
|
$request->getModel() instanceof \ArrayAccess |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|