gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:11:16 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

实现此目的的一种方法可能是使用表单,将 Stripe token 添加到该表单中的隐藏输入,然后使用 Javascript 提交。

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

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

关于javascript - 如何创建可变数量的 Stripe 订阅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com