forked from Combodo/CombodoPayumStripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripeV3UpdatePaymentStateOnCheckoutCompletedEvent.php
177 lines (148 loc) · 5.38 KB
/
StripeV3UpdatePaymentStateOnCheckoutCompletedEvent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace AppBundle\Payment;
use AppBundle\Entity\Payment;
use Combodo\StripeV3\Action\CheckoutCompletedEventAction;
use Combodo\StripeV3\Action\CheckoutCompletedInformationProvider;
use Combodo\StripeV3\Request\handleCheckoutCompletedEvent;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\Payum;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Security\TokenInterface;
use Psr\Log\LoggerInterface;
use SM\Factory\FactoryInterface;
use Sylius\Bundle\PayumBundle\Action\CapturePaymentAction;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Resource\StateMachine\StateMachineInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Webmozart\Assert\Assert;
class StripeV3UpdatePaymentStateOnCheckoutCompletedEvent implements ExtensionInterface
{
/** @var FactoryInterface */
private $factory;
/** @var HttpRequestVerifierInterface $httpRequestVerifier */
private $httpRequestVerifier;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(FactoryInterface $factory, LoggerInterface $logger)
{
$this->factory = $factory;
$this->logger = $logger;
$this->httpRequestVerifier = null;
}
/**
* This method is used by the dependency injection to avoid a false positive circular reference.
* Please, you must not call this method
*
* @param HttpRequestVerifierInterface $httpRequestVerifier
*/
public function setHttpRequestVerifier(HttpRequestVerifierInterface $httpRequestVerifier): void
{
if (null !== $this->httpRequestVerifier) {
throw new \LogicException(__METHOD__.' is not meant to be called outside of the dependency injection!');
}
$this->httpRequestVerifier = $httpRequestVerifier;
}
/**
* {@inheritdoc}
*/
public function onPreExecute(Context $context): void
{
}
/**
* {@inheritdoc}
*/
public function onExecute(Context $context): void
{
}
/**
* {@inheritdoc}
*/
public function onPostExecute(Context $context): void
{
$action = $context->getAction();
if (!$action instanceof CheckoutCompletedInformationProvider) {
return;
}
if ($context->getException() !== null) {
return;
}
$token = $action->getToken();
$status = $action->getStatus();
if (empty($token) ) {
throw new BadRequestHttpException('The token provided was not found! (see previous exceptions)');
}
if (empty($status)) {
throw new \LogicException('The request status could not be retrieved! (see previous exceptions)');
}
if (! $status->isCaptured()) {
return;//this return is pretty important!! DO NOT REMOVE IT!!!! if you do so, the user who cancels their payment will have the payment granted anyway!
}
$payment = $status->getFirstModel();
$this->runPaymentWorkflow($payment);
$this->invalidateToken($context, $token);
}
/**
* @param $payment
*/
private function runPaymentWorkflow($payment): void
{
if ($payment->getState() !== PaymentInterface::STATE_COMPLETED) {
$this->applyTransition($payment, PaymentInterface::STATE_COMPLETED);
} else {
$this->logger->debug(
"Transition skipped",
[
'target state' => PaymentInterface::STATE_COMPLETED,
'payment object' => $payment,
]
);
}
}
private function applyTransition(PaymentInterface $payment, string $nextState): void
{
/** @var StateMachineInterface $stateMachine */
$stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH);
Assert::isInstanceOf($stateMachine, StateMachineInterface::class);
if (null !== $transition = $stateMachine->getTransitionToState($nextState)) {
$stateMachine->apply($transition);
$this->logger->debug("Transition applied", [
'next state' => $nextState,
'transition' => $transition,
'payment object' => $payment,
]);
} else {
$this->logger->debug("No Transition to apply ?!?", [
'next state' => $nextState,
'transition' => $transition,
'payment object' => $payment,
]);
}
}
/**
* @param Context $context
* @param TokenInterface $token
*/
private function invalidateToken(Context $context, TokenInterface $token): void
{
/** @var handleCheckoutCompletedEvent $request */
$request = $context->getRequest();
if ($request->canTokenBeInvalidated()) {
$this->httpRequestVerifier->invalidate($token);
} else {
$this->logger->debug('The request asked to keep the token, it was not invalidated');
}
}
}