[英]Creating a Stripe Subscription Python
I am trying to create a stripe subscription and then charge the customer for that subscription. 我正在尝试创建条带订阅,然后向客户收取该订阅的费用。 My stripe transactions are showing as "incomplete" in the dashboard, because they are not being payed.
我的条纹交易在仪表板中显示为“未完成”,因为未付款。
The front-endend is creating a token using the stripe.js successfully using a pre-made stripe credit card form, but I am not sure if my back-end python code for creating the subscription and charging it is correct.. 前端正在使用预制的条纹信用卡表格成功地使用stripe.js创建令牌,但是我不确定用于创建订阅和收费的后端python代码是否正确。
The charge should be immediate for the subscription: 订阅应立即收费:
"collection_method": "charge_automatically",
“ collection_method”:“自动收费”,
...
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")
...
It sounds like you do not have a Card attached to your Customer, so the Subscription is not paid when you create it! 听起来您的客户没有附属卡,因此在创建订阅时不会支付订阅费用!
If you've already created your Customer, you should do something like this: 如果已经创建了客户,则应执行以下操作:
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.