Python 请求/答复方法
我在Stripe.com上创建了一个简单的表单和一个计划,效果很好。我可以在开发模式下订阅该计划,它反映了条带上的付款。现在,我需要与Stripe站点实时同步,以获取计划到期和其他事件。我知道,为此,我需要创建一个回调函数来获取条带id并将其保存到数据库中。我更喜欢将其保存到数据库,而不是会话。请告知如何创建回调路由并将值从JSON api保存到数据库。下面是我订阅的代码,我需要显示过期和其他事件Python 请求/答复方法,python,flask,stripe-payments,Python,Flask,Stripe Payments,我在Stripe.com上创建了一个简单的表单和一个计划,效果很好。我可以在开发模式下订阅该计划,它反映了条带上的付款。现在,我需要与Stripe站点实时同步,以获取计划到期和其他事件。我知道,为此,我需要创建一个回调函数来获取条带id并将其保存到数据库中。我更喜欢将其保存到数据库,而不是会话。请告知如何创建回调路由并将值从JSON api保存到数据库。下面是我订阅的代码,我需要显示过期和其他事件 def yearly_charged(): #Need to save customer
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>
需要帮助设计以下函数,以便我可以设置方法,以适当的方式获得响应,以保存在数据库中
@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对象中提取条带id、到期事件和其他订阅通知,以保存在Flask模型中。首先,请注意,您共享的代码只是一个订阅,而不是一个订阅(即经常性费用)。如果要自动创建定期费用,则应查看 如果我正确理解您的问题,您希望使用以获得成功订阅付款的通知。将为每次成功付款创建一个事件。(有关订阅事件的更多信息,请参阅。) 对于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
谢谢你,你帮了我大忙。
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