forked from Sylius/StripePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentStateProcessor.php
More file actions
84 lines (72 loc) · 3.13 KB
/
Copy pathPaymentStateProcessor.php
File metadata and controls
84 lines (72 loc) · 3.13 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
<?php
declare(strict_types=1);
namespace FluxSE\SyliusStripePlugin\StateMachine;
use Sylius\Bundle\PaymentBundle\Announcer\PaymentRequestAnnouncerInterface;
use Sylius\Bundle\PaymentBundle\Checker\FinalizedPaymentRequestCheckerInterface;
use Sylius\Bundle\PaymentBundle\Provider\GatewayFactoryNameProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\Factory\PaymentRequestFactoryInterface;
use Sylius\Component\Payment\Model\PaymentRequestInterface;
use Sylius\Component\Payment\Repository\PaymentRequestRepositoryInterface;
use Webmozart\Assert\Assert;
final readonly class PaymentStateProcessor implements PaymentStateProcessorInterface
{
/**
* @param PaymentRequestFactoryInterface<PaymentRequestInterface> $paymentRequestFactory
* @param PaymentRequestRepositoryInterface<PaymentRequestInterface> $paymentRequestRepository
* @param string[] $supportedFactories
* @param string[] $allowedPaymentFromStates
*/
public function __construct(
private GatewayFactoryNameProviderInterface $gatewayFactoryNameProvider,
private FinalizedPaymentRequestCheckerInterface $finalizedPaymentRequestChecker,
private PaymentRequestFactoryInterface $paymentRequestFactory,
private PaymentRequestRepositoryInterface $paymentRequestRepository,
private PaymentRequestAnnouncerInterface $paymentRequestAnnouncer,
private array $supportedFactories,
private array $allowedPaymentFromStates,
private string $requiredPaymentState,
private string $paymentRequestAction,
) {
}
public function __invoke(PaymentInterface $payment, string $fromState): void
{
if (
$this->allowedPaymentFromStates !== [] &&
false === in_array(
$fromState,
$this->allowedPaymentFromStates,
true,
)) {
return;
}
$paymentMethod = $payment->getMethod();
if (null === $paymentMethod) {
return;
}
$factoryName = $this->gatewayFactoryNameProvider->provide($paymentMethod);
if (false === in_array($factoryName, $this->supportedFactories, true)) {
return;
}
Assert::eq(
$payment->getState(),
$this->requiredPaymentState,
sprintf(
'The payment must have state "%s" at this point, found "%s".',
$this->requiredPaymentState,
$payment->getState(),
),
);
$paymentRequest = $this->paymentRequestRepository->findOneByActionPaymentAndMethod(
$this->paymentRequestAction,
$payment,
$paymentMethod,
);
if (null === $paymentRequest || $this->finalizedPaymentRequestChecker->isFinal($paymentRequest)) {
$paymentRequest = $this->paymentRequestFactory->create($payment, $paymentMethod);
$paymentRequest->setAction($this->paymentRequestAction);
$this->paymentRequestRepository->add($paymentRequest);
}
$this->paymentRequestAnnouncer->dispatchPaymentRequestCommand($paymentRequest);
}
}