/** * Currencies accepted by the PayPal Orders API. * See https://developer.paypal.com/api/rest/reference/currency-codes/ */ const PAYPAL_SUPPORTED_CURRENCIES = new Set([ "AUD", "BRL", "CAD", "CHF", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "ILS", "JPY", "MXN", "MYR", "NOK", "NZD", "PHP", "PLN", "SEK", "SGD", "THB", "TWD", "USD", ]) type CurrencyCheck = { currency: string overrideCurrency?: string supported: boolean errors: string[] } /** Trim + upper-case an ISO 4217 code, substituting `fallback` when blank. */ export function normalizeCurrencyCode(code?: string, fallback = "EUR") { const trimmed = String(code || "").trim() return (trimmed || fallback).toUpperCase() } /** Whether PayPal accepts orders in this currency. */ export function isPayPalCurrencySupported(currencyCode: string) { return PAYPAL_SUPPORTED_CURRENCIES.has(normalizeCurrencyCode(currencyCode)) } /** * Check a cart currency against PayPal support and, when configured, the * merchant's PayPal currency override. Returns every problem found so the * storefront can show an actionable message rather than a bare failure. */ export function getPayPalCurrencyCompatibility(input: { currencyCode?: string paypalCurrencyOverride?: string }): CurrencyCheck { const currency = normalizeCurrencyCode(input.currencyCode) const overrideCurrency = input.paypalCurrencyOverride ? normalizeCurrencyCode(input.paypalCurrencyOverride) : undefined const errors: string[] = [] if (!isPayPalCurrencySupported(currency)) { errors.push(`PayPal does not support currency "${currency}".`) } if (overrideCurrency && overrideCurrency !== currency) { errors.push( `PayPal is configured for "${overrideCurrency}", but the store cart uses "${currency}".` ) } return { currency, overrideCurrency, supported: errors.length === 0, errors, } } /** `getPayPalCurrencyCompatibility`, but throws when incompatible. */ export function assertPayPalCurrencySupported(input: { currencyCode?: string paypalCurrencyOverride?: string }) { const result = getPayPalCurrencyCompatibility(input) if (!result.supported) { throw new Error(result.errors.join(" ")) } return result } /** The supported currency codes as an array (for API responses). */ export function getPayPalSupportedCurrencies() { return Array.from(PAYPAL_SUPPORTED_CURRENCIES.values()) }