【问题标题】:Stripe Flask request/response methodologyStripe Flask 请求/响应方法
【发布时间】:2017-02-19 21:14:13
【问题描述】:

我在 Stripe.com 创建了一个 Flask 简单表单并创建了一个计划,它工作得很好。我可以在开发模式下订阅计划,它反映了 Stripe 上的付款。现在进一步我需要与 Stripe 站点实时同步以获取计划到期和其他事件。我知道为此目的,我需要创建一个回调函数来获取条带 id 并将其保存到数据库中。我宁愿将它保存到数据库而不是会话。请告知如何创建回调路由并将值从 JSON api 保存到数据库。以下是我的订阅代码,我需要显示过期和其他事件。

def yearly_charged():
    #Need to save customer stripe ID to DB model  
    amount = 1450

    customer = stripe.Customer.create(
        email='test@test.com',
        source=request.form['stripeToken']
    )
    try:
        charge = stripe.Charge.create(
            customer=customer.id,
            capture='true',
            amount=amount,
            currency='usd',
            description='standard',
        )
        data="$" + str(float(amount) / 100) + " " + charge.currency.upper()
    except stripe.error.CardError as e:
        # The card has been declined
        body = e.json_body
        err = body['error']
        print
        "Status is: %s" % e.http_status
        print
        "Type is: %s" % err['type']
        print
        "Code is: %s" % err['code']
        print
        "Message is: %s" % err['message']


    return render_template('/profile/charge.html', data=data, charge=charge)

模板:

<form action="/charged" method="post">
            <div class="form-group">
                <label for="email">Amount is 14.95 USD </label>
                <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="{{ key }}"
                    data-description="Yearly recurring billing"
                    data-name="yearly"
                    data-amount="1495"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-locale="auto">
                </script>
            </div>
        </form>

型号:

class Students(db.Model):
    __tablename__='students'
    .......
    student_strip_id = db.Column(db.String(45))
    .......

需要帮助来设计以下功能,以便我可以设置方法以正确的方式获取响应以保存在数据库中。

@app.route('/oauth/callback/, methods=['POST'])
    # This is where I guess I have to define callback function to get API data
    return redirect('/')

这里的目的是从 Stripe API 对象中提取 Stripe id、到期事件和其他订阅通知,以保存在 Flask Model 中。

【问题讨论】:

    标签: python flask stripe-payments


    【解决方案1】:

    首先,请注意您共享的代码只是creates a one-off charge,而不是订阅(即经常性费用)。如果您想创建自动重复收费,您应该查看documentation for subscriptions

    如果我正确理解您的问题,您希望使用webhooks 收到订阅付款成功的通知。每次成功付款都会创建一个invoice.payment_succeeded 事件。 (有关订阅事件的更多信息,请参阅here。)

    使用 Flask,webhook 处理程序看起来类似于:

    import json
    import stripe
    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/webhook', methods=['POST'])
    def webhook():
        event_json = json.loads(request.data)
        event = stripe.Event.retrieve(event_json['id'])
    
        if event.type == 'invoice.payment_succeeded':
            invoice = event.data.object
            # Do something with invoice
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多