为什么我的表单没有提交?

时间:2014-03-06 02:37:22

标签: python django stripe-payments

我正在使用Stripe checkout和自定义按钮,并且在成功返回令牌后无法让它提交我的表单。

这是我的HTML:

<form id="payment-form" action="{% url 'shipment:confirm' %}" method="POST">
{% csrf_token %}
</form>

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<button id="customButton">Join BossBox</button>

<script>
  var handler = StripeCheckout.configure({
    key: 'secrettestkey (I redacted this)',
    image: '/static/shipment/images/logo.png',
    email: "{{ email_address }}",
    token: function(token, args) {
        // get the form by it's ID, you would have to add the id="payment-form" to your HTML
        var $form = $("#payment-form");
        // token contains id, last4, and card type
        var token = response.id;
        // insert the token into the form so it gets submitted to the server
        $form.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
        // and submit
        $form.get(0).submit();
    }
  });

  document.getElementById('customButton').addEventListener('click', function(e) {
    // Open Checkout with further options
    handler.open({
      name: 'MyCompany',
      description: 'Monthly Membership ($29.00)',
      amount: 2900,
    });
    e.preventDefault();
  });
</script>

在Chrome开发者工具中,这是我通过Stripe Checkout成功提交测试凭据后出现的错误:

Uncaught ReferenceError: response is not defined (index):120
StripeCheckout.configure.token (index):120
onToken checkout.js:3
rpc.methods.setToken checkout.js:4
e.message checkout.js:3
(anonymous function)"

1 个答案:

答案 0 :(得分:1)

您对令牌处理函数的第一个参数是token。您可以在token.id中访问令牌ID,而不是response.id。您可以在条带文档中查看自定义Checkout集成的示例:

(我认为文档中还有其他代码使用response作为参数,这可能是混淆的来源。)

希望有所帮助, 拉里

PS我在Stripe工作。