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

问题描述 投票:0回答:1

现在我正在成功创建一个具有硬编码数量的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">

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

javascript flask stripe-payments
1个回答
1
投票

你如何向你的后端服务器提交令牌(即你如何实现token功能)?完成后,您只需以相同的方式提交数量即可。

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

这是一个例子:https://jsfiddle.net/53u96kvw/

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

© www.soinside.com 2019 - 2024. All rights reserved.