Skip to content
Snippets Groups Projects
Commit b2114a2b authored by mattwire's avatar mattwire
Browse files

Fix #126 setting user locale for stripe elements

parent bc18062a
No related branches found
No related tags found
No related merge requests found
......@@ -386,7 +386,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
'publishableKey' => CRM_Core_Payment_Stripe::getPublicKeyById($form->_paymentProcessor['id']),
'jsDebug' => (boolean) \Civi::settings()->get('stripe_jsdebug'),
'paymentProcessorTypeID' => $form->_paymentProcessor['payment_processor_type_id'],
'locale' => CRM_Core_I18n::getLocale(),
'locale' => CRM_Stripe_Api::mapCiviCRMLocaleToStripeLocale(),
'apiVersion' => CRM_Stripe_Check::API_VERSION,
'csrfToken' => class_exists('\Civi\Firewall\Firewall') ? \Civi\Firewall\Firewall::getCSRFToken() : NULL,
'country' => \Civi::settings()->get('stripe_country'),
......
......@@ -208,4 +208,37 @@ class CRM_Stripe_Api {
$amount = round($amount, CRM_Utils_Money::getCurrencyPrecision($currency));
return $amount;
}
/**
* We have to map CiviCRM locales to a specific set of Stripe locales for elements to set the user language correctly.
* Reference: https://stripe.com/docs/js/appendix/supported_locales
* @param string $civiCRMLocale (eg. en_GB).
*
* @return string
*/
public static function mapCiviCRMLocaleToStripeLocale($civiCRMLocale = '') {
if (empty($civiCRMLocale)) {
$civiCRMLocale = CRM_Core_I18n::getLocale();
}
$localeMap = [
'en_AU' => 'en',
'en_CA' => 'en',
'en_GB' => 'en-GB',
'en_US' => 'en',
'es_ES' => 'es',
'es_MX' => 'es-419',
'es_PR' => 'es-419',
'fr_FR' => 'fr',
'fr_CA' => 'fr-CA',
'pt_BR' => 'pt-BR',
'pt_PT' => 'pt',
'zh_CN' => 'zh',
'zh_HK' => 'zh-HK',
'zh_TW' => 'zh-TW'
];
if (array_key_exists($civiCRMLocale, $localeMap)) {
return $localeMap[$civiCRMLocale];
}
return 'auto';
}
}
......@@ -396,6 +396,7 @@
debugging('New Stripe ID: ' + CRM.vars.stripe.id + ' pubKey: ' + CRM.vars.stripe.publishableKey);
stripe = Stripe(CRM.vars.stripe.publishableKey);
debugging('locale: ' + CRM.vars.stripe.locale);
var stripeElements = stripe.elements({locale: CRM.vars.stripe.locale});
// By default we load paymentRequest button if we can, fallback to card element
......
......@@ -167,7 +167,7 @@ function stripe_civicrm_buildForm($formName, &$form) {
$jsVars = [
'id' => $form->_paymentProcessor['id'],
'publishableKey' => CRM_Core_Payment_Stripe::getPublicKeyById($form->_paymentProcessor['id']),
'locale' => CRM_Core_I18n::getLocale(),
'locale' => CRM_Stripe_Api::mapCiviCRMLocaleToStripeLocale(),
'apiVersion' => CRM_Stripe_Check::API_VERSION,
'jsDebug' => (boolean) \Civi::settings()->get('stripe_jsdebug'),
'csrfToken' => class_exists('\Civi\Firewall\Firewall') ? \Civi\Firewall\Firewall::getCSRFToken() : NULL,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment