-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCoreGatewayFactory.php
More file actions
517 lines (435 loc) · 22.6 KB
/
Copy pathCoreGatewayFactory.php
File metadata and controls
517 lines (435 loc) · 22.6 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
namespace Payum\Core;
use Closure;
use DI\ContainerBuilder;
use Exception;
use GuzzleHttp\Psr7\Request;
use Http\Adapter\Buzz\Client as HttpBuzzClient;
use Http\Adapter\Guzzle5\Client as HttpGuzzle5Client;
use Http\Adapter\Guzzle6\Client as HttpGuzzle6Client;
use Http\Adapter\Guzzle7\Client as HttpGuzzle7Client;
use Http\Client\Curl\Client as HttpCurlClient;
use Http\Client\HttpClient;
use Http\Client\Socket\Client as HttpSocketClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\MessageFactory;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Message\StreamFactory\GuzzleStreamFactory;
use LogicException;
use Nyholm\Psr7\Factory\HttplugFactory;
use Payum\Core\Action\AuthorizePaymentAction;
use Payum\Core\Action\CapturePaymentAction;
use Payum\Core\Action\ExecuteSameRequestWithModelDetailsAction;
use Payum\Core\Action\GetCurrencyAction;
use Payum\Core\Action\GetTokenAction;
use Payum\Core\Action\PayoutPayoutAction;
use Payum\Core\Action\PrependActionInterface;
use Payum\Core\Bridge\Httplug\HttplugClient;
use Payum\Core\Bridge\PlainPhp\Action\GetHttpRequestAction;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Bridge\Twig\Action\RenderTemplateAction;
use Payum\Core\Bridge\Twig\TwigUtil;
use Payum\Core\DI\ContainerConfiguration;
use Payum\Core\DI\CreatesGateway;
use Payum\Core\Extension\EndlessCycleDetectorExtension;
use Payum\Core\Extension\PrependExtensionInterface;
use Payum\Core\Gateway\DeclaresActions;
use Payum\Core\Gateway\GatewayInterface as PaymentGateway;
use Payum\Core\Handler\HandlerMap;
use Payum\Core\Middleware\EndlessCycleDetectorMiddleware;
use Payum\Core\Middleware\LegacyExtensionMiddleware;
use Payum\Core\Middleware\MiddlewareCollection;
use Payum\Core\Middleware\PersistStateMiddleware;
use Payum\Core\Middleware\RecordPaymentStatusMiddleware;
use Payum\Core\Registry\StorageRegistryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionType;
use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplugClient;
use Twig\Environment;
use Twig\Loader\ChainLoader;
use function array_combine;
use function array_map;
use function array_merge;
use function class_exists;
use function func_get_args;
use function func_num_args;
use function in_array;
use function is_string;
use function str_starts_with;
use function trigger_deprecation;
use function trigger_error;
use const E_USER_DEPRECATED;
class CoreGatewayFactory implements GatewayFactoryInterface, ContainerConfiguration, CreatesGateway, GatewayFactoryConfigInterface
{
/**
* @var array<string, mixed>
*/
protected array $defaultConfig = [];
/**
* @param array<string, mixed> $defaultConfig
*/
public function __construct(array $defaultConfig = [])
{
$this->defaultConfig = $defaultConfig;
}
/**
* @param array<string, mixed> $config
*
* @throws ContainerExceptionInterface|NotFoundExceptionInterface|Exception
*/
public function create(array $config = []): Gateway
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions($config);
$containerBuilder->addDefinitions($this->configureContainer());
$containerBuilder->addDefinitions([
ArrayObject::class => static function (ContainerInterface $container) {
trigger_deprecation('payum/core', '2.0.0', 'The %s service is deprecated and will be removed in 3.0. Use the %s service instead.', ArrayObject::class, ContainerInterface::class);
return new class($container) extends ArrayObject {
private ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->container = $container;
}
public function offsetGet($key): mixed
{
return $this->container->get($key);
}
public function offsetExists($key): bool
{
return $this->container->has($key);
}
};
},
]);
$container = $containerBuilder->build();
$entries = $container->getKnownEntryNames();
$configArray = ArrayObject::ensureArrayObject($config);
$configArray->defaults(array_combine(
$entries,
array_map(static fn (string $name) => static fn () => $container->get($name), $entries)
));
$gateway = new Gateway();
$this->buildClosures($configArray);
$this->buildActions($gateway, $configArray);
$this->buildApis($gateway, $configArray);
$this->buildExtensions($gateway, $configArray);
return $this->createGateway($container, $gateway);
}
/**
* @param array<string, mixed> $config
*
* @return array<string, mixed>
*/
public function createConfig(array $config = []): array
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
$containerConfig = $this->configureContainer();
$result = array_merge($containerConfig, $config);
if (isset($config['payum.paths'])) {
$result['payum.paths'] = array_merge(
$containerConfig['payum.paths'] ?? [],
$config['payum.paths']
);
}
return $result;
}
public function configureContainer(): array
{
return array_merge(
[
'httplug.message_factory' => static function (): MessageFactory {
@trigger_error('Using "httplug.message_factory" is deprecated, use "payum.http_message_factory" instead, which will return a PSR-17 RequestFactoryInterface since payum/core 2.0.0', E_USER_DEPRECATED);
if (class_exists(MessageFactoryDiscovery::class)) {
return MessageFactoryDiscovery::find();
}
if (class_exists(Request::class)) {
return new GuzzleMessageFactory();
}
if (class_exists(\Laminas\Diactoros\Request::class)) {
return new DiactorosMessageFactory();
}
if (class_exists(\Nyholm\Psr7\Request::class)) {
return new HttplugFactory();
}
throw new LogicException('The httplug.message_factory could not be guessed. Install one of the following packages: php-http/guzzle7-adapter. You can also overwrite the config option with your implementation.');
},
'httplug.stream_factory' => static function () {
@trigger_error('Using "httplug.stream_factory" is deprecated, use "payum.http_stream_factory" instead which will return a PSR-17 StreamFactoryInterface since payum/core 2.0.0', E_USER_DEPRECATED);
if (class_exists(StreamFactoryDiscovery::class)) {
return StreamFactoryDiscovery::find();
}
if (class_exists(Request::class)) {
return new GuzzleStreamFactory();
}
if (class_exists(\Nyholm\Psr7\Request::class)) {
return new HttplugFactory();
}
throw new LogicException('The httplug.stream_factory could not be guessed. Install one of the following packages: php-http/guzzle7-adapter. You can also overwrite the config option with your implementation.');
},
'httplug.client' => static function (ArrayObject $config) {
@trigger_error('Using "httplug.client" is deprecated, use "payum.http_client" instead which will return a PSR-18 ClientInterface since payum/core 2.0.0', E_USER_DEPRECATED);
if (class_exists(HttpClientDiscovery::class)) {
return HttpClientDiscovery::find();
}
if (class_exists(HttpGuzzle7Client::class)) {
return new HttpGuzzle7Client();
}
if (class_exists(HttpGuzzle6Client::class)) {
return new HttpGuzzle6Client();
}
if (class_exists(HttpGuzzle5Client::class)) {
return new HttpGuzzle5Client();
}
if (class_exists(SymfonyHttplugClient::class)) {
return new SymfonyHttplugClient();
}
if (class_exists(HttpSocketClient::class)) {
return new HttpSocketClient();
}
if (class_exists(HttpCurlClient::class)) {
return new HttpCurlClient($config['httplug.message_factory'], $config['httplug.stream_factory']);
}
if (class_exists(HttpBuzzClient::class)) {
return new HttpBuzzClient();
}
throw new LogicException('The httplug.client could not be guessed. Install one of the following packages: php-http/guzzle7-adapter, php-http/guzzle7-adapter. You can also overwrite the config option with your implementation.');
},
// PSR-18/17 services - lazily discover if not provided
ClientInterface::class => Psr18ClientDiscovery::find(...),
StreamFactoryInterface::class => Psr17FactoryDiscovery::findStreamFactory(...),
RequestFactoryInterface::class => Psr17FactoryDiscovery::findRequestFactory(...),
ServerRequestFactoryInterface::class => Psr17FactoryDiscovery::findServerRequestFactory(...),
// Middleware wrapping command execution. The collection is what decides the order;
// PayumBuilder replaces it with the defaults plus whatever the application and the
// gateway registered.
MiddlewareCollection::class => self::defaultMiddleware(...),
EndlessCycleDetectorMiddleware::class => static fn (): EndlessCycleDetectorMiddleware => new EndlessCycleDetectorMiddleware(),
LegacyExtensionMiddleware::class => static fn (): LegacyExtensionMiddleware => new LegacyExtensionMiddleware(),
RecordPaymentStatusMiddleware::class => static fn (): RecordPaymentStatusMiddleware => new RecordPaymentStatusMiddleware(),
PersistStateMiddleware::class => static fn (ContainerInterface $c): PersistStateMiddleware => new PersistStateMiddleware(
$c->has(StorageRegistryInterface::class) ? $c->get(StorageRegistryInterface::class) : null,
),
ServerRequestInterface::class => static fn (ContainerInterface $c): ServerRequestInterface => $c
->get(ServerRequestFactoryInterface::class)
->createServerRequest($_SERVER['REQUEST_METHOD'] ?? 'GET', $_SERVER['REQUEST_URI'] ?? '/', $_SERVER)
->withQueryParams($_GET)
->withParsedBody($_POST)
->withCookieParams($_COOKIE),
// Legacy Payum service names - delegate to PSR interfaces
'payum.http_client' => static fn (ContainerInterface $c): HttplugClient => new HttplugClient($c->get(ClientInterface::class)),
'payum.http_stream_factory' => static fn (ContainerInterface $c): StreamFactoryInterface => $c->get(StreamFactoryInterface::class),
'payum.http_message_factory' => static fn (ContainerInterface $c): RequestFactoryInterface => $c->get(RequestFactoryInterface::class),
'payum.template.layout' => '@PayumCore/layout.html.twig',
'twig.env' => static fn () => new Environment(new ChainLoader()),
'payum.default_options' => [],
'payum.required_options' => [],
// Backwards compatibility arrays for deprecated build* methods
'payum.prepend_actions' => [],
'payum.prepend_apis' => [],
'payum.prepend_extensions' => [],
'payum.api.http_client' => static fn (ContainerInterface $c) => $c->get('payum.http_client'),
// Token storage - should be provided externally
// 'payum.security.token_storage' => null,
'payum.paths' => [
'PayumCore' => __DIR__ . '/Resources/views',
],
// Additional aliases
ResponseFactoryInterface::class => static fn (ContainerInterface $c): RequestFactoryInterface => $c->get(RequestFactoryInterface::class),
Environment::class => static fn (ContainerInterface $c) => $c->get('twig.env'),
HttpClient::class => static fn (ContainerInterface $c) => $c->get('payum.http_client'),
// BC: Deprecated action entries (used by deprecated buildActions method)
'payum.action.get_http_request' => static fn () => new GetHttpRequestAction(),
'payum.action.capture_payment' => static fn () => new CapturePaymentAction(),
'payum.action.authorize_payment' => static fn () => new AuthorizePaymentAction(),
'payum.action.payout_payout' => static fn () => new PayoutPayoutAction(),
'payum.action.execute_same_request_with_model_details' => static fn () => new ExecuteSameRequestWithModelDetailsAction(),
'payum.action.get_currency' => static fn () => new GetCurrencyAction(),
'payum.action.render_template' => static fn (ContainerInterface $c) => new RenderTemplateAction($c->get('twig.env'), $c->get('payum.template.layout')),
'payum.action.get_token' => static fn (ContainerInterface $c) => $c->has('payum.security.token_storage')
? new GetTokenAction($c->get('payum.security.token_storage'))
: null,
// BC: Deprecated extension entries (used by deprecated buildExtensions method)
'payum.extension.endless_cycle_detector' => static fn () => new EndlessCycleDetectorExtension(),
// New DI approach: Action classes for createGateway()
RenderTemplateAction::class => static fn (ContainerInterface $c) => new RenderTemplateAction($c->get('twig.env'), $c->get('payum.template.layout')),
// GetTokenAction is conditionally added in createGateway() if token storage is available
GetTokenAction::class => static fn (ContainerInterface $c) => $c->has('payum.security.token_storage')
? new GetTokenAction($c->get('payum.security.token_storage'))
: throw new LogicException('Token storage must be configured to use GetTokenAction'),
],
$this->defaultConfig,
);
}
/**
* @throws ContainerExceptionInterface|NotFoundExceptionInterface|LoaderError
*/
/**
* The middleware every gateway gets, before anything the application or the gateway adds.
*/
public static function defaultMiddleware(): MiddlewareCollection
{
return (new MiddlewareCollection())
->with(EndlessCycleDetectorMiddleware::class)
->with(LegacyExtensionMiddleware::class)
->with(PersistStateMiddleware::class)
->with(RecordPaymentStatusMiddleware::class);
}
public function createGateway(ContainerInterface $container): Gateway
{
if (2 === func_num_args() && func_get_args()[1] instanceof Gateway) {
$gateway = func_get_args()[1];
} else {
$gateway = new Gateway();
}
$gateway->setContainer($container);
$declaredActions = [];
if ($container->has(PaymentGateway::class)) {
$paymentGateway = $container->get(PaymentGateway::class);
$gateway->setHandlerMap(HandlerMap::fromHandlers($paymentGateway->handlers()));
// A gateway that has finished moving gets nothing else: no actions, no extensions, no Twig
// environment it will never render with. One still holding actions says so, and then needs
// core's own alongside them, since an action dispatching GetHttpRequest expects an answer.
if (! $paymentGateway instanceof DeclaresActions) {
return $gateway;
}
$declaredActions = $paymentGateway->actions();
}
if ($container->has('twig.env')) {
TwigUtil::registerPaths(
$container->get('twig.env'),
array_merge(
[
'PayumCore' => __DIR__ . '/Resources/views',
],
(array) $container->get('payum.paths'),
)
);
}
foreach ([...$this->getActions(), ...$declaredActions] as $action) {
// Skip GetTokenAction if token storage is not configured
if (GetTokenAction::class === $action && ! $container->has('payum.security.token_storage')) {
continue;
}
$action = $container->get($action);
$gateway->addAction($action, $action instanceof PrependActionInterface);
}
foreach ($this->getExtensions() as $extension) {
if (is_string($extension)) {
$extension = $container->get($extension);
}
$gateway->addExtension($extension, $extension instanceof PrependExtensionInterface);
}
return $gateway;
}
public function getActions(): array
{
return [
GetHttpRequestAction::class,
CapturePaymentAction::class,
AuthorizePaymentAction::class,
PayoutPayoutAction::class,
ExecuteSameRequestWithModelDetailsAction::class,
RenderTemplateAction::class,
GetCurrencyAction::class,
GetTokenAction::class,
];
}
public function getExtensions(): array
{
return [
EndlessCycleDetectorExtension::class,
];
}
/**
* @deprecated since 2.0. Implement the ContainerConfiguration interface instead.
*/
protected function buildClosures(ArrayObject $config): void
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
// Helper to check if closure expects ArrayObject
$canInvokeWithArrayObject = static function ($value): bool {
if (! $value instanceof Closure) {
return false;
}
$reflection = new ReflectionFunction($value);
if (0 === $reflection->getNumberOfParameters()) {
return true;
}
$params = $reflection->getParameters();
$firstParam = $params[0];
return ! $firstParam->getType() instanceof ReflectionType || ($firstParam->getType() instanceof ReflectionNamedType && ArrayObject::class === $firstParam->getType()->getName());
};
/** @var ContainerInterface $container */
$container = $config[ContainerInterface::class]();
foreach ($config as $name => $value) {
if (GetTokenAction::class === $name && ! $container->has('payum.security.token_storage')) {
continue;
}
if (is_callable($value) && ! (is_string($value) && function_exists('\\' . $value))) {
if ($canInvokeWithArrayObject($value)) {
$config[$name] = $value($config);
} else {
$config[$name] = $container->get($name);
}
}
}
}
/**
* @deprecated since 2.0. Implement the ContainerConfiguration interface instead.
*/
protected function buildActions(Gateway $gateway, ArrayObject $config): void
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.action') && null !== $value) {
$prepend = in_array($name, $config['payum.prepend_actions'], true) || $value instanceof PrependActionInterface;
$gateway->addAction($value, $prepend);
}
}
}
/**
* @deprecated since 2.0. Implement the ContainerConfiguration interface instead.
*/
protected function buildApis(Gateway $gateway, ArrayObject $config): void
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.api')) {
@trigger_error('The payum.api.* config is deprecated and will be removed in 3.0. Use dependency-injection to inject the api into the action instead.', E_USER_DEPRECATED);
$prepend = in_array($name, $config['payum.prepend_apis'], true);
$gateway->addApi($value, $prepend);
}
}
}
/**
* @deprecated since 2.0. Implement the ContainerConfiguration interface instead.
*/
protected function buildExtensions(Gateway $gateway, ArrayObject $config): void
{
trigger_deprecation('payum/core', '2.0.0', 'The %s is deprecated. Implement the %s interface instead.', __METHOD__, ContainerConfiguration::class);
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.extension')) {
$prepend = in_array($name, $config['payum.prepend_extensions'], true) || $value instanceof PrependExtensionInterface;
$gateway->addExtension($value, $prepend);
}
}
}
}