【问题标题】:Customize Stripe error messages with JavaScript使用 JavaScript 自定义 Stripe 错误消息
【发布时间】:2016-08-22 16:43:14
【问题描述】:

我正在使用 API 中的 Stripe 和“自定义表单”。如果出现错误,下面的代码会抛出错误,用英语表示,但我想将一些错误消息翻译成挪威语,以使其对我的客户更加用户友好。例如当前为英文的“invalid_expiry_year”和“invalid_expiry_month”。

是否有可能实现?如果可以,如何实现?

<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>

【问题讨论】:

    标签: javascript stripe-payments


    【解决方案1】:

    您可以选择使用与英语不同的语言设置条带表单,但仅限于其他少数languages Stripe supports。对于自定义集成,您必须在调用 StripeCheckout.configure() 时传递 locale: 'auto',以便自动检测语言。 docs 中有更多信息。

    但是,由于尚不支持挪威语,我建议映射响应代码并为错误提供您自己的翻译。

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

    您可以找到包含所有错误代码here的完整列表

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 1970-01-01
      • 2016-12-14
      • 2015-09-22
      • 2016-09-03
      • 1970-01-01
      • 2015-09-21
      • 2011-04-27
      • 2018-09-02
      相关资源
      最近更新 更多