【问题标题】:Creating a Stripe Subscription Python创建条带订阅 Python
【发布时间】:2019-05-21 16:06:23
【问题描述】:

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

前端正在使用 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")

...

【问题讨论】:

  • 你用的是哪张测试卡?似乎给定的卡片被划定了。试试4242424242424242

标签: python django stripe-payments


【解决方案1】:

听起来您的客户没有附加卡,因此在创建订阅时未支付订阅费用!

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

# 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

【讨论】:

  • 我已经创建了客户,所以这是正确的做法。谢谢!
  • 这比 Stripe 支持更好!
  • 很高兴这对您有所帮助! :) 对于代码 qs,Freenode (IRC) 上的#stripe 频道非常好,很多开发人员都在那里闲逛。
  • 我添加了我最终使用的修复程序,而是使用了检索。您对此有何看法而不是修改?
  • 两者都可以,但是retrieve方法使用了两个api请求(取回对象,调用save),修改一下就搞定了
猜你喜欢
  • 2014-07-11
  • 2019-07-07
  • 2021-12-03
  • 2021-05-30
  • 2018-09-20
  • 2023-03-07
  • 2016-09-05
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多