-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAbortPaymentProcessor.php
More file actions
57 lines (48 loc) · 1.95 KB
/
Copy pathAbortPaymentProcessor.php
File metadata and controls
57 lines (48 loc) · 1.95 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
<?php
declare(strict_types=1);
namespace PayPlug\SyliusPayPlugPlugin\PaymentProcessing;
use Payplug\Exception\HttpException;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\Workflow\Attribute\AsCompletedListener;
use Symfony\Component\Workflow\Event\CompletedEvent;
#[Autoconfigure(public: true)]
class AbortPaymentProcessor
{
public function __construct(
private PayPlugApiClientFactoryInterface $payplugApiClientFactory,
) {
}
#[AsCompletedListener(workflow: PaymentTransitions::GRAPH, transition: PaymentTransitions::TRANSITION_FAIL)]
public function onFailedCompleteTransitionEvent(CompletedEvent $event): void
{
$subject = $event->getSubject();
if (!$subject instanceof PaymentInterface) {
return;
}
$this->process($subject);
}
public function process(PaymentInterface $payment): void
{
$paymentId = $payment->getDetails()['payment_id'] ?? null;
if (null === $paymentId) {
// Payment not even started on payplug
return;
}
$method = $payment->getMethod();
if (null === $method) {
return;
}
$client = $this->payplugApiClientFactory->createForPaymentMethod($method);
try {
// When a payment is failed on Sylius, also abort it on PayPlug.
// This should prevent the case that if we are already on PayPlug payment page
// and go to the order history in another tab to click on pay again, then fail the transaction
// and go back on the first PayPlug payment page and succeed it, it stays failed as its first payment model is already failed
$client->abortPayment($paymentId);
} catch (HttpException) {
}
}
}