gpt4 book ai didi

python - 创建 Stripe 订阅 Python

转载 作者:行者123 更新时间:2023-12-01 00:56:10 26 4
gpt4 key购买 nike

我正在尝试创建 strip 订阅,然后向客户收取该订阅的费用。我的 Stripe 交易在仪表板中显示为“不完整”,因为它们没有被支付。

前端正在使用 stripe.js 使用预制的 stripe 信用卡表单成功创建 token ,但我不确定用于创建订阅和收费的后端 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

关于python - 创建 Stripe 订阅 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56242327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com