-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAdyenConfigured.php
85 lines (71 loc) · 2.24 KB
/
AdyenConfigured.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Adyen\Hyva\Model\PaymentMethod\Filter;
use Adyen\Hyva\Api\ProcessingMetadataInterface;
use Adyen\Hyva\Magewire\Payment\Method\CreditCard;
use Adyen\Hyva\Model\PaymentMethod\PaymentMethods;
/**
* Filter out methods that are Adyen Based, but are not supported by Adyen configuration
*/
class AdyenConfigured implements FilterInterface
{
public function __construct(
private readonly PaymentMethods $paymentMethods
) {
}
/**
* {@inheritDoc}
*/
public function execute(int $quoteId, array $list): array
{
$configuredKeys = $this->collectConfiguredKeys($quoteId);
foreach ($list as $key => $method) {
if ($this->isMethodAdyenBased($method) && !$this->isMethodAvailable($method, $configuredKeys)) {
unset($list[$key]);
}
}
return $list;
}
/**
* @param int $quoteId
* @return array
*/
private function collectConfiguredKeys(int $quoteId): array
{
$paymentMethodsConfiguration = json_decode($this->paymentMethods->getData($quoteId), true);
if (!isset($paymentMethodsConfiguration['paymentMethodsExtraDetails'])) {
return [];
}
return array_keys($paymentMethodsConfiguration['paymentMethodsExtraDetails']);
}
/**
* @param $method
* @return bool
*/
private function isMethodAdyenBased($method): bool
{
if (str_starts_with($method->getCode(), ProcessingMetadataInterface::METHOD_ADYEN_PREFIX)) {
return true;
}
return false;
}
/**
* @param $method
* @param $configuredKeys
* @return bool
*/
private function isMethodAvailable($method, $configuredKeys): bool
{
$methodCode = $this->collectMethodCodeWithoutPrefix($method->getCode());
return in_array($methodCode, $configuredKeys);
}
private function collectMethodCodeWithoutPrefix($methodCode): string
{
if ($methodCode == CreditCard::METHOD_CC) {
return 'card';
}
return substr(
$methodCode,
strpos($methodCode, ProcessingMetadataInterface::METHOD_ADYEN_PREFIX) + strlen(ProcessingMetadataInterface::METHOD_ADYEN_PREFIX)
);
}
}