【发布时间】: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