|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Library\Stripe; |
| 4 | + |
| 5 | +use App\Library\Stripe\Exceptions\UnsupportedCurrency; |
| 6 | + |
| 7 | +class Amount |
| 8 | +{ |
| 9 | + public static $supportCurrencies = [ |
| 10 | + 'USD', 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', |
| 11 | + 'AWG', 'AZN', 'BAM', 'BBD', 'BDT', 'BGN', 'BIF', 'BMD', 'BND', |
| 12 | + 'BOB', 'BRL', 'BSD', 'BWP', 'BYN', 'BZD', 'CAD', 'CDF', 'CHF', |
| 13 | + 'CLP', 'CNY', 'COP', 'CRC', 'CVE', 'CZK', 'DJF', 'DKK', 'DOP', |
| 14 | + 'DZD', 'EGP', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GIP', |
| 15 | + 'GMD', 'GNF', 'GTQ', 'GYD', 'HKD', 'HNL', 'HTG', 'HUF', 'IDR', |
| 16 | + 'ILS', 'INR', 'ISK', 'JMD', 'JPY', 'KES', 'KGS', 'KHR', 'KMF', |
| 17 | + 'KRW', 'KYD', 'KZT', 'LAK', 'LBP', 'LKR', 'LRD', 'LSL', 'MAD', |
| 18 | + 'MDL', 'MGA', 'MKD', 'MMK', 'MNT', 'MOP', 'MUR', 'MVR', 'MWK', |
| 19 | + 'MXN', 'MYR', 'MZN', 'NAD', 'NGN', 'NIO', 'NOK', 'NPR', 'NZD', |
| 20 | + 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', 'PYG', 'QAR', 'RON', |
| 21 | + 'RSD', 'RUB', 'RWF', 'SAR', 'SBD', 'SCR', 'SEK', 'SGD', 'SHP', |
| 22 | + 'SLE', 'SOS', 'SRD', 'STD', 'SZL', 'THB', 'TJS', 'TOP', 'TRY', |
| 23 | + 'TTD', 'TWD', 'TZS', 'UAH', 'UGX', 'UYU', 'UZS', 'VND', 'VUV', |
| 24 | + 'WST', 'XAF', 'XCD', 'XCG', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMW', |
| 25 | + ]; |
| 26 | + |
| 27 | + public static $eurSubCurrencies = [ |
| 28 | + 'AD', 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FO', 'FI', |
| 29 | + 'FR', 'DE', 'GI', 'GR', 'GL', 'GG', 'VA', 'HU', 'IS', 'IE', 'IM', |
| 30 | + 'IT', 'JE', 'LV', 'LI', 'LT', 'LU', 'MT', 'MC', 'NL', 'NO', 'PL', |
| 31 | + 'PT', 'RO', 'PM', 'SM', 'SK', 'SI', 'ES', 'SE', 'TR', 'GB', |
| 32 | + ]; |
| 33 | + |
| 34 | + public static $specialDecimalCurrencies = [ |
| 35 | + 'BIF' => 0, |
| 36 | + 'CLP' => 0, |
| 37 | + 'DJF' => 0, |
| 38 | + 'GNF' => 0, |
| 39 | + 'JPY' => 0, |
| 40 | + 'KMF' => 0, |
| 41 | + 'KRW' => 0, |
| 42 | + 'MGA' => 0, |
| 43 | + 'PYG' => 0, |
| 44 | + 'RWF' => 0, |
| 45 | + 'UGX' => 0, |
| 46 | + 'VND' => 0, |
| 47 | + 'VUV' => 0, |
| 48 | + 'XAF' => 0, |
| 49 | + 'XOF' => 0, |
| 50 | + 'XPF' => 0, |
| 51 | + ]; |
| 52 | + |
| 53 | + public static $specialActualDecimal = [ |
| 54 | + 'ISK' => 0, |
| 55 | + 'HUF' => 0, |
| 56 | + 'TWD' => 0, |
| 57 | + 'UGX' => 0, |
| 58 | + ]; |
| 59 | + |
| 60 | + // if add more special currencies when it has special actual decimal or special decimal, please also add more test cases |
| 61 | + public static $specialCurrenciesMaximumDigits = [ |
| 62 | + 'IDR' => 9, |
| 63 | + 'INR' => 9, |
| 64 | + ]; |
| 65 | + |
| 66 | + private static function getCurrency() |
| 67 | + { |
| 68 | + $currency = strtoupper(config('stripe.currency', 'hkd')); |
| 69 | + if (in_array($currency, self::$eurSubCurrencies)) { |
| 70 | + $currency = 'EUR'; |
| 71 | + } |
| 72 | + |
| 73 | + return $currency; |
| 74 | + } |
| 75 | + |
| 76 | + public static function getActualDecimal(): int |
| 77 | + { |
| 78 | + $currency = self::getCurrency(); |
| 79 | + if (array_key_exists($currency, self::$specialDecimalCurrencies)) { |
| 80 | + return self::$specialDecimalCurrencies[$currency]; |
| 81 | + } |
| 82 | + if (in_array($currency, self::$supportCurrencies)) { |
| 83 | + return 2; |
| 84 | + } |
| 85 | + |
| 86 | + throw new UnsupportedCurrency($currency); |
| 87 | + } |
| 88 | + |
| 89 | + public static function getValidationDecimal(): int |
| 90 | + { |
| 91 | + $currency = self::getCurrency(); |
| 92 | + if (array_key_exists($currency, self::$specialActualDecimal)) { |
| 93 | + return self::$specialActualDecimal[$currency]; |
| 94 | + } |
| 95 | + |
| 96 | + return self::getActualDecimal(); |
| 97 | + } |
| 98 | + |
| 99 | + public static function getMaximumDigits(): int |
| 100 | + { |
| 101 | + $currency = self::getCurrency(); |
| 102 | + if (array_key_exists($currency, self::$specialCurrenciesMaximumDigits)) { |
| 103 | + return self::$specialCurrenciesMaximumDigits[$currency]; |
| 104 | + } |
| 105 | + if (in_array($currency, self::$supportCurrencies)) { |
| 106 | + return 8; |
| 107 | + } |
| 108 | + |
| 109 | + throw new UnsupportedCurrency($currency); |
| 110 | + } |
| 111 | + |
| 112 | + public static function getMaximumValidation(): int|float |
| 113 | + { |
| 114 | + $digits = self::getMaximumDigits(); |
| 115 | + $digits -= self::getActualDecimal(); |
| 116 | + $decimal = self::getValidationDecimal(); |
| 117 | + if ($decimal > 0) { |
| 118 | + return (float) (str_repeat('9', $digits).'.'.str_repeat('9', $decimal)); |
| 119 | + } else { |
| 120 | + return (int) str_repeat('9', $digits); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments