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
|
|
|
public function __construct( |
50
|
|
|
RepositoryInterface $mollieGatewayRepository, |
51
|
|
|
MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver, |
52
|
|
|
ProductVoucherTypeCheckerInterface $productVoucherTypeChecker, |
53
|
|
|
PaymentCheckoutOrderResolverInterface $paymentCheckoutOrderResolver, |
54
|
|
|
PaymentMethodRepositoryInterface $paymentMethodRepository, |
55
|
|
|
MollieAllowedMethodsResolver $allowedMethodsResolver, |
56
|
|
|
MollieLoggerActionInterface $loggerAction |
57
|
|
|
) { |
58
|
|
|
$this->mollieGatewayRepository = $mollieGatewayRepository; |
59
|
|
|
$this->countriesRestrictionResolver = $countriesRestrictionResolver; |
60
|
|
|
$this->productVoucherTypeChecker = $productVoucherTypeChecker; |
61
|
|
|
$this->paymentCheckoutOrderResolver = $paymentCheckoutOrderResolver; |
62
|
|
|
$this->paymentMethodRepository = $paymentMethodRepository; |
63
|
|
|
$this->allowedMethodsResolver = $allowedMethodsResolver; |
64
|
|
|
$this->loggerAction = $loggerAction; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function resolve(): array |
68
|
|
|
{ |
69
|
|
|
$order = $this->paymentCheckoutOrderResolver->resolve(); |
70
|
|
|
|
71
|
|
|
/** @var OrderInterface $order */ |
72
|
|
|
$address = $order->getBillingAddress(); |
73
|
|
|
|
74
|
|
|
if (null === $address) { |
75
|
|
|
$address = $order->getShippingAddress(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (null === $address) { |
79
|
|
|
return $this->getDefaultOptions(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->getMolliePaymentOptions($order, $address->getCountryCode()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getMolliePaymentOptions(OrderInterface $order, string $countryCode): array |
86
|
|
|
{ |
87
|
|
|
$allowedMethods = []; |
88
|
|
|
|
89
|
|
|
$methods = $this->getDefaultOptions(); |
90
|
|
|
|
91
|
|
|
/** @var GatewayConfigInterface $gateway */ |
92
|
|
|
$paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName( |
93
|
|
|
$order->getChannel(), |
|
|
|
|
94
|
|
|
MollieGatewayFactory::FACTORY_NAME |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if (null === $paymentMethod) { |
98
|
|
|
return $this->getDefaultOptions(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$gateway = $paymentMethod->getGatewayConfig(); |
102
|
|
|
|
103
|
|
|
if (null === $gateway) { |
104
|
|
|
return $this->getDefaultOptions(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway); |
|
|
|
|
108
|
|
|
|
109
|
|
|
if (empty($paymentConfigs)) { |
110
|
|
|
return $this->getDefaultOptions(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$allowedMethodsIds = $this->allowedMethodsResolver->resolve($order); |
115
|
|
|
} catch (ApiException $e) { |
116
|
|
|
$this->loggerAction->addNegativeLog($e->getMessage()); |
117
|
|
|
|
118
|
|
|
return $this->getDefaultOptions(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @var MollieGatewayConfig $paymentMethod */ |
122
|
|
|
foreach ($paymentConfigs as $paymentMethod) { |
123
|
|
|
if (in_array($paymentMethod->getMethodId(), $allowedMethodsIds, true)) { |
124
|
|
|
$allowedMethods[] = $paymentMethod; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (empty($allowedMethods)) { |
129
|
|
|
return $this->getDefaultOptions(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @var MollieGatewayConfigInterface $paymentMethod */ |
133
|
|
|
foreach ($allowedMethods as $paymentMethod) { |
134
|
|
|
$methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$methods = $this->productVoucherTypeChecker->checkTheProductTypeOnCart($order, $methods); |
|
|
|
|
138
|
|
|
|
139
|
|
|
return $methods; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function getDefaultOptions(): array |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
'data' => [], |
146
|
|
|
'image' => [], |
147
|
|
|
'issuers' => [], |
148
|
|
|
'paymentFee' => [], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|