Flask Stripe Checkout动态价格SessionID错误

问题描述

我一直在使用https://stripe.com/docs/payments/checkout/migrating-prices#server-side-code-for-inline-itemshttps://stripe.com/docs/payments/checkout/accept-a-payment以及其他指南/ SO帖子作为指南。

payments.html页面中的结帐按钮不起作用;页面上没有任何内容。
在Flask的/ payment函数中,我可以很好地打印出session / session.id。
我尝试过将会话和session.id传递到payments.html页面,但是都没有用。 Payments.html上的{{price}}工作正常。

要让“结帐”页面正常运行,我在这里要俯视什么?

base.html

<!DOCTYPE html>
<html>
  <head>
    <title>Stripe Checkout</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='main.css') }}">
    <link rel="stylesheet" href="{{ url_for('static',filename='bootstrap.min.css') }}">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
  </head>
  <body>
    <div class="container">
      {% block content %}
      {% endblock %}
    </div>
    {% block scripts %}
    {% endblock %}
  </body>
</html>

Payments.html

{% extends "base.html" %}

{% block content %}
  <div>
    <h1>Cost</h1>
    <h2>Pay ${{price}}</h2>
    <button type="button" class="btn btn-primary" id="checkout">Pay with Card</button>
    <button type="button" class="btn btn-primary" id="back">Back</button>
    
  </div>
  <div id="status">
    <br>
    <div class="alert alert-primary" id="alert" role="alert"></div>
  </div>
{% endblock %}

{% block scripts %}
  <script src="https://js.stripe.com/v3"></script>
  <script>
    var stripe = Stripe('pk_test_...');

    var checkoutButton = document.querySelector('#checkout');
    checkoutButton.addEventListener('click',function () {
      stripe.redirectToCheckout({
        sessionId: {{sessionID}}
      });
    });
  </script>
{% endblock %}

烧瓶的付款功能

@app.route("/payment")
def payment():
    price = "50"
    session = session.Session().getSession(int(price)*100)
    sessionID = session.id
    print("SessionID:",sessionID)
    return render_template("payment.html",sessionID=sessionID,price=price)

Python Stripe文件

import stripe

stripe.api_key = 'sk_test_...'

class Session:
  
    def __init__(self):
        pass
  
    def getSession(self,price):
        session = stripe.checkout.Session.create(
          payment_method_types=['card'],line_items=[{
            'price_data': {
              'currency': 'usd','product_data': {
                'name': 'blah',},'unit_amount': price,'quantity': 1,}],mode='payment',success_url='https://example.com/success',cancel_url='https://example.com/cancel',)
      
        return session

解决方法

由于@floatingLomas,我不认为使用浏览器的控制台。是的,我知道了,我必须使用session.id,并在{{sessionID}}周围加上引号。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...