PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Support / Currency.php
Currency.php
729 lines 21.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Support;
4
5 use SureCart\Models\DisplayCurrency;
6
7 /**
8 * Handles currency coversion and formatting
9 */
10 class Currency {
11 /**
12 * Check if the currency is supported and enabled for display/conversion.
13 *
14 * @param string $currency The currency code.
15 *
16 * @return bool
17 */
18 public static function isSupportedCurrency( $currency ) {
19 if ( empty( $currency ) ) {
20 return false;
21 }
22 $display_currencies = DisplayCurrency::get();
23 if ( is_wp_error( $display_currencies ) ) {
24 return false;
25 }
26 return in_array( $currency, array_column( $display_currencies, 'currency' ), true );
27 }
28
29 /**
30 * Get the current currency.
31 *
32 * @return string
33 */
34 public static function getCurrentCurrency() {
35 // we are not converting, so we can return the default currency.
36 if ( ! \SureCart::currency()->is_converting ) {
37 return self::getDefaultCurrency();
38 }
39
40 // Try getting currency from request parameters.
41 $currency = self::getCurrencyFromRequest();
42 if ( $currency ) {
43 return $currency;
44 }
45
46 // Try getting currency from geolocation.
47 $currency = self::getCurrencyFromGeolocation();
48 if ( $currency ) {
49 return $currency;
50 }
51
52 // Fallback to default currency.
53 return self::getDefaultCurrency();
54 }
55
56 /**
57 * Get currency from request parameters (URL or cookie)
58 *
59 * @return string|null
60 */
61 public static function getCurrencyFromRequest() {
62 $currency = strtolower( sanitize_text_field( $_GET['currency'] ?? $_COOKIE['sc_current_currency'] ?? null ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
63 return self::isSupportedCurrency( $currency ) ? strtolower( sanitize_text_field( $currency ) ) : null;
64 }
65
66 /**
67 * Get currency from geolocation using NumberFormatter
68 *
69 * @return string|null
70 */
71 public static function getCurrencyFromGeolocation() {
72 if ( ! class_exists( 'NumberFormatter' ) || ! \SureCart::settings()->get( 'currency_geolocation_enabled', true ) ) {
73 return null;
74 }
75
76 $locale = self::getPreferredLocaleFromHeader();
77 if ( ! isset( $locale ) ) {
78 return null;
79 }
80
81 try {
82 $fmt = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );
83 $currency = strtolower( $fmt->getTextAttribute( \NumberFormatter::CURRENCY_CODE ) );
84 return self::isSupportedCurrency( $currency ) ? strtolower( sanitize_text_field( $currency ) ) : null;
85 } catch ( \Exception $e ) {
86 error_log( 'SureCart: NumberFormatter error - ' . $e->getMessage() );
87 }
88
89 return null;
90 }
91
92 /**
93 * Get the currency locale.
94 *
95 * @return string
96 */
97 public static function getCurrencyLocale() {
98 $locale = \SureCart::settings()->get( 'currency_locale', false );
99
100 if ( empty( $locale ) || 'default' === $locale ) {
101 $locale = get_locale();
102 }
103
104 return $locale;
105 }
106
107 /**
108 * Get the locales.
109 *
110 * @return array
111 */
112 public static function getLocales() {
113 // Load translations.
114 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
115 $available_translations = wp_get_available_translations();
116
117 // english is not in the available translations.
118 $locales['en_US'] = array(
119 'language' => 'en_US',
120 'english_name' => 'English (United States)',
121 'native_name' => 'English (United States)',
122 );
123
124 // Merge with available translations.
125 $locales = array_merge( $locales, $available_translations );
126
127 // sort by english name.
128 uasort(
129 $locales,
130 function ( $a, $b ) {
131 return strnatcasecmp( $a['english_name'], $b['english_name'] );
132 }
133 );
134
135 $current_locale = get_locale() ? $locales[ get_locale() ] : $locales['en_US'];
136 $store_default_name = sprintf(
137 /* translators: %s: current language name */
138 __( 'Site Locale - %s', 'surecart' ),
139 $current_locale['native_name']
140 );
141
142 return array_merge(
143 array(
144 'default' => array(
145 'language' => 'default',
146 'english_name' => $store_default_name,
147 'native_name' => $store_default_name,
148 ),
149 ),
150 $locales
151 );
152 }
153
154 /**
155 * Get the preferred locale from the header.
156 *
157 * @return string
158 */
159 public static function getPreferredLocaleFromHeader() {
160 $accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
161 // Split on commas.
162 $languages = explode( ',', $accept_language );
163 $best_lang = '';
164
165 foreach ( $languages as $lang ) {
166 // Each segment might look like "en-US" or "en;q=0.9".
167 // Strip out any ";q=..." portion.
168 if ( strpos( $lang, ';q=' ) !== false ) {
169 list( $lang_part, ) = explode( ';', $lang );
170 } else {
171 $lang_part = $lang;
172 }
173
174 // Clean up whitespace.
175 $lang_part = trim( $lang_part );
176
177 // If we haven't set a bestLang yet, use the first valid one.
178 if ( $lang_part ) {
179 $best_lang = $lang_part;
180 break;
181 }
182 }
183
184 // Convert from "en-US" to "en_US".
185 $best_lang = str_replace( '-', '_', $best_lang );
186
187 // Validate the locale format (must be in standard locale format).
188 if ( ! preg_match( '/^[a-z]{2,3}(?:_[A-Z][a-z]{3})?(?:_[A-Z]{2})?$/', $best_lang ) ) {
189 return self::getCurrencyLocale(); // Invalid format, fallback to default.
190 }
191
192 return $best_lang ? $best_lang : self::getCurrencyLocale(); // Fallback.
193 }
194
195 /**
196 * Get the default currency.
197 *
198 * @return string
199 */
200 public static function getDefaultCurrency() {
201 return strtolower( \SureCart::account()->currency ?? 'usd' );
202 }
203
204 /**
205 * Get the exchange rate for the current currency.
206 *
207 * @param string|null $currency_code Currency code.
208 * @return float
209 */
210 public static function getExchangeRate( $currency_code = null ) {
211 // get the currency.
212 $currency = self::getDisplayCurrency( $currency_code );
213
214 // return the exchange rate.
215 return $currency ? ( $currency->current_exchange_rate ?? 1 ) : 1;
216 }
217
218 /**
219 * Get the currency.
220 *
221 * @param string $currency_code Currency code.
222 *
223 * @return DisplayCurrency|null
224 */
225 public static function getDisplayCurrency( $currency_code = null ) {
226 // get the currencies.
227 $currencies = DisplayCurrency::get();
228
229 // handle error.
230 if ( is_wp_error( $currencies ) ) {
231 return null;
232 }
233
234 // get the currency.
235 $currency = array_values(
236 array_filter(
237 $currencies,
238 function ( $currency ) use ( $currency_code ) {
239 return strtolower( $currency_code ) === strtolower( $currency->currency );
240 }
241 )
242 );
243
244 // return the currency.
245 return $currency[0] ?? null;
246 }
247
248 /**
249 * Get the name of the currency.
250 *
251 * @param string $currency_code Currency code.
252 *
253 * @return string The name of the currency.
254 */
255 public static function getName( $currency_code ) {
256 return self::getSupportedCurrencies()[ $currency_code ] ?? $currency_code;
257 }
258
259 /**
260 * Get all available Currency symbols.
261 *
262 * Currency symbols and names should follow the Unicode CLDR recommendation (http://cldr.unicode.org/translation/currency-names)
263 *
264 * @param string|null $key Currency code.
265 *
266 * @return string
267 */
268 public static function getCurrencySymbol( $key = null ) {
269 $key = strtoupper( $key ?? self::getCurrentCurrency() );
270 $symbols = apply_filters(
271 'surecart/currency_symbols',
272 array(
273 'AED' => '&#x62f;.&#x625;',
274 'AFN' => '&#x60b;',
275 'ALL' => 'L',
276 'AMD' => 'AMD',
277 'ANG' => '&fnof;',
278 'AOA' => 'Kz',
279 'ARS' => '&#36;',
280 'AUD' => '&#36;',
281 'AWG' => 'Afl.',
282 'AZN' => 'AZN',
283 'BAM' => 'KM',
284 'BBD' => '&#36;',
285 'BDT' => '&#2547;&nbsp;',
286 'BGN' => '&#1083;&#1074;.',
287 'BHD' => '.&#x62f;.&#x628;',
288 'BIF' => 'Fr',
289 'BMD' => '&#36;',
290 'BND' => '&#36;',
291 'BOB' => 'Bs.',
292 'BRL' => '&#82;&#36;',
293 'BSD' => '&#36;',
294 'BTC' => '&#3647;',
295 'BTN' => 'Nu.',
296 'BWP' => 'P',
297 'BYR' => 'Br',
298 'BYN' => 'Br',
299 'BZD' => '&#36;',
300 'CAD' => '&#36;',
301 'CDF' => 'Fr',
302 'CHF' => '&#67;&#72;&#70;',
303 'CLP' => '&#36;',
304 'CNY' => '&yen;',
305 'COP' => '&#36;',
306 'CRC' => '&#x20a1;',
307 'CUC' => '&#36;',
308 'CUP' => '&#36;',
309 'CVE' => '&#36;',
310 'CZK' => '&#75;&#269;',
311 'DJF' => 'Fr',
312 'DKK' => 'DKK',
313 'DOP' => 'RD&#36;',
314 'DZD' => '&#x62f;.&#x62c;',
315 'EGP' => 'EGP',
316 'ERN' => 'Nfk',
317 'ETB' => 'Br',
318 'EUR' => '&euro;',
319 'FJD' => '&#36;',
320 'FKP' => '&pound;',
321 'GBP' => '&pound;',
322 'GEL' => '&#x20be;',
323 'GGP' => '&pound;',
324 'GHS' => '&#x20b5;',
325 'GIP' => '&pound;',
326 'GMD' => 'D',
327 'GNF' => 'Fr',
328 'GTQ' => 'Q',
329 'GYD' => '&#36;',
330 'HKD' => '&#36;',
331 'HNL' => 'L',
332 'HRK' => 'kn',
333 'HTG' => 'G',
334 'HUF' => '&#70;&#116;',
335 'IDR' => 'Rp',
336 'ILS' => '&#8362;',
337 'IMP' => '&pound;',
338 'INR' => '&#8377;',
339 'IQD' => '&#x639;.&#x62f;',
340 'IRR' => '&#xfdfc;',
341 'IRT' => '&#x062A;&#x0648;&#x0645;&#x0627;&#x0646;',
342 'ISK' => 'kr.',
343 'JEP' => '&pound;',
344 'JMD' => '&#36;',
345 'JOD' => '&#x62f;.&#x627;',
346 'JPY' => '&yen;',
347 'KES' => 'KSh',
348 'KGS' => '&#x441;&#x43e;&#x43c;',
349 'KHR' => '&#x17db;',
350 'KMF' => 'Fr',
351 'KPW' => '&#x20a9;',
352 'KRW' => '&#8361;',
353 'KWD' => '&#x62f;.&#x643;',
354 'KYD' => '&#36;',
355 'KZT' => '&#8376;',
356 'LAK' => '&#8365;',
357 'LBP' => '&#x644;.&#x644;',
358 'LKR' => '&#xdbb;&#xdd4;',
359 'LRD' => '&#36;',
360 'LSL' => 'L',
361 'LYD' => '&#x644;.&#x62f;',
362 'MAD' => '&#x62f;.&#x645;.',
363 'MDL' => 'MDL',
364 'MKD' => '&#x434;&#x435;&#x43d;',
365 'MMK' => 'Ks',
366 'MNT' => '&#x20ae;',
367 'MOP' => 'P',
368 'MRU' => 'UM',
369 'MUR' => '&#x20a8;',
370 'MVR' => '.&#x783;',
371 'MWK' => 'MK',
372 'MXN' => '&#36;',
373 'MYR' => '&#82;&#77;',
374 'MZN' => 'MT',
375 'NAD' => 'N&#36;',
376 'NGN' => '&#8358;',
377 'NIO' => 'C&#36;',
378 'NOK' => '&#107;&#114;',
379 'NPR' => '&#8360;',
380 'NZD' => '&#36;',
381 'OMR' => '&#x631;.&#x639;.',
382 'PAB' => 'B/.',
383 'PEN' => 'S/',
384 'PGK' => 'K',
385 'PHP' => '&#8369;',
386 'PKR' => '&#8360;',
387 'PLN' => '&#122;&#322;',
388 'PRB' => '&#x440;.',
389 'PYG' => '&#8370;',
390 'QAR' => '&#x631;.&#x642;',
391 'RMB' => '&yen;',
392 'RON' => 'lei',
393 'RSD' => '&#1088;&#1089;&#1076;',
394 'RUB' => '&#8381;',
395 'RWF' => 'Fr',
396 'SAR' => '&#x631;.&#x633;',
397 'SBD' => '&#36;',
398 'SCR' => '&#x20a8;',
399 'SDG' => '&#x62c;.&#x633;.',
400 'SEK' => '&#107;&#114;',
401 'SGD' => '&#36;',
402 'SHP' => '&pound;',
403 'SLL' => 'Le',
404 'SOS' => 'Sh',
405 'SRD' => '&#36;',
406 'SSP' => '&pound;',
407 'STN' => 'Db',
408 'SYP' => '&#x644;.&#x633;',
409 'SZL' => 'L',
410 'THB' => '&#3647;',
411 'TJS' => '&#x405;&#x41c;',
412 'TMT' => 'm',
413 'TND' => '&#x62f;.&#x62a;',
414 'TOP' => 'T&#36;',
415 'TRY' => '&#8378;',
416 'TTD' => '&#36;',
417 'TWD' => '&#78;&#84;&#36;',
418 'TZS' => 'Sh',
419 'UAH' => '&#8372;',
420 'UGX' => 'UGX',
421 'USD' => '&#36;',
422 'UYU' => '&#36;',
423 'UZS' => 'UZS',
424 'VEF' => 'Bs F',
425 'VES' => 'Bs.S',
426 'VND' => '&#8363;',
427 'VUV' => 'Vt',
428 'WST' => 'T',
429 'XAF' => 'CFA',
430 'XCD' => '&#36;',
431 'XOF' => 'CFA',
432 'XPF' => 'Fr',
433 'YER' => '&#xfdfc;',
434 'ZAR' => '&#82;',
435 'ZMW' => 'ZK',
436 )
437 );
438 return $symbols[ $key ] ?? '&#36;';
439 }
440
441 /**
442 * Format the currency into the current locale.
443 *
444 * @param integer $amount Amount as an integer.
445 * @param string $currency_code 3 digit currency code.
446 * @param array $args Additional formatting arguments.
447 *
448 * @return string
449 */
450 public static function format( $amount, $currency_code = null, $args = [] ) {
451 $args = wp_parse_args(
452 $args,
453 [
454 // convert the currency by default.
455 'convert' => true,
456 ]
457 );
458
459 $currency_code = $currency_code ?? \SureCart::account()->currency;
460
461 // maybe convert the amount.
462 $converted_amount = self::maybeConvertAmount( $amount, $currency_code );
463
464 // Only convert if currency matches account currency and conversion is requested.
465 // this prevents converting currency from previous purchases that may have been in a different currency.
466 if ( \SureCart::account()->currency === $currency_code && ! empty( $args['convert'] ) ) {
467 $currency_code = self::getCurrentCurrency();
468 $converted_amount = $converted_amount * self::getExchangeRate( $currency_code );
469 }
470
471 if ( class_exists( 'NumberFormatter' ) ) {
472 $fmt = new \NumberFormatter( apply_filters( 'surecart/currency/locale', self::getCurrencyLocale() ), \NumberFormatter::CURRENCY );
473
474 // Extract the fractional part.
475 $fractional_part = fmod( $converted_amount, 1 );
476
477 // Check if the fractional part is .00.
478 $minimum_fraction_digits = ( 0.00 === $fractional_part ) ? 0 : 2;
479
480 $fmt->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, apply_filters( 'surecart/currency/max_cents', $minimum_fraction_digits, $amount, $converted_amount ) );
481 return apply_filters( 'surecart/currency/format', $fmt->formatCurrency( $converted_amount, strtoupper( $currency_code ) ), $amount, $currency_code );
482 }
483
484 return apply_filters( 'surecart/currency/format', html_entity_decode( self::getCurrencySymbol( $currency_code ) ) . self::formatCurrencyNumber( $converted_amount ), $amount, $currency_code );
485 }
486
487 /**
488 * Get zero decimal currencies in uppercase.
489 *
490 * @return array
491 */
492 public static function getZeroDecicalCurrencies(): array {
493 return array(
494 'BIF',
495 'BYR',
496 'CLP',
497 'DJF',
498 'GNF',
499 'ISK',
500 'JPY',
501 'KMF',
502 'KRW',
503 'PYG',
504 'RWF',
505 'UGX',
506 'VND',
507 'VUV',
508 'XAF',
509 'XAG',
510 'XAU',
511 'XBA',
512 'XBB',
513 'XBC',
514 'XBD',
515 'XDR',
516 'XOF',
517 'XPD',
518 'XPF',
519 'XPT',
520 'XTS',
521 );
522 }
523
524 /**
525 * Format the currency number.
526 *
527 * @param integer|float $amount Amount.
528 *
529 * @return string Formatted currency number.
530 */
531 public static function formatCurrencyNumber( $amount ) {
532 if ( ! is_numeric( $amount ) ) {
533 return '0';
534 }
535
536 return number_format_i18n( (float) $amount, 2 );
537 }
538
539 /**
540 * Get a list of supported currencies.
541 *
542 * @return array
543 */
544 public static function list() {
545 $currencies = [];
546 foreach ( self::getSupportedCurrencies() as $currency => $name ) {
547 $currencies[] = [
548 'currency' => $currency,
549 'name' => $name,
550 'symbol' => html_entity_decode( self::getCurrencySymbol( $currency ) ),
551 'flag' => plugins_url( 'images/flags/' . $currency . '.svg', SURECART_PLUGIN_FILE ),
552 ];
553 }
554 return $currencies;
555 }
556
557 /**
558 * Get a list of supported currencies.
559 *
560 * @return array
561 */
562 public static function getSupportedCurrencies() {
563 return [
564 'all' => __( 'Albanian Lek', 'surecart' ),
565 'dzd' => __( 'Algerian Dinar', 'surecart' ),
566 'aoa' => __( 'Angolan Kwanza', 'surecart' ),
567 'ars' => __( 'Argentine Peso', 'surecart' ),
568 'amd' => __( 'Armenian Dram', 'surecart' ),
569 'awg' => __( 'Aruban Florin', 'surecart' ),
570 'aud' => __( 'Australian Dollar', 'surecart' ),
571 'azn' => __( 'Azerbaijani Manat', 'surecart' ),
572 'bsd' => __( 'Bahamian Dollar', 'surecart' ),
573 'bdt' => __( 'Bangladeshi Taka', 'surecart' ),
574 'bbd' => __( 'Barbadian Dollar', 'surecart' ),
575 'byn' => __( 'Belarusian Ruble', 'surecart' ),
576 'bzd' => __( 'Belize Dollar', 'surecart' ),
577 'bmd' => __( 'Bermudian Dollar', 'surecart' ),
578 'bob' => __( 'Bolivian Boliviano', 'surecart' ),
579 'bam' => __( 'Bosnia and Herzegovina Convertible Mark', 'surecart' ),
580 'bwp' => __( 'Botswana Pula', 'surecart' ),
581 'brl' => __( 'Brazilian Real', 'surecart' ),
582 'gbp' => __( 'British Pound', 'surecart' ),
583 'bnd' => __( 'Brunei Dollar', 'surecart' ),
584 'bgn' => __( 'Bulgarian Lev', 'surecart' ),
585 'bif' => __( 'Burundian Franc', 'surecart' ),
586 'khr' => __( 'Cambodian Riel', 'surecart' ),
587 'cad' => __( 'Canadian Dollar', 'surecart' ),
588 'cve' => __( 'Cape Verdean Escudo', 'surecart' ),
589 'kyd' => __( 'Cayman Islands Dollar', 'surecart' ),
590 'xaf' => __( 'Central African Cfa Franc', 'surecart' ),
591 'xpf' => __( 'Cfp Franc', 'surecart' ),
592 'clp' => __( 'Chilean Peso', 'surecart' ),
593 'cny' => __( 'Chinese Renminbi Yuan', 'surecart' ),
594 'cop' => __( 'Colombian Peso', 'surecart' ),
595 'kmf' => __( 'Comorian Franc', 'surecart' ),
596 'cdf' => __( 'Congolese Franc', 'surecart' ),
597 'crc' => __( 'Costa Rican Colón', 'surecart' ),
598 'hrk' => __( 'Croatian Kuna', 'surecart' ),
599 'czk' => __( 'Czech Koruna', 'surecart' ),
600 'dkk' => __( 'Danish Krone', 'surecart' ),
601 'djf' => __( 'Djiboutian Franc', 'surecart' ),
602 'dop' => __( 'Dominican Peso', 'surecart' ),
603 'xcd' => __( 'East Caribbean Dollar', 'surecart' ),
604 'egp' => __( 'Egyptian Pound', 'surecart' ),
605 'etb' => __( 'Ethiopian Birr', 'surecart' ),
606 'eur' => __( 'Euro', 'surecart' ),
607 'fkp' => __( 'Falkland Pound', 'surecart' ),
608 'fjd' => __( 'Fijian Dollar', 'surecart' ),
609 'gmd' => __( 'Gambian Dalasi', 'surecart' ),
610 'gel' => __( 'Georgian Lari', 'surecart' ),
611 'ghs' => __( 'Ghanaian Cedi', 'surecart' ),
612 'gip' => __( 'Gibraltar Pound', 'surecart' ),
613 'gtq' => __( 'Guatemalan Quetzal', 'surecart' ),
614 'gnf' => __( 'Guinean Franc', 'surecart' ),
615 'gyd' => __( 'Guyanese Dollar', 'surecart' ),
616 'htg' => __( 'Haitian Gourde', 'surecart' ),
617 'hnl' => __( 'Honduran Lempira', 'surecart' ),
618 'hkd' => __( 'Hong Kong Dollar', 'surecart' ),
619 'huf' => __( 'Hungarian Forint', 'surecart' ),
620 'isk' => __( 'Icelandic Króna', 'surecart' ),
621 'inr' => __( 'Indian Rupee', 'surecart' ),
622 'idr' => __( 'Indonesian Rupiah', 'surecart' ),
623 'ils' => __( 'Israeli New Sheqel', 'surecart' ),
624 'jmd' => __( 'Jamaican Dollar', 'surecart' ),
625 'jpy' => __( 'Japanese Yen', 'surecart' ),
626 'kzt' => __( 'Kazakhstani Tenge', 'surecart' ),
627 'kes' => __( 'Kenyan Shilling', 'surecart' ),
628 'kgs' => __( 'Kyrgyzstani Som', 'surecart' ),
629 'lak' => __( 'Lao Kip', 'surecart' ),
630 'lbp' => __( 'Lebanese Pound', 'surecart' ),
631 'lsl' => __( 'Lesotho Loti', 'surecart' ),
632 'lrd' => __( 'Liberian Dollar', 'surecart' ),
633 'mop' => __( 'Macanese Pataca', 'surecart' ),
634 'mkd' => __( 'Macedonian Denar', 'surecart' ),
635 'mwk' => __( 'Malawian Kwacha', 'surecart' ),
636 'myr' => __( 'Malaysian Ringgit', 'surecart' ),
637 'mvr' => __( 'Maldivian Rufiyaa', 'surecart' ),
638 'mro' => __( 'Mauritanian Ouguiya', 'surecart' ),
639 'mur' => __( 'Mauritian Rupee', 'surecart' ),
640 'mxn' => __( 'Mexican Peso', 'surecart' ),
641 'mdl' => __( 'Moldovan Leu', 'surecart' ),
642 'mnt' => __( 'Mongolian Tögrög', 'surecart' ),
643 'mad' => __( 'Moroccan Dirham', 'surecart' ),
644 'mzn' => __( 'Mozambican Metical', 'surecart' ),
645 'mmk' => __( 'Myanmar Kyat', 'surecart' ),
646 'nad' => __( 'Namibian Dollar', 'surecart' ),
647 'npr' => __( 'Nepalese Rupee', 'surecart' ),
648 'ang' => __( 'Netherlands Antillean Gulden', 'surecart' ),
649 'twd' => __( 'New Taiwan Dollar', 'surecart' ),
650 'nzd' => __( 'New Zealand Dollar', 'surecart' ),
651 'nio' => __( 'Nicaraguan Córdoba', 'surecart' ),
652 'ngn' => __( 'Nigerian Naira', 'surecart' ),
653 'nok' => __( 'Norwegian Krone', 'surecart' ),
654 'pkr' => __( 'Pakistani Rupee', 'surecart' ),
655 'pab' => __( 'Panamanian Balboa', 'surecart' ),
656 'pgk' => __( 'Papua New Guinean Kina', 'surecart' ),
657 'pyg' => __( 'Paraguayan Guaraní', 'surecart' ),
658 'pen' => __( 'Peruvian Sol', 'surecart' ),
659 'php' => __( 'Philippine Peso', 'surecart' ),
660 'pln' => __( 'Polish Złoty', 'surecart' ),
661 'qar' => __( 'Qatari Riyal', 'surecart' ),
662 'ron' => __( 'Romanian Leu', 'surecart' ),
663 'rub' => __( 'Russian Ruble', 'surecart' ),
664 'rwf' => __( 'Rwandan Franc', 'surecart' ),
665 'shp' => __( 'Saint Helenian Pound', 'surecart' ),
666 'wst' => __( 'Samoan Tala', 'surecart' ),
667 'sar' => __( 'Saudi Riyal', 'surecart' ),
668 'rsd' => __( 'Serbian Dinar', 'surecart' ),
669 'scr' => __( 'Seychellois Rupee', 'surecart' ),
670 'sll' => __( 'Sierra Leonean Leone', 'surecart' ),
671 'sgd' => __( 'Singapore Dollar', 'surecart' ),
672 'sbd' => __( 'Solomon Islands Dollar', 'surecart' ),
673 'sos' => __( 'Somali Shilling', 'surecart' ),
674 'zar' => __( 'South African Rand', 'surecart' ),
675 'krw' => __( 'South Korean Won', 'surecart' ),
676 'lkr' => __( 'Sri Lankan Rupee', 'surecart' ),
677 'srd' => __( 'Surinamese Dollar', 'surecart' ),
678 'szl' => __( 'Swazi Lilangeni', 'surecart' ),
679 'sek' => __( 'Swedish Krona', 'surecart' ),
680 'chf' => __( 'Swiss Franc', 'surecart' ),
681 'std' => __( 'São Tomé and Príncipe Dobra', 'surecart' ),
682 'tjs' => __( 'Tajikistani Somoni', 'surecart' ),
683 'tzs' => __( 'Tanzanian Shilling', 'surecart' ),
684 'thb' => __( 'Thai Baht', 'surecart' ),
685 'top' => __( 'Tongan Paʻanga', 'surecart' ),
686 'ttd' => __( 'Trinidad and Tobago Dollar', 'surecart' ),
687 'try' => __( 'Turkish Lira', 'surecart' ),
688 'ugx' => __( 'Ugandan Shilling', 'surecart' ),
689 'uah' => __( 'Ukrainian Hryvnia', 'surecart' ),
690 'aed' => __( 'United Arab Emirates Dirham', 'surecart' ),
691 'usd' => __( 'United States Dollar', 'surecart' ),
692 'uyu' => __( 'Uruguayan Peso', 'surecart' ),
693 'uzs' => __( 'Uzbekistan Som', 'surecart' ),
694 'vuv' => __( 'Vanuatu Vatu', 'surecart' ),
695 'vnd' => __( 'Vietnamese Đồng', 'surecart' ),
696 'xof' => __( 'West African Cfa Franc', 'surecart' ),
697 'yer' => __( 'Yemeni Rial', 'surecart' ),
698 'zmw' => __( 'Zambian Kwacha', 'surecart' ),
699 'tnd' => __( 'Tunisian Dinar', 'surecart' ),
700 ];
701 }
702
703 /**
704 * Determine if this is a zero decimal currency.
705 *
706 * @param string $currency The currency code.
707 *
708 * @return bool
709 */
710 public static function isZeroDecimal( $currency ) {
711 return in_array( strtoupper( $currency ), self::getZeroDecicalCurrencies(), true );
712 }
713
714 /**
715 * Convery product amount.
716 *
717 * @param int $amount The Amount.
718 * @param string $currency The Currency.
719 *
720 * @return int
721 */
722 public static function maybeConvertAmount( $amount, $currency = null ) {
723 if ( ! $currency ) {
724 $currency = \SureCart::account()->currency;
725 }
726 return self::isZeroDecimal( (string) $currency ) ? $amount : $amount / 100;
727 }
728 }
729