python - 创建条带订阅 Python
问题描述
我正在尝试创建条带订阅,然后向客户收取该订阅的费用。我的条带交易在仪表板中显示为“不完整”,因为它们没有被支付。
前端正在使用 stripe.js 使用预制的条纹信用卡表格成功创建令牌,但我不确定用于创建订阅和收费的后端 python 代码是否正确..
订阅费用应立即收取:
"collection_method": "charge_automatically",
...
if request.method == "POST":
try:
token = request.POST['stripeToken']
#Create Stripe Subscription Charge
subscription = stripe.Subscription.create(
customer=user_membership.stripe_customer_id,
items=[
{
"plan": selected_membership.stripe_plan_id,
},
],
)
#Charge Stripe Subscription
charge = stripe.Charge.create(
amount=selected_membership.stripe_price,
currency="usd",
source=token, # obtained with Stripe.js
description=selected_membership.description,
receipt_email=email,
)
return redirect(reverse('memberships:update_transactions',
kwargs={
'subscription_id': subscription.id
}))
except stripe.error.CardError as e:
messages.info(request, "Oops, your card has been declined")
...
解决方案
听起来您的客户没有附加卡,因此在创建订阅时不会支付订阅费用!
如果您已经创建了客户,您应该执行以下操作:
# add the token to the customer
# https://stripe.com/docs/api/customers/update?lang=python
stripe.Customer.modify(
user_membership.stripe_customer_id, # cus_xxxyyyyz
source=token # tok_xxxx or src_xxxyyy
)
# create the subscription
subscription = stripe.Subscription.create(
customer=user_membership.stripe_customer_id,
items=[
{
"plan": selected_membership.stripe_plan_id,
},
])
# no need for a stripe.Charge.create, as stripe.Subscription.create will bill the subscription
推荐阅读
- javascript - 为什么下面的代码异步执行
- html - 导航栏背景颜色 css
- angular - 尝试将 ReactiveForms 添加到组件时出错`无法绑定到'formGroup'...`
- sql - 如何在 IBM DB2 中的导出结果中也包含列标题
- python - 如何在查找2个列表的差异时维护输出列表的顺序
- android - UnsatisfiedLinkError:没有找到 void mono.android.text.TextWatcherImplementor 的实现
- jquery - JQuery - 如果 div 文本包含存储在数组中的相同文本,则将该文本包装在新 div 中
- android - NDK 解析结果:项目设置:Gradle 模型版本=5.4.1,NDK 版本为 UNKNOWN
- javascript - 如何在 ASP.NET MVC 中创建具有预定义设置的 .xml 文件?
- linux - 使用 Yocto 构建 Linux 映像时,pathspec 'gnulib' 与 git 已知的任何文件都不匹配