2016-08-19 67 views
0

我正在使用條帶和API中的「自定義窗體」。以下代碼在英文中出現錯誤時會拋出錯誤,但我想將一些錯誤消息翻譯成挪威語,以便讓我的客戶更加友好。例如當前爲英文的「invalid_expiry_year」和「invalid_expiry_month」。使用JavaScript自定義條帶錯誤消息

是否有可能實現,如果有,如何實現?

<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 
<script type="text/javascript"> 
    Stripe.setPublishableKey('pk_test_2iA9ERjj5lVuUgvOS9W5fNtV'); 
    $(function() { 
     var $form = $('#payment-form'); 
     $form.submit(function(event) { 
     // Disable the submit button to prevent repeated clicks: 
     $form.find('.submit').prop('disabled', true); 

     // Request a token from Stripe: 
     Stripe.card.createToken($form, stripeResponseHandler); 

     // Prevent the form from being submitted: 
     return false; 
     }); 
    }); 
    function stripeResponseHandler(status, response) { 


    function stripeHandler(status, response){ 
     if (response.error && response.error.type == 'card_error'){ 
     $('.errors').text(errorMessages[ response.error.code ]); 
     } 

     else { 
     // do other stuff (and handle api/request errors) 
     } 
    } 
     // Grab the form: 
     var $form = $('#payment-form'); 

     if (response.error) { // Problem 

     // Show the errors on the form: 
     $form.find('.payment-errors').text(response.error.message); 
     $form.find('.submit').prop('disabled', false); // Re-enable submission 

     } else { // Token was created! 

     // Get the token ID: 
     var token = response.id; 

     // Insert the token ID into the form so it gets submitted to the server: 
     $form.append($('<input type="hidden" name="stripeToken">').val(token)); 

     // Submit the form: 
     $form.get(0).submit(); 
     } 
    }; 
</script> 

回答

0

沒有爲您設置的條紋狀在不同的語言不是英語的選擇,但只有少數其他languages Stripe supports。對於定製集成,您在致電StripeCheckout.configure()時必須通過locale: 'auto',以便自動檢測語言。 docs有更多信息。

但是,由於挪威語尚未得到支持,我所建議的是映射響應代碼並提供您自己的錯誤翻譯。

var errorMessages = { 
    incorrect_number: "The card number is incorrect.", 
    .... 
}; 

你可以找到所有的錯誤代碼的完整列表here