forked from CommerceWeavers/SyliusSaferpayPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptureAction.php
86 lines (71 loc) · 3.7 KB
/
CaptureAction.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
<?php
declare(strict_types=1);
namespace CommerceWeavers\SyliusSaferpayPlugin\Payum\Action;
use CommerceWeavers\SyliusSaferpayPlugin\Client\SaferpayClientInterface;
use CommerceWeavers\SyliusSaferpayPlugin\Client\ValueObject\CaptureResponse;
use CommerceWeavers\SyliusSaferpayPlugin\Client\ValueObject\ErrorResponse;
use CommerceWeavers\SyliusSaferpayPlugin\Payum\Status\StatusCheckerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\Capture;
use Psr\Log\LoggerInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\HttpFoundation\Response;
final class CaptureAction implements ActionInterface
{
public function __construct(
private SaferpayClientInterface $saferpayClient,
private StatusCheckerInterface $statusChecker,
private LoggerInterface $logger,
private EntityManagerInterface $entityManager
) {
}
/** @param Capture $request */
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
/** @var PaymentInterface $payment */
$payment = $request->getModel();
$this->logger->debug('CaptureAction: Capture started for order: ' . $payment->getId());
if ($this->statusChecker->isCaptured($payment)) {
$this->logger->debug('CaptureAction: already captured for order: ' . $payment->getId());
return;
}
/** @var CaptureResponse|ErrorResponse $response */
$response = $this->saferpayClient->capture($payment);
if ($response instanceof ErrorResponse) {
if ($response->getName() === 'TRANSACTION_ALREADY_CAPTURED') {
$this->entityManager->refresh($payment);
if ($payment->getState() === PaymentInterface::STATE_COMPLETED) {
$this->logger->debug('Capture failed for payment: ' . $payment->getId() . ' already captured and state complete', ['details' => $payment->getDetails()]);
return;
}
if ($payment->getState() === PaymentInterface::STATE_AUTHORIZED) {
$this->logger->debug('Capture failed for payment: ' . $payment->getId() . ' already captured and state authorized', ['details' => $payment->getDetails()]);
$payment->setDetails(array_merge($payment->getDetails(), [
'status' => StatusAction::STATUS_CAPTURED,
'transaction_id' => $response->getTransactionId(),
]));
return;
}
$this->logger->debug('Capture failed for payment: ' . $payment->getId() . ' already captured', ['state' => $payment->getState(), 'details' => $payment->getDetails()]);
}
$this->logger->debug('Capture failed for payment: ' . $payment->getId(), ['response' => $response->toArray()]);
$payment->setDetails(array_merge($payment->getDetails(), [
'status' => StatusAction::STATUS_FAILED,
'transaction_id' => $response->getTransactionId(),
]));
return;
}
$paymentDetails = $payment->getDetails();
$isSuccessfulResponse = $response->getStatusCode() === Response::HTTP_OK;
$paymentDetails['status'] = $isSuccessfulResponse ? $response->getStatus() : StatusAction::STATUS_FAILED;
$paymentDetails['capture_id'] = $response->getCaptureId();
$payment->setDetails($paymentDetails);
}
public function supports($request): bool
{
return $request instanceof Capture && $request->getFirstModel() instanceof PaymentInterface;
}
}