-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPayum.php
More file actions
311 lines (265 loc) · 10 KB
/
Copy pathPayum.php
File metadata and controls
311 lines (265 loc) · 10 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
namespace Payum\Core;
use Exception;
use Payum\Core\Command\CaptureCommand;
use Payum\Core\Command\NotifyCommand;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\WebhookNotVerifiedException;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Registry\RegistryInterface;
use Payum\Core\Reply\HttpPostRedirect;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Reply\ReplyInterface;
use Payum\Core\Request\Capture;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Request\Notify;
use Payum\Core\Result\Acknowledgement;
use Payum\Core\Result\NextAction;
use Payum\Core\Result\NextAction\PostRedirect;
use Payum\Core\Result\NextAction\Redirect;
use Payum\Core\Result\NextAction\RenderTemplate;
use Payum\Core\Result\NotifyResult;
use Payum\Core\Result\Result;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Security\TokenInterface;
use Payum\Core\Storage\StorageInterface;
use Payum\Core\Template\RendererInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @template StorageType of object
* @implements RegistryInterface<StorageType>
*/
class Payum implements RegistryInterface
{
/**
* @var RegistryInterface<StorageType>
*/
protected RegistryInterface $registry;
protected HttpRequestVerifierInterface $httpRequestVerifier;
protected GenericTokenFactoryInterface $tokenFactory;
/**
* @var StorageInterface<TokenInterface>
*/
protected StorageInterface $tokenStorage;
protected ?RendererInterface $renderer;
/**
* @param RegistryInterface<StorageType> $registry
* @param StorageInterface<TokenInterface> $tokenStorage
* @param RendererInterface|null $renderer optional only so hand-built instances keep working; PayumBuilder always supplies one
*/
public function __construct(
RegistryInterface $registry,
HttpRequestVerifierInterface $httpRequestVerifier,
GenericTokenFactoryInterface $tokenFactory,
StorageInterface $tokenStorage,
?RendererInterface $renderer = null
) {
$this->registry = $registry;
$this->httpRequestVerifier = $httpRequestVerifier;
$this->tokenFactory = $tokenFactory;
$this->tokenStorage = $tokenStorage;
$this->renderer = $renderer;
}
/**
* Renders the template a RenderTemplate result names.
*
* One renderer serves every gateway, which is why it lives here rather than on a gateway: a gateway
* cannot replace it, and reaching it through one implied otherwise.
*/
public function renderer(): RendererInterface
{
if (! $this->renderer instanceof RendererInterface) {
throw new LogicException(sprintf(
'This %s was built without a renderer. Build it with %s, or pass a %s to the constructor.',
self::class,
PayumBuilder::class,
RendererInterface::class,
));
}
return $this->renderer;
}
public function getGatewayFactory(string $name): GatewayFactoryInterface
{
return $this->registry->getGatewayFactory($name);
}
public function getGatewayFactories(): array
{
return $this->registry->getGatewayFactories();
}
/**
* @throws InvalidArgumentException
*/
public function getGateway(string $name): GatewayInterface
{
return $this->registry->getGateway($name);
}
public function getGateways(): array
{
return $this->registry->getGateways();
}
/**
* @param class-string<StorageType> $class
* @return StorageInterface<StorageType>
*/
public function getStorage($class): StorageInterface
{
return $this->registry->getStorage($class);
}
/**
* @return array<class-string, StorageInterface<StorageType>>
*/
public function getStorages(): array
{
return $this->registry->getStorages();
}
public function getHttpRequestVerifier(): HttpRequestVerifierInterface
{
return $this->httpRequestVerifier;
}
public function getTokenFactory(): GenericTokenFactoryInterface
{
return $this->tokenFactory;
}
/**
* @return StorageInterface<TokenInterface>
*/
public function getTokenStorage(): StorageInterface
{
return $this->tokenStorage;
}
/**
* Persist the model and create the capture token that starts the payment.
*
* The storage is resolved from the model's own class, so any model with a registered
* storage works here: a Payment, an ArrayObject of gateway details, or your own order
* class. When no after path is given the one configured on the builder is used.
*
* @param array<string, mixed> $afterParameters
*
* @throws InvalidArgumentException if the model has no registered storage
*/
public function prepare(
string $gatewayName,
object $model,
?string $afterPath = null,
array $afterParameters = []
): TokenInterface {
$this->getStorage($model::class)->update($model);
return $this->tokenFactory->createCaptureToken(
$gatewayName,
$model,
$afterPath,
$afterParameters
);
}
/**
* @param Request|array<string, mixed>|null $request
*
* @throws Exception
*/
public function capture(Request | array | null $request = null): Response
{
$token = $this->httpRequestVerifier->verify($request ?: Request::createFromGlobals());
$gateway = $this->getGateway($token->getGatewayName());
if ($gateway instanceof Gateway && $gateway->supportsCommand(CaptureCommand::class)) {
return $this->respondTo($gateway->execute(new CaptureCommand($token)), $token);
}
$reply = $gateway->execute(new Capture($token), true);
if ($reply instanceof HttpRedirect) {
return new RedirectResponse($reply->getUrl(), $reply->getStatusCode(), $reply->getHeaders());
}
if ($reply instanceof HttpPostRedirect) {
return new Response($reply->getContent(), $reply->getStatusCode(), $reply->getHeaders());
}
return new RedirectResponse($token->getAfterUrl());
}
/**
* @param Request|array<string, mixed>|null $request
*
* @throws Exception
*/
public function done(Request | array | null $request = null): PaymentInterface
{
$token = $this->getHttpRequestVerifier()->verify($request ?: Request::createFromGlobals());
$gateway = $this->getGateway($token->getGatewayName());
$this->httpRequestVerifier->invalidate($token);
$gateway->execute($status = new GetHumanStatus($token));
return $status->getFirstModel();
}
/**
* @param Request|array<string, mixed>|null $request
*
* @throws Exception
*/
public function notify(Request | array | null $request = null): Response
{
$token = $this->httpRequestVerifier->verify($request ?: Request::createFromGlobals());
$gateway = $this->getGateway($token->getGatewayName());
if ($gateway instanceof Gateway && $gateway->supportsCommand(NotifyCommand::class)) {
return $this->acknowledge($gateway, $token);
}
try {
$gateway->execute(new Notify($token));
return new Response('', Response::HTTP_NO_CONTENT);
} catch (HttpResponse $reply) {
return new Response($reply->getContent(), $reply->getStatusCode(), $reply->getHeaders());
} catch (ReplyInterface $reply) {
throw new LogicException('Unsupported reply', $reply->getCode(), $reply);
}
}
/**
* Turns what a handler decided into an HTTP response.
*
* A null next action means there is nothing left for the customer to do, so the application takes
* over at the token's after URL.
*/
private function respondTo(Result $result, TokenInterface $token): Response
{
$next = $result->next;
if (! $next instanceof NextAction) {
return new RedirectResponse($token->getAfterUrl());
}
if ($next instanceof Redirect) {
return new RedirectResponse($next->url, $next->statusCode, $next->headers);
}
if ($next instanceof PostRedirect) {
$reply = new HttpPostRedirect($next->url, $next->fields);
return new Response($reply->getContent(), $reply->getStatusCode(), $reply->getHeaders());
}
if ($next instanceof RenderTemplate) {
return new Response($this->renderer()->render($next->template, $next->context));
}
throw new LogicException(sprintf(
'%s cannot turn %s into a response; it handles %s, %s and %s. Execute the command yourself and act on the result.',
__METHOD__,
$next::class,
Redirect::class,
PostRedirect::class,
RenderTemplate::class,
));
}
/**
* Runs a webhook and turns the handler's answer into the response the PSP is waiting for.
*
* A handler that named no acknowledgement gets 204, which every PSP accepts. A message that failed
* verification gets 400 with nothing in it; the exception carries the reason, for the application to
* log.
*/
private function acknowledge(Gateway $gateway, TokenInterface $token): Response
{
try {
$result = $gateway->execute(NotifyCommand::forToken($token));
} catch (WebhookNotVerifiedException) {
return new Response('', Response::HTTP_BAD_REQUEST);
}
$acknowledgement = $result instanceof NotifyResult && $result->acknowledgement instanceof Acknowledgement
? $result->acknowledgement
: Acknowledgement::noContent();
return new Response($acknowledgement->body, $acknowledgement->status, $acknowledgement->headers);
}
}