创建条带订阅Python

2024-04-29 08:24:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个条带订阅,然后向客户收取该订阅的费用。我的条带交易在仪表板上显示为“未完成”,因为它们没有付款。在

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

...

Tags: idrequestjspostmethodsubscriptionstripemembership
1条回答
网友
1楼 · 发布于 2024-04-29 08:24:59

听起来你的客户没有附加卡,所以在创建订阅时不会支付订阅费!在

如果您已经创建了客户,您应该执行以下操作:

# 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

相关问题 更多 >