|
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\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Checker\Voucher\ProductVoucherTypeCheckerInterface; |
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface; |
|
16
|
|
|
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig; |
|
17
|
|
|
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface; |
|
18
|
|
|
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory; |
|
19
|
|
|
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface; |
|
20
|
|
|
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface; |
|
21
|
|
|
use BitBag\SyliusMolliePlugin\Resolver\Order\PaymentCheckoutOrderResolverInterface; |
|
22
|
|
|
use Mollie\Api\Exceptions\ApiException; |
|
23
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
24
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class MolliePaymentsMethodResolver implements MolliePaymentsMethodResolverInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var RepositoryInterface */ |
|
29
|
|
|
private $mollieGatewayRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** @var MollieCountriesRestrictionResolverInterface */ |
|
32
|
|
|
private $countriesRestrictionResolver; |
|
33
|
|
|
|
|
34
|
|
|
/** @var ProductVoucherTypeCheckerInterface */ |
|
35
|
|
|
private $productVoucherTypeChecker; |
|
36
|
|
|
|
|
37
|
|
|
/** @var PaymentCheckoutOrderResolverInterface */ |
|
38
|
|
|
private $paymentCheckoutOrderResolver; |
|
39
|
|
|
|
|
40
|
|
|
/** @var PaymentMethodRepositoryInterface */ |
|
41
|
|
|
private $paymentMethodRepository; |
|
42
|
|
|
|
|
43
|
|
|
/** @var MollieAllowedMethodsResolver */ |
|
44
|
|
|
private $allowedMethodsResolver; |
|
45
|
|
|
|
|
46
|
|
|
/** @var MollieLoggerActionInterface */ |
|
47
|
|
|
private $loggerAction; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
RepositoryInterface $mollieGatewayRepository, |
|
52
|
|
|
MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver, |
|
53
|
|
|
ProductVoucherTypeCheckerInterface $productVoucherTypeChecker, |
|
54
|
|
|
PaymentCheckoutOrderResolverInterface $paymentCheckoutOrderResolver, |
|
55
|
|
|
PaymentMethodRepositoryInterface $paymentMethodRepository, |
|
56
|
|
|
MollieAllowedMethodsResolver $allowedMethodsResolver, |
|
57
|
|
|
MollieLoggerActionInterface $loggerAction |
|
58
|
|
|
) { |
|
59
|
|
|
$this->mollieGatewayRepository = $mollieGatewayRepository; |
|
60
|
|
|
$this->countriesRestrictionResolver = $countriesRestrictionResolver; |
|
61
|
|
|
$this->productVoucherTypeChecker = $productVoucherTypeChecker; |
|
62
|
|
|
$this->paymentCheckoutOrderResolver = $paymentCheckoutOrderResolver; |
|
63
|
|
|
$this->paymentMethodRepository = $paymentMethodRepository; |
|
64
|
|
|
$this->allowedMethodsResolver = $allowedMethodsResolver; |
|
65
|
|
|
$this->loggerAction = $loggerAction; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function resolve(): array |
|
69
|
|
|
{ |
|
70
|
|
|
$order = $this->paymentCheckoutOrderResolver->resolve(); |
|
71
|
|
|
|
|
72
|
|
|
/** @var OrderInterface $order */ |
|
73
|
|
|
$address = $order->getBillingAddress(); |
|
74
|
|
|
|
|
75
|
|
|
if (null === $address) { |
|
76
|
|
|
$address = $order->getShippingAddress(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (null === $address) { |
|
80
|
|
|
return $this->getDefaultOptions(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->getMolliePaymentOptions($order, $address->getCountryCode()); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function getMolliePaymentOptions(OrderInterface $order, string $countryCode): array |
|
87
|
|
|
{ |
|
88
|
|
|
$allowedMethods = []; |
|
89
|
|
|
|
|
90
|
|
|
$methods = $this->getDefaultOptions(); |
|
91
|
|
|
|
|
92
|
|
|
/** @var GatewayConfigInterface $gateway */ |
|
93
|
|
|
$paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName( |
|
94
|
|
|
$order->getChannel(), |
|
|
|
|
|
|
95
|
|
|
MollieGatewayFactory::FACTORY_NAME |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
if (null === $paymentMethod) { |
|
99
|
|
|
return $this->getDefaultOptions(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$gateway = $paymentMethod->getGatewayConfig(); |
|
103
|
|
|
|
|
104
|
|
|
if (null === $gateway) { |
|
105
|
|
|
return $this->getDefaultOptions(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if (empty($paymentConfigs)) { |
|
111
|
|
|
return $this->getDefaultOptions(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
try { |
|
115
|
|
|
$allowedMethodsIds = $this->allowedMethodsResolver->resolve($order); |
|
116
|
|
|
} catch (ApiException $e) { |
|
117
|
|
|
$this->loggerAction->addNegativeLog($e->getMessage()); |
|
118
|
|
|
|
|
119
|
|
|
return $this->getDefaultOptions(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** @var MollieGatewayConfig $paymentMethod */ |
|
123
|
|
|
foreach ($paymentConfigs as $paymentMethod) { |
|
124
|
|
|
if (in_array($paymentMethod->getMethodId(), $allowedMethodsIds, true)) { |
|
125
|
|
|
$allowedMethods[] = $paymentMethod; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (empty($allowedMethods)) { |
|
130
|
|
|
return $this->getDefaultOptions(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** @var MollieGatewayConfigInterface $paymentMethod */ |
|
134
|
|
|
foreach ($allowedMethods as $paymentMethod) { |
|
135
|
|
|
$methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$methods = $this->productVoucherTypeChecker->checkTheProductTypeOnCart($order, $methods); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
return $methods; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function getDefaultOptions(): array |
|
144
|
|
|
{ |
|
145
|
|
|
return [ |
|
146
|
|
|
'data' => [], |
|
147
|
|
|
'image' => [], |
|
148
|
|
|
'issuers' => [], |
|
149
|
|
|
'paymentFee' => [], |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|