Javascript 如何创建多个项目的付款和收费
我正在按照Stripe文档中的示例构建与Stripe的集成,但我不理解为多个产品创建费用的部分 我搜索了Stripe的所有文档,并搜索了关于类似问题的任何文章/论坛,但找不到任何内容。我非常感谢关于这件事的文章的链接或任何帮助我理解如何解决它的提示 以下是服务器端代码:Javascript 如何创建多个项目的付款和收费,javascript,python,html,flask,stripe-payments,Javascript,Python,Html,Flask,Stripe Payments,我正在按照Stripe文档中的示例构建与Stripe的集成,但我不理解为多个产品创建费用的部分 我搜索了Stripe的所有文档,并搜索了关于类似问题的任何文章/论坛,但找不到任何内容。我非常感谢关于这件事的文章的链接或任何帮助我理解如何解决它的提示 以下是服务器端代码: ```python @app.route("/checkout", methods=["GET", "POST"]) def checkout(): if request.method == "POST":
```python
@app.route("/checkout", methods=["GET", "POST"])
def checkout():
if request.method == "POST":
# Process a JSON string with a checkout information:
# { item_id: item_quantity, ... }
# Build the SQL query based on it
items = {}
shopping_cart = request.form["cart_checkout"]
shopping_cart = shopping_cart.lstrip("{")
shopping_cart = shopping_cart.rstrip("}")
shopping_cart = shopping_cart.split(",")
sqlQuery = "SELECT * FROM Items WHERE item_id IN ("
for KeyValPair in shopping_cart:
Key = KeyValPair.split(":")[0]
Key = Key.strip('"')
sqlQuery = sqlQuery + Key + ","
Value = KeyValPair.split(":")[1]
items[Key] = Value
sqlQuery = sqlQuery.rstrip(",")
sqlQuery = sqlQuery + ") ORDER BY item_id ASC"
cart_items = sql_select(sqlQuery)
# Add a column about the quantity of items
for item in cart_items:
item["quantity"] = items[item["item_id"]]
# Build a Stripe checkout list
line_items_list = []
for item in cart_items:
line_item = {}
line_item["name"] = item["item_name"]
line_item["description"] = item["item_description"]
line_item["amount"] = item["price"]
line_item["currency"] = "usd"
line_item["quantity"] = item["quantity"]
line_items_list.append(dict(line_item))
stripe_session = stripe.checkout.Session.create(
submit_type="pay",
payment_method_types=["card"],
line_items=line_items_list,
success_url='https://example.com/success',
cancel_url='https://example.com/cancel',
)
return render_template("checkout.html",
stripe_id=stripe_session.id,
stripe_pk=stripe_keys["PUBLIC_KEY"])
return redirect("/")
```
下面是HTML模板的一部分:
```html
<form action="/checkout" method="post" id="form_checkout" onsubmit="return cart_info()"
...
<input type="hidden" name="cart_checkout" id="checkout_info" value="{{ cart_checkout }}">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ stripe_pk }}"
data-name="Company Name"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-description="A description of the product or service being purchased"
data-amount="999"
data-shipping-address="true"
data-zip-code="true"
data-allow-remember-me="true"
data-panel-label="Pay"
data-label="Checkout"
data-locale="auto">
</script>
</form>
```
然后我可以创建一个没有任何问题的成功测试费用,它与成功标签一起显示在我的条带的仪表板上。如果我使用stripe.checkout.Session.create,stripe dashboard会正确地创建一个关于我的结帐会话和所选项目列表的不完整记录,但我不知道如何从那里开始确定这些项目的费用。正如经常发生的那样,当我开始提问时,我最终会自己找到答案,哈哈。我有一个
“checkout.html”
模板,但它不起作用,并且没有显示错误,所以我认为我缺少了更多的代码,这一切都需要工作
碰巧,我所缺少的只是一行代码中的“”。下面是一个工作签出会话,添加了一点JavaScript:
{%extends“layout.html”%}
{%block title%}签出{%endblock%}
{%block head%}
{%endblock%}
{%block main%}
{%with messages=get\u flashed\u messages(with\u categories=true)%}
{%对于类别,消息中的消息%}
{{message}}
{%endfor%}
{%endwith%}
结账
结账
函数签出(){
var stripe=stripe(“{{stripe_pk}}”);
stripe.redirectToCheckout({
//从签出会话创建API响应生成id字段
//可用于此文件,因此您可以在此处将其作为参数提供
//而不是{{CHECKOUT\u SESSION\u ID}占位符。
会话ID:“{{CHECKOUT\u SESSION\u ID}”
}).然后(函数(结果){
//如果“redirectToCheckout”由于浏览器或网络而失败
//错误,向您的客户显示本地化的错误消息
//使用“result.error.message”。
document.getElementById(“result_msg”).innerHTML=result.error.message;
});
}
{%endblock%}
```python
@app.route('/charge', methods=['POST'])
def charge():
# Amount in cents
amount = 500
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Flask Charge'
)
return render_template('charge.html', amount=amount)
```