1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Action; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction; |
15
|
|
|
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactoryInterface; |
17
|
|
|
use BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface; |
18
|
|
|
use BitBag\SyliusMolliePlugin\Helper\PaymentDescriptionInterface; |
19
|
|
|
use BitBag\SyliusMolliePlugin\Payments\PaymentTerms\Options; |
20
|
|
|
use BitBag\SyliusMolliePlugin\Request\Api\CreateCustomer; |
21
|
|
|
use Mollie\Api\Types\PaymentMethod; |
22
|
|
|
use Payum\Core\Action\ActionInterface; |
23
|
|
|
use Payum\Core\ApiAwareInterface; |
24
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
25
|
|
|
use Payum\Core\GatewayAwareInterface; |
26
|
|
|
use Payum\Core\GatewayAwareTrait; |
27
|
|
|
use Payum\Core\Request\Convert; |
28
|
|
|
use Payum\Core\Request\GetCurrency; |
29
|
|
|
use Sylius\Bundle\CoreBundle\Context\CustomerContext; |
30
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
31
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
32
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
33
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
34
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
35
|
|
|
|
36
|
|
|
final class ConvertPaymentAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface |
37
|
|
|
{ |
38
|
|
|
use GatewayAwareTrait; |
39
|
|
|
|
40
|
|
|
/** @var PaymentDescriptionInterface */ |
41
|
|
|
private $paymentDescription; |
42
|
|
|
|
43
|
|
|
/** @var SessionInterface */ |
44
|
|
|
private $session; |
45
|
|
|
|
46
|
|
|
/** @var RepositoryInterface */ |
47
|
|
|
private $mollieMethodsRepository; |
48
|
|
|
|
49
|
|
|
/** @var ConvertOrderInterface */ |
50
|
|
|
private $orderConverter; |
51
|
|
|
|
52
|
|
|
/** @var CustomerContext */ |
53
|
|
|
private $customerContext; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
PaymentDescriptionInterface $paymentDescription, |
57
|
|
|
SessionInterface $session, |
58
|
|
|
RepositoryInterface $mollieMethodsRepository, |
59
|
|
|
ConvertOrderInterface $orderConverter, |
60
|
|
|
CustomerContext $customerContext |
61
|
|
|
) { |
62
|
|
|
$this->paymentDescription = $paymentDescription; |
63
|
|
|
$this->session = $session; |
64
|
|
|
$this->mollieMethodsRepository = $mollieMethodsRepository; |
65
|
|
|
$this->orderConverter = $orderConverter; |
66
|
|
|
$this->customerContext = $customerContext; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @param Convert $request */ |
70
|
|
|
public function execute($request): void |
71
|
|
|
{ |
72
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
73
|
|
|
|
74
|
|
|
/** @var PaymentInterface $payment */ |
75
|
|
|
$payment = $request->getSource(); |
76
|
|
|
|
77
|
|
|
/** @var OrderInterface $order */ |
78
|
|
|
$order = $payment->getOrder(); |
79
|
|
|
|
80
|
|
|
/** @var CustomerInterface $customer */ |
81
|
|
|
$customer = $order->getCustomer(); |
82
|
|
|
|
83
|
|
|
$this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode())); |
84
|
|
|
|
85
|
|
|
$divisor = 10 ** $currency->exp; |
86
|
|
|
|
87
|
|
|
$amount = number_format(abs($payment->getAmount() / $divisor), 2, '.', ''); |
88
|
|
|
|
89
|
|
|
$paymentOptions = $payment->getDetails(); |
90
|
|
|
|
91
|
|
|
if (isset($paymentOptions['metadata'])) { |
92
|
|
|
$paymentMethod = $paymentOptions['metadata']['molliePaymentMethods']; |
93
|
|
|
$cartToken = $paymentOptions['metadata']['cartToken']; |
94
|
|
|
$selectedIssuer = $paymentMethod === PaymentMethod::IDEAL ? $paymentOptions['metadata']['selected_issuer'] : null; |
95
|
|
|
} else { |
96
|
|
|
$paymentMethod = $paymentOptions['molliePaymentMethods']; |
97
|
|
|
$cartToken = $paymentOptions['cartToken']; |
98
|
|
|
$selectedIssuer = $paymentMethod === PaymentMethod::IDEAL ? $paymentOptions['issuers']['id'] : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @var MollieGatewayConfigInterface $method */ |
102
|
|
|
$method = $this->mollieMethodsRepository->findOneBy(['methodId' => $paymentMethod]); |
103
|
|
|
$gatewayConfig = $method->getGateway()->getConfig(); |
104
|
|
|
|
105
|
|
|
$details = [ |
106
|
|
|
'amount' => [ |
107
|
|
|
'value' => "$amount", |
108
|
|
|
'currency' => $currency->code, |
109
|
|
|
], |
110
|
|
|
'description' => $this->paymentDescription->getPaymentDescription($payment, $method, $order), |
111
|
|
|
'metadata' => [ |
112
|
|
|
'order_id' => $order->getId(), |
113
|
|
|
'customer_id' => $customer->getId() ?? null, |
114
|
|
|
'molliePaymentMethods' => $paymentMethod ?? null, |
115
|
|
|
'cartToken' => $cartToken ?? null, |
116
|
|
|
'selected_issuer' => $selectedIssuer ?? null, |
117
|
|
|
], |
118
|
|
|
'full_name' => $customer->getFullName() ?? null, |
119
|
|
|
'email' => $customer->getEmail() ?? null, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
if (null !== $this->customerContext->getCustomer() && true === $gatewayConfig['single_click_enabled']) { |
123
|
|
|
$this->gateway->execute($mollieCustomer = new CreateCustomer($details)); |
124
|
|
|
$model = $mollieCustomer->getModel(); |
125
|
|
|
$details['metadata']['customer_mollie_id'] = $model['customer_mollie_id']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (true === $this->mollieApiClient->isRecurringSubscription()) { |
129
|
|
|
$config = $this->mollieApiClient->getConfig(); |
130
|
|
|
|
131
|
|
|
$details['times'] = $config['times']; |
132
|
|
|
$details['interval'] = $config['interval']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (false === $this->mollieApiClient->isRecurringSubscription()) { |
136
|
|
|
$details['customerId'] = $model['customer_mollie_id'] ?? null; |
137
|
|
|
$details['metadata']['methodType'] = Options::PAYMENT_API; |
138
|
|
|
$details['locale'] = true === in_array($order->getLocaleCode(), MollieGatewayFactoryInterface::LOCALES_AVAILABLE) ? $order->getLocaleCode() : 'en_US'; |
139
|
|
|
|
140
|
|
|
if (array_search($method->getPaymentType(), Options::getAvailablePaymentType()) === Options::ORDER_API) { |
141
|
|
|
unset($details['customerId']); |
142
|
|
|
|
143
|
|
|
$details['metadata']['methodType'] = Options::ORDER_API; |
144
|
|
|
$details = $this->orderConverter->convert($order, $details, $divisor, $method); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$request->setResult($details); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function supports($request): bool |
152
|
|
|
{ |
153
|
|
|
return |
154
|
|
|
$request instanceof Convert && |
155
|
|
|
$request->getSource() instanceof PaymentInterface && |
156
|
|
|
$request->getTo() === 'array' |
157
|
|
|
; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|