|
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\Client\MollieApiClient; |
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Creator\MollieMethodsCreatorInterface; |
|
16
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
17
|
|
|
|
|
18
|
|
|
use Mollie\Api\Resources\Method; |
|
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class MollieAllowedMethodsResolver implements MollieAllowedMethodsResolverInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var MollieApiClientKeyResolverInterface */ |
|
24
|
|
|
private $mollieApiClientKeyResolver; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(MollieApiClientKeyResolverInterface $mollieApiClientKeyResolver) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->mollieApiClientKeyResolver = $mollieApiClientKeyResolver; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function resolve(OrderInterface $order): array |
|
32
|
|
|
{ |
|
33
|
|
|
$allowedMethodsIds = []; |
|
34
|
|
|
|
|
35
|
|
|
$client = $this->mollieApiClientKeyResolver->getClientWithKey(); |
|
36
|
|
|
|
|
37
|
|
|
/** API will return only payment methods allowed for order total, currency, billing country */ |
|
38
|
|
|
$allowedMethods = $client->methods->allActive($this->createParametersByOrder($order)); |
|
39
|
|
|
|
|
40
|
|
|
/** @var Method $method */ |
|
41
|
|
|
foreach ($allowedMethods as $method) { |
|
42
|
|
|
$allowedMethodsIds[] = $method->id; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $allowedMethodsIds; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function createParametersByOrder($order): array |
|
49
|
|
|
{ |
|
50
|
|
|
return array_merge( |
|
51
|
|
|
[ |
|
52
|
|
|
'amount[value]' => $this->parseTotalToString($order->getTotal()), |
|
53
|
|
|
'amount[currency]' => $order->getCurrencyCode(), |
|
54
|
|
|
'locale' => $order->getLocaleCode(), |
|
55
|
|
|
'billingCountry' => null !== $order->getBillingAddress() |
|
56
|
|
|
? $order->getBillingAddress()->getCountryCode() |
|
57
|
|
|
: null |
|
58
|
|
|
], |
|
59
|
|
|
MollieMethodsCreatorInterface::PARAMETERS |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function parseTotalToString(int $total): string |
|
64
|
|
|
{ |
|
65
|
|
|
return substr_replace((string) $total, '.', -2, 0); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|