| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tamara\Wp\Plugin\Helpers; |
| 4 |
|
| 5 |
class PhoneHelper |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Known Tamara market dialing codes (digits only, no leading zeros). |
| 9 |
* |
| 10 |
* @return string[] |
| 11 |
*/ |
| 12 |
public static function getKnownDialCodes() |
| 13 |
{ |
| 14 |
return ['966', '971', '965', '973']; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Countries qualified to show Tamara on checkout (KSA and UAE). |
| 19 |
* |
| 20 |
* @return string[] |
| 21 |
*/ |
| 22 |
public static function getQualifiedCountryCodes() |
| 23 |
{ |
| 24 |
return ['SA', 'AE']; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Whether the billing/checkout country qualifies for Tamara (KSA or UAE). |
| 29 |
* |
| 30 |
* @param string $countryCode ISO 3166-1 alpha-2 country code |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public static function isQualifiedCountry($countryCode) |
| 35 |
{ |
| 36 |
return in_array(strtoupper((string) $countryCode), self::getQualifiedCountryCodes(), true); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Resolve dialing code digits for a WooCommerce country code (e.g. SA -> 966). |
| 41 |
* |
| 42 |
* @param string $countryCode ISO 3166-1 alpha-2 country code |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public static function getDialCodeDigits($countryCode) |
| 47 |
{ |
| 48 |
$countryCode = strtoupper((string) $countryCode); |
| 49 |
if ('' === $countryCode) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
$callingCode = ''; |
| 54 |
if (function_exists('WC') && WC()->countries) { |
| 55 |
$callingCode = (string) WC()->countries->get_country_calling_code($countryCode); |
| 56 |
} |
| 57 |
|
| 58 |
if ('' === $callingCode) { |
| 59 |
$fallback = [ |
| 60 |
'SA' => '966', |
| 61 |
'AE' => '971', |
| 62 |
'KW' => '965', |
| 63 |
'BH' => '973', |
| 64 |
]; |
| 65 |
$callingCode = $fallback[$countryCode] ?? ''; |
| 66 |
} |
| 67 |
|
| 68 |
if ('' === $callingCode) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
// Strip non-digits, then leading zeros (e.g. +966 / 00966 -> 966). |
| 73 |
return ltrim(preg_replace('/\D+/', '', $callingCode), '0'); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Format phone for Tamara pre-checkout API as: |
| 78 |
* <country_code_without_plus_or_zeros_prefixed><phone_number_without_zero_prefix> |
| 79 |
* |
| 80 |
* If the customer phone already includes a country calling code, that dial code is kept |
| 81 |
* and any national trunk zero after it is stripped. Otherwise the dialing code is taken |
| 82 |
* from the billing country. |
| 83 |
* |
| 84 |
* @param string $phone |
| 85 |
* @param string $billingCountryCode ISO country code from checkout billing address |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public static function formatForPreCheckout($phone, $billingCountryCode = '') |
| 90 |
{ |
| 91 |
if ('' === trim((string) $phone)) { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
$digits = preg_replace('/\D+/', '', (string) $phone); |
| 96 |
if ('' === $digits) { |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
// International prefix 00... |
| 101 |
if (0 === strpos($digits, '00')) { |
| 102 |
$digits = substr($digits, 2); |
| 103 |
} |
| 104 |
|
| 105 |
$billingDialCode = self::getDialCodeDigits($billingCountryCode); |
| 106 |
|
| 107 |
// Phone already starts with billing country dial code. |
| 108 |
if ('' !== $billingDialCode && 0 === strpos($digits, $billingDialCode)) { |
| 109 |
return self::combineDialCodeAndNationalNumber($billingDialCode, $digits); |
| 110 |
} |
| 111 |
|
| 112 |
// Phone already starts with a known Tamara market dial code (longest match first). |
| 113 |
$knownDialCodes = self::getKnownDialCodes(); |
| 114 |
usort($knownDialCodes, function ($a, $b) { |
| 115 |
return strlen($b) - strlen($a); |
| 116 |
}); |
| 117 |
foreach ($knownDialCodes as $knownDialCode) { |
| 118 |
if (0 === strpos($digits, $knownDialCode)) { |
| 119 |
return self::combineDialCodeAndNationalNumber($knownDialCode, $digits); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// No country code in input — prepend dial code from billing country. |
| 124 |
$nationalNumber = ltrim($digits, '0'); |
| 125 |
if ('' === $nationalNumber) { |
| 126 |
return ''; |
| 127 |
} |
| 128 |
|
| 129 |
if ('' !== $billingDialCode) { |
| 130 |
return $billingDialCode.$nationalNumber; |
| 131 |
} |
| 132 |
|
| 133 |
return $nationalNumber; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Keep dial code and strip a national trunk zero from the remainder. |
| 138 |
* |
| 139 |
* @param string $dialCode |
| 140 |
* @param string $digits Full digit string that starts with $dialCode |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
protected static function combineDialCodeAndNationalNumber($dialCode, $digits) |
| 145 |
{ |
| 146 |
$nationalNumber = ltrim(substr($digits, strlen($dialCode)), '0'); |
| 147 |
if ('' === $nationalNumber) { |
| 148 |
return ''; |
| 149 |
} |
| 150 |
|
| 151 |
return $dialCode.$nationalNumber; |
| 152 |
} |
| 153 |
} |
| 154 |
|