| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\PayPalGateway\API; |
| 4 |
|
| 5 |
class DccApplies |
| 6 |
{ |
| 7 |
protected string $country; |
| 8 |
protected string $currency; |
| 9 |
protected array $allowedCountryCurrencyMatrix; |
| 10 |
protected array $countryCardMatrix; |
| 11 |
|
| 12 |
/** |
| 13 |
* @throws \Exception |
| 14 |
*/ |
| 15 |
public function __construct($country, $currency) |
| 16 |
{ |
| 17 |
$this->country = $country; |
| 18 |
$this->currency = $currency; |
| 19 |
if (!$this->country || !$this->currency) { |
| 20 |
throw new \Exception(__('Please set store country and currency first!', 'fluent-cart')); |
| 21 |
} |
| 22 |
$this->allowedCountryCurrencyMatrix = self::dccSupportedCountryCurrencyMatrix(); |
| 23 |
$this->countryCardMatrix = self::countryCardMatrix(); |
| 24 |
} |
| 25 |
|
| 26 |
public function forCountryCurrency(): bool |
| 27 |
{ |
| 28 |
if (!in_array($this->country, array_keys($this->allowedCountryCurrencyMatrix), \true)) { |
| 29 |
return \false; |
| 30 |
} |
| 31 |
|
| 32 |
return in_array($this->currency, $this->allowedCountryCurrencyMatrix[$this->country], \true); |
| 33 |
} |
| 34 |
|
| 35 |
public static function countryCardMatrix(): array |
| 36 |
{ |
| 37 |
$mastercardVisaAmex = [ |
| 38 |
'mastercard' => [], |
| 39 |
'visa' => [], |
| 40 |
'amex' => [], |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Empty credit card arrays mean no restriction on currency. |
| 45 |
* return supported cards currency |
| 46 |
*/ |
| 47 |
return [ |
| 48 |
'AU' => [ |
| 49 |
'mastercard' => [], |
| 50 |
'visa' => [], |
| 51 |
'amex' => ['AUD'], |
| 52 |
], |
| 53 |
'AT' => $mastercardVisaAmex, |
| 54 |
'BE' => $mastercardVisaAmex, |
| 55 |
'BG' => $mastercardVisaAmex, |
| 56 |
'CN' => [ |
| 57 |
'mastercard' => [], |
| 58 |
'visa' => [], |
| 59 |
], |
| 60 |
'CY' => $mastercardVisaAmex, |
| 61 |
'CZ' => $mastercardVisaAmex, |
| 62 |
'DE' => $mastercardVisaAmex, |
| 63 |
'DK' => $mastercardVisaAmex, |
| 64 |
'EE' => $mastercardVisaAmex, |
| 65 |
'ES' => $mastercardVisaAmex, |
| 66 |
'FI' => $mastercardVisaAmex, |
| 67 |
'FR' => $mastercardVisaAmex, |
| 68 |
'GB' => $mastercardVisaAmex, |
| 69 |
'GR' => $mastercardVisaAmex, |
| 70 |
'HK' => $mastercardVisaAmex, |
| 71 |
'HU' => $mastercardVisaAmex, |
| 72 |
'IE' => $mastercardVisaAmex, |
| 73 |
'IT' => $mastercardVisaAmex, |
| 74 |
'US' => [ |
| 75 |
'mastercard' => [], |
| 76 |
'visa' => [], |
| 77 |
'amex' => ['USD'], |
| 78 |
'discover' => ['USD'], |
| 79 |
], |
| 80 |
'CA' => [ |
| 81 |
'mastercard' => [], |
| 82 |
'visa' => [], |
| 83 |
'amex' => ['CAD', 'USD'], |
| 84 |
'jcb' => ['CAD'], |
| 85 |
], |
| 86 |
'LI' => $mastercardVisaAmex, |
| 87 |
'LT' => $mastercardVisaAmex, |
| 88 |
'LU' => $mastercardVisaAmex, |
| 89 |
'LV' => $mastercardVisaAmex, |
| 90 |
'MT' => $mastercardVisaAmex, |
| 91 |
'MX' => $mastercardVisaAmex, |
| 92 |
'NL' => $mastercardVisaAmex, |
| 93 |
'NO' => $mastercardVisaAmex, |
| 94 |
'PL' => $mastercardVisaAmex, |
| 95 |
'PT' => $mastercardVisaAmex, |
| 96 |
'RO' => $mastercardVisaAmex, |
| 97 |
'SE' => $mastercardVisaAmex, |
| 98 |
'SI' => $mastercardVisaAmex, |
| 99 |
'SK' => $mastercardVisaAmex, |
| 100 |
'SG' => $mastercardVisaAmex, |
| 101 |
'JP' => [ |
| 102 |
'mastercard' => [], |
| 103 |
'visa' => [], |
| 104 |
'amex' => ['JPY'], |
| 105 |
'jcb' => ['JPY'], |
| 106 |
], |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
public static function dccSupportedCountryCurrencyMatrix(): array |
| 111 |
{ |
| 112 |
$defaultCurrencies = array('AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'HKD', 'GBP', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PHP', 'PLN', 'SGD', 'SEK', 'THB', 'TWD', 'USD'); |
| 113 |
|
| 114 |
// Returns which countries and currency combinations can be used for DCC |
| 115 |
return [ |
| 116 |
'AU' => $defaultCurrencies, |
| 117 |
'AT' => $defaultCurrencies, |
| 118 |
'BE' => $defaultCurrencies, |
| 119 |
'BG' => $defaultCurrencies, |
| 120 |
'CA' => $defaultCurrencies, |
| 121 |
'CN' => $defaultCurrencies, |
| 122 |
'CY' => $defaultCurrencies, |
| 123 |
'CZ' => $defaultCurrencies, |
| 124 |
'DK' => $defaultCurrencies, |
| 125 |
'EE' => $defaultCurrencies, |
| 126 |
'FI' => $defaultCurrencies, |
| 127 |
'FR' => $defaultCurrencies, |
| 128 |
'DE' => $defaultCurrencies, |
| 129 |
'GR' => $defaultCurrencies, |
| 130 |
'HK' => $defaultCurrencies, |
| 131 |
'HU' => $defaultCurrencies, |
| 132 |
'IE' => $defaultCurrencies, |
| 133 |
'IT' => $defaultCurrencies, |
| 134 |
'JP' => $defaultCurrencies, |
| 135 |
'LV' => $defaultCurrencies, |
| 136 |
'LI' => $defaultCurrencies, |
| 137 |
'LT' => $defaultCurrencies, |
| 138 |
'LU' => $defaultCurrencies, |
| 139 |
'MT' => $defaultCurrencies, |
| 140 |
'MX' => ['MXN'], |
| 141 |
'NL' => $defaultCurrencies, |
| 142 |
'PL' => $defaultCurrencies, |
| 143 |
'PT' => $defaultCurrencies, |
| 144 |
'RO' => $defaultCurrencies, |
| 145 |
'SK' => $defaultCurrencies, |
| 146 |
'SG' => $defaultCurrencies, |
| 147 |
'SI' => $defaultCurrencies, |
| 148 |
'ES' => $defaultCurrencies, |
| 149 |
'SE' => $defaultCurrencies, |
| 150 |
'GB' => $defaultCurrencies, |
| 151 |
'US' => $defaultCurrencies, |
| 152 |
'NO' => $defaultCurrencies, |
| 153 |
]; |
| 154 |
|
| 155 |
} |
| 156 |
} |