/
CaptureAction.php
108 lines (88 loc) · 4.18 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace SyliusMolliePlugin\Action;
use SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
use SyliusMolliePlugin\Payments\PaymentTerms\Options;
use SyliusMolliePlugin\Request\Api\CreateCustomer;
use SyliusMolliePlugin\Request\Api\CreateInternalRecurring;
use SyliusMolliePlugin\Request\Api\CreateOnDemandSubscription;
use SyliusMolliePlugin\Request\Api\CreateOnDemandSubscriptionPayment;
use SyliusMolliePlugin\Request\Api\CreateOrder;
use SyliusMolliePlugin\Request\Api\CreatePayment;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\RuntimeException;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Capture;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use Psr\Log\InvalidArgumentException;
final class CaptureAction extends BaseApiAwareAction implements CaptureActionInterface
{
use GatewayAwareTrait;
/** @var GenericTokenFactoryInterface|null */
private $tokenFactory;
public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
{
$this->tokenFactory = $genericTokenFactory;
}
/** @param Capture|mixed $request */
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
$details = ArrayObject::ensureArrayObject($request->getModel());
if (true === isset($details['payment_mollie_id']) ||
true === isset($details['subscription_mollie_id']) ||
true === isset($details['order_mollie_id'])) {
return;
}
/** @var TokenInterface $token */
$token = $request->getToken();
if (null === $this->tokenFactory) {
throw new RuntimeException();
}
$notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());
$refundToken = $this->tokenFactory->createRefundToken($token->getGatewayName(), $token->getDetails());
$details['webhookUrl'] = $notifyToken->getTargetUrl();
$details['backurl'] = $token->getTargetUrl();
$metadata = $details['metadata'];
$metadata['refund_token'] = $refundToken->getHash();
$details['metadata'] = $metadata;
if (true === $this->mollieApiClient->isRecurringSubscription()) {
if ('first' === $details['sequenceType']) {
$cancelToken = $this->tokenFactory->createToken(
$token->getGatewayName(),
$token->getDetails(),
'sylius_mollie_plugin_cancel_subscription_mollie',
['orderId' => $details['metadata']['order_id']]
);
$details['cancel_token'] = $cancelToken->getHash();
$this->gateway->execute(new CreateCustomer($details));
$this->gateway->execute(new CreateInternalRecurring($details));
$this->gateway->execute(new CreateOnDemandSubscription($details));
} elseif ('recurring' === $details['sequenceType']) {
$this->gateway->execute(new CreateOnDemandSubscriptionPayment($details));
}
} else {
if (isset($details['metadata']['methodType']) && Options::PAYMENT_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], Options::getOnlyOrderAPIMethods(), true)) {
throw new InvalidArgumentException(sprintf(
'Method %s is not allowed to use %s',
$details['metadata']['molliePaymentMethods'],
Options::PAYMENT_API
));
}
$this->gateway->execute(new CreatePayment($details));
}
if (isset($details['metadata']['methodType']) && Options::ORDER_API === $details['metadata']['methodType']) {
$this->gateway->execute(new CreateOrder($details));
}
}
}
public function supports($request): bool
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess;
}
}