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
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusMolliePlugin\Resolver; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface; |
17
|
|
|
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory; |
18
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
19
|
|
|
use Sylius\Component\Order\Context\CartContextInterface; |
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
22
|
|
|
|
23
|
|
|
final class MolliePaymentsMethodResolver implements MolliePaymentsMethodResolverInterface |
24
|
|
|
{ |
25
|
|
|
/** @var RepositoryInterface */ |
26
|
|
|
private $orderRepository; |
27
|
|
|
|
28
|
|
|
/** @var RepositoryInterface */ |
29
|
|
|
private $gatewayConfigRepository; |
30
|
|
|
|
31
|
|
|
/** @var RepositoryInterface */ |
32
|
|
|
private $mollieGatewayRepository; |
33
|
|
|
|
34
|
|
|
/** @var Session */ |
35
|
|
|
private $session; |
36
|
|
|
|
37
|
|
|
/** @var CartContextInterface */ |
38
|
|
|
private $cartContext; |
39
|
|
|
|
40
|
|
|
/** @var MollieCountriesRestrictionResolverInterface */ |
41
|
|
|
private $countriesRestrictionResolver; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
RepositoryInterface $orderRepository, |
45
|
|
|
RepositoryInterface $gatewayConfigRepository, |
46
|
|
|
RepositoryInterface $mollieGatewayRepository, |
47
|
|
|
Session $session, |
48
|
|
|
CartContextInterface $cartContext, |
49
|
|
|
MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver |
50
|
|
|
) { |
51
|
|
|
$this->orderRepository = $orderRepository; |
52
|
|
|
$this->gatewayConfigRepository = $gatewayConfigRepository; |
53
|
|
|
$this->mollieGatewayRepository = $mollieGatewayRepository; |
54
|
|
|
$this->session = $session; |
55
|
|
|
$this->cartContext = $cartContext; |
56
|
|
|
$this->countriesRestrictionResolver = $countriesRestrictionResolver; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function resolve(): array |
60
|
|
|
{ |
61
|
|
|
$orderId = $this->session->get('sylius_order_id'); |
62
|
|
|
$orderFromSession = null; |
63
|
|
|
|
64
|
|
|
$order = $this->cartContext->getCart(); |
65
|
|
|
|
66
|
|
|
if (null !== $orderId) { |
67
|
|
|
$orderFromSession = $this->orderRepository->findOneBy(['id' => $orderId]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (null !== $orderFromSession) { |
71
|
|
|
$order = $orderFromSession; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var OrderInterface $order */ |
75
|
|
|
$address = $order->getBillingAddress(); |
76
|
|
|
|
77
|
|
|
if (null === $address) { |
78
|
|
|
$address = $order->getShippingAddress(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (null === $address) { |
82
|
|
|
return $this->getDefaultOptions(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->getMolliePaymentOptions($address->getCountryCode()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function getMolliePaymentOptions(string $countryCode): array |
89
|
|
|
{ |
90
|
|
|
$methods = $this->getDefaultOptions(); |
91
|
|
|
|
92
|
|
|
/** @var GatewayConfigInterface $gateway */ |
93
|
|
|
$gateway = $this->gatewayConfigRepository->findOneBy([ |
94
|
|
|
'factoryName' => MollieGatewayFactory::FACTORY_NAME, |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
if (null === $gateway) { |
98
|
|
|
return $this->getDefaultOptions(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway); |
|
|
|
|
102
|
|
|
|
103
|
|
|
if (empty($paymentConfigs)) { |
104
|
|
|
return $this->getDefaultOptions(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @var MollieGatewayConfigInterface $paymentMethod */ |
108
|
|
|
foreach ($paymentConfigs as $paymentMethod) { |
109
|
|
|
$methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $methods; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getDefaultOptions(): array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'data' => [], |
119
|
|
|
'image' => [], |
120
|
|
|
'issuers' => [], |
121
|
|
|
'paymentFee' => [], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|