如何创建数量可变的Stripe订阅?

时间:2019-02-15 23:28:48

标签: javascript flask stripe-payments

现在,我已成功创建具有硬编码数量的 Stripe 订阅:

customer = stripe.Customer.create(email=request.form['stripeEmail'], source=request.form['stripeToken'])

# For the moment, change the quantity parameter for each customer
subscription = stripe.Subscription.create(
    customer = customer.id,
    items = [
        {
            'plan': 'plan_*************',
            'quantity': 7,
        },
    ],
)

想法是从前端获取quantity的值。我已经有一个选择器,可以通过编程方式设置数量值,实际上我正在使用它在 Stripe Checkout中打印可变数量:

<script>
  var handler = StripeCheckout.configure({
    key: "{{pub_key}}",
    image: "https://stripe.com/img/documentation/checkout/marketplace.png",
    locale: "auto",
    zipCode: true,
    token: function(token) {
      // You can access the token ID with `token.id`.
      // Get the token ID to your server-side code for use.
    }
  });

  document.getElementById("customButton").addEventListener("click", function(e) {
    var quantity = document.getElementsByName("selectedQuantity")[0];
    var text = quantity.options[quantity.selectedIndex].text;
    // Open Checkout with further options:
    handler.open({
      name: "Company, Inc.",
      description: text + " Subscriptions",
      amount: 900 * text
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation:
  window.addEventListener("popstate", function() {
    handler.close();
  });
</script>

请注意,我正在从后端获取公钥,但是我不知道如何从后端的前端检索数量值。

我该怎么做?

编辑:

当我不使用自定义Checkout集成时,我有一个可以执行此操作的表单:

<form action="{{ url_for('pay') }}" method="POST">

但是这里没有表格,所以我不确定下一步应该是什么。

1 个答案:

答案 0 :(得分:1)

您如何将令牌提交到后端服务器(即,如何实现token函数)?完成此操作后,您可以采用相同的方式提交数量。

一种方法是使用表单,将Stripe令牌添加到该表单中的隐藏输入中,然后使用Javascript提交。

下面是一个示例:https://jsfiddle.net/53u96kvw/

或者,如果您根本没有表单,则可以发出具有相同信息的AJAX请求:https://jsfiddle.net/kcp12o3z/

相关问题