条纹表单返回缺少的参数

时间:2016-09-28 05:55:28

标签: javascript jquery stripe-payments

我的条带代码未生成要发送到服务器端代码以向用户收费的令牌,而是返回:

Missing required param: number.

我已经无数次阅读了我的代码,阅读了Stripe文档等。控制台中返回的唯一错误是Stripe的400请求错误,因为"缺少参数",甚至虽然它存在并使用数据条带。

表格的片段:

<input type="text" data-stripe="number" class="form-control" placeholder="Card Number">

JavaScript代码:

<script type="text/javascript" src="https://js.stripe.com/v2/">  </script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_6G4sPCuttf9bYhMhs0xM3JEo');
</script>

<script type="text/javascript">
$(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) {
// 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();
}
};

1 个答案:

答案 0 :(得分:1)

您无法仅通过卡号生成令牌。您还需要传递以下数据。

  1. 卡号
  2. CVC

  3. exp_month

  4. exp_year

  5. 您的表单应该从用户那里获取所有这些详细信息。

    <form action="/your-charge-code" method="POST" id="payment-form">
      <span class="payment-errors"></span>
    
      <div class="form-row">
        <label>
          <span>Card Number</span>
          <input type="text" size="20" data-stripe="number">
        </label>
      </div>
    
      <div class="form-row">
        <label>
          <span>Expiration (MM/YY)</span>
          <input type="text" size="2" data-stripe="exp_month">
        </label>
        <span> / </span>
        <input type="text" size="2" data-stripe="exp_year">
      </div>
    
      <div class="form-row">
        <label>
          <span>CVC</span>
          <input type="text" size="4" data-stripe="cvc">
        </label>
      </div>
      <input type="submit" class="submit" value="Submit Payment">
    </form>