|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\PayumBundle\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Payum\Core\Extension\Context; |
|
15
|
|
|
use Payum\Core\Extension\ExtensionInterface; |
|
16
|
|
|
use Payum\Core\Request\Generic; |
|
17
|
|
|
use Payum\Core\Request\GetStatusInterface; |
|
18
|
|
|
use Payum\Core\Request\Notify; |
|
19
|
|
|
use SM\Factory\FactoryInterface; |
|
20
|
|
|
use Sylius\Bundle\PayumBundle\Request\GetStatus; |
|
21
|
|
|
use Sylius\Component\Payment\Model\PaymentInterface; |
|
22
|
|
|
use Sylius\Component\Payment\PaymentTransitions; |
|
23
|
|
|
|
|
24
|
|
|
final class UpdatePaymentStateExtension implements ExtensionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var FactoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $factory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param FactoryInterface $factory |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(FactoryInterface $factory) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->factory = $factory; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function onPreExecute(Context $context) |
|
43
|
|
|
{ |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function onExecute(Context $context) |
|
50
|
|
|
{ |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function onPostExecute(Context $context) |
|
57
|
|
|
{ |
|
58
|
|
|
$previousStack = $context->getPrevious(); |
|
59
|
|
|
$previousStackSize = count($previousStack); |
|
60
|
|
|
|
|
61
|
|
|
if ($previousStackSize > 1) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($previousStackSize === 1) { |
|
66
|
|
|
$previousActionClassName = get_class($previousStack[0]->getAction()); |
|
|
|
|
|
|
67
|
|
|
if (false === stripos($previousActionClassName, 'NotifyNullAction')) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @var Generic $request */ |
|
73
|
|
|
$request = $context->getRequest(); |
|
74
|
|
|
if (false === $request instanceof Generic) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (false === $request instanceof GetStatusInterface && false === $request instanceof Notify) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @var PaymentInterface $payment */ |
|
83
|
|
|
$payment = $request->getFirstModel(); |
|
84
|
|
|
if (false === $payment instanceof PaymentInterface) { |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$context->getGateway()->execute($status = new GetStatus($payment)); |
|
89
|
|
|
if ($payment->getState() !== $status->getValue()) { |
|
|
|
|
|
|
90
|
|
|
$this->updatePaymentState($payment, $status->getValue()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param PaymentInterface $payment |
|
96
|
|
|
* @param string $nextState |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function updatePaymentState(PaymentInterface $payment, $nextState) |
|
99
|
|
|
{ |
|
100
|
|
|
$stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH); |
|
101
|
|
|
|
|
102
|
|
|
if (null !== $transition = $stateMachine->getTransitionToState($nextState)) { |
|
103
|
|
|
$stateMachine->apply($transition); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.