1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusBraintreePlugin\Action; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusBraintreePlugin\ApiClient\BraintreeApiClientInterface; |
8
|
|
|
use Payum\Core\Action\ActionInterface; |
9
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
10
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
11
|
|
|
use Payum\Core\Request\GetStatusInterface; |
12
|
|
|
|
13
|
|
|
final class StatusAction implements ActionInterface |
14
|
|
|
{ |
15
|
|
|
public function execute($request): void |
16
|
|
|
{ |
17
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
18
|
|
|
|
19
|
|
|
$details = ArrayObject::ensureArrayObject($request->getModel()); |
20
|
|
|
|
21
|
|
|
$status = $details['status']; |
22
|
|
|
|
23
|
|
|
if (null != $status) { |
24
|
|
|
switch ($status) { |
25
|
|
|
case BraintreeApiClientInterface::FAILED: |
26
|
|
|
$request->markFailed(); |
27
|
|
|
|
28
|
|
|
return; |
29
|
|
|
case BraintreeApiClientInterface::AUTHORIZED: |
30
|
|
|
if ($this->hasSuccessfulTransaction($details)) { |
31
|
|
|
$request->markAuthorized(); |
32
|
|
|
} else { |
33
|
|
|
$request->markUnknown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return; |
37
|
|
|
case BraintreeApiClientInterface::CAPTURED: |
38
|
|
|
if ($this->hasSuccessfulTransaction($details)) { |
39
|
|
|
$request->markCaptured(); |
40
|
|
|
} else { |
41
|
|
|
$request->markUnknown(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return; |
45
|
|
|
case BraintreeApiClientInterface::REFUNDED: |
46
|
|
|
if ($this->hasSuccessfulTransaction($details)) { |
47
|
|
|
$request->markRefunded(); |
48
|
|
|
} else { |
49
|
|
|
$request->markUnknown(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($details['paymentMethodNonce']) { |
57
|
|
|
$request->markPending(); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$request->markNew(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function supports($request): bool |
66
|
|
|
{ |
67
|
|
|
return |
68
|
|
|
$request instanceof GetStatusInterface && |
69
|
|
|
$request->getModel() instanceof \ArrayAccess |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function hasSuccessfulTransaction($details): bool |
74
|
|
|
{ |
75
|
|
|
return $details['sale'] && $details['sale']['success']; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|