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\SyliusQuadPayPlugin\Action; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusQuadPayPlugin\Action\Api\ApiAwareTrait; |
16
|
|
|
use BitBag\SyliusQuadPayPlugin\Client\QuadPayApiClientInterface; |
17
|
|
|
use GuzzleHttp\Exception\ClientException; |
18
|
|
|
use Payum\Core\Action\ActionInterface; |
19
|
|
|
use Payum\Core\ApiAwareInterface; |
20
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
21
|
|
|
use Payum\Core\GatewayAwareInterface; |
22
|
|
|
use Payum\Core\GatewayAwareTrait; |
23
|
|
|
use Payum\Core\Request\GetHttpRequest; |
24
|
|
|
use Payum\Core\Request\GetStatusInterface; |
25
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
27
|
|
|
|
28
|
|
|
final class StatusAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface |
29
|
|
|
{ |
30
|
|
|
use GatewayAwareTrait; |
31
|
|
|
use ApiAwareTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
* |
36
|
|
|
* @param GetStatusInterface $request |
37
|
|
|
*/ |
38
|
|
|
public function execute($request): void |
39
|
|
|
{ |
40
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
41
|
|
|
|
42
|
|
|
/** @var PaymentInterface $payment */ |
43
|
|
|
$payment = $request->getModel(); |
44
|
|
|
|
45
|
|
|
$details = $payment->getDetails(); |
46
|
|
|
|
47
|
|
|
if (!isset($details['orderToken'])) { |
48
|
|
|
$request->markNew(); |
49
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->gateway->execute($httpRequest = new GetHttpRequest()); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
if (isset($details['orderId'])) { |
57
|
|
|
$order = $this->quadPayApiClient->getOrderById($details['orderId']); |
58
|
|
|
} else { |
59
|
|
|
$order = $this->quadPayApiClient->getOrderByToken($details['orderToken']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$details['orderId'] = $order['orderId']; |
63
|
|
|
$details['orderStatus'] = strtolower($order['orderStatus']); |
64
|
|
|
} catch (ClientException $clientException) { |
65
|
|
|
if ( |
66
|
|
|
(Response::HTTP_NOT_FOUND === $clientException->getCode() && |
67
|
|
|
isset($httpRequest->query['status']) && QuadPayApiClientInterface::STATUS_ABANDONED === $httpRequest->query['status']) || |
68
|
|
|
QuadPayApiClientInterface::STATUS_ABANDONED === $details['orderStatus'] |
69
|
|
|
) { |
70
|
|
|
$details['orderStatus'] = QuadPayApiClientInterface::STATUS_ABANDONED; |
71
|
|
|
} else { |
72
|
|
|
$details['orderStatus'] = QuadPayApiClientInterface::STATUS_DECLINED; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$payment->setDetails($details); |
77
|
|
|
|
78
|
|
|
switch ($details['orderStatus']) { |
79
|
|
|
case QuadPayApiClientInterface::STATUS_CREATED: |
80
|
|
|
$request->markPending(); |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
case QuadPayApiClientInterface::STATUS_ABANDONED: |
84
|
|
|
$request->markCanceled(); |
85
|
|
|
|
86
|
|
|
break; |
87
|
|
|
case QuadPayApiClientInterface::STATUS_DECLINED: |
88
|
|
|
$request->markFailed(); |
89
|
|
|
|
90
|
|
|
break; |
91
|
|
|
case QuadPayApiClientInterface::STATUS_APPROVED: |
92
|
|
|
$request->markCaptured(); |
93
|
|
|
|
94
|
|
|
break; |
95
|
|
|
default: |
96
|
|
|
$request->markUnknown(); |
97
|
|
|
|
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function supports($request): bool |
103
|
|
|
{ |
104
|
|
|
return |
105
|
|
|
$request instanceof GetStatusInterface && |
106
|
|
|
$request->getModel() instanceof PaymentInterface |
107
|
|
|
; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|