|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PH\Bundle\PayumBundle\Extension; |
|
6
|
|
|
|
|
7
|
|
|
use Payum\Core\Extension\Context; |
|
8
|
|
|
use Payum\Core\Extension\ExtensionInterface; |
|
9
|
|
|
use Payum\Core\Request\Generic; |
|
10
|
|
|
use Payum\Core\Request\GetStatusInterface; |
|
11
|
|
|
use Payum\Core\Request\Notify; |
|
12
|
|
|
use PH\Component\Core\PaymentTransitions; |
|
13
|
|
|
use SM\Factory\FactoryInterface; |
|
14
|
|
|
use Sylius\Bundle\PayumBundle\Request\GetStatus; |
|
15
|
|
|
use Sylius\Component\Payment\Model\PaymentInterface; |
|
16
|
|
|
use Sylius\Component\Resource\StateMachine\StateMachineInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class UpdatePaymentStateExtension implements ExtensionInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FactoryInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $factory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param FactoryInterface $factory |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(FactoryInterface $factory) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->factory = $factory; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function onPreExecute(Context $context) |
|
37
|
|
|
{ |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function onExecute(Context $context) |
|
44
|
|
|
{ |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function onPostExecute(Context $context) |
|
51
|
|
|
{ |
|
52
|
|
|
$previousStack = $context->getPrevious(); |
|
53
|
|
|
$previousStackSize = count($previousStack); |
|
54
|
|
|
|
|
55
|
|
|
if ($previousStackSize > 1) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (1 === $previousStackSize) { |
|
60
|
|
|
$previousActionClassName = get_class($previousStack[0]->getAction()); |
|
61
|
|
|
if (false === stripos($previousActionClassName, 'NotifyNullAction')) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var Generic $request */ |
|
67
|
|
|
$request = $context->getRequest(); |
|
68
|
|
|
if (false === $request instanceof Generic) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (false === $request instanceof GetStatusInterface && false === $request instanceof Notify) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @var PaymentInterface $payment */ |
|
77
|
|
|
$payment = $request->getFirstModel(); |
|
78
|
|
|
if (false === $payment instanceof PaymentInterface) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$context->getGateway()->execute($status = new GetStatus($payment)); |
|
83
|
|
|
$value = $status->getValue(); |
|
84
|
|
|
if ($payment->getState() !== $value && PaymentInterface::STATE_UNKNOWN !== $value) { |
|
85
|
|
|
$this->updatePaymentState($payment, $value); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param PaymentInterface $payment |
|
91
|
|
|
* @param string $nextState |
|
92
|
|
|
*/ |
|
93
|
|
|
private function updatePaymentState(PaymentInterface $payment, string $nextState) |
|
94
|
|
|
{ |
|
95
|
|
|
/** @var StateMachineInterface $stateMachine */ |
|
96
|
|
|
$stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH); |
|
97
|
|
|
|
|
98
|
|
|
if (null !== $transition = $stateMachine->getTransitionToState($nextState)) { |
|
99
|
|
|
$stateMachine->apply($transition); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|