Создание подписки на полосу Python - PullRequest
0 голосов
/ 21 мая 2019

Я пытаюсь создать полосовую подписку, а затем выставить счет клиенту за эту подписку.Мои транзакции с полосами отображаются на панели инструментов как «незавершенные», потому что они не оплачиваются.

Front-end создает токен, используя stripe.js, успешно используя предварительно созданную форму кредитной карты в виде полосок, но я не уверен, что мой внутренний код Python для создания подписки и ее оплатыправильно ..

Для подписки плата должна быть немедленной:

"collection_method": "charge_automatics",

...
    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")

...

1 Ответ

1 голос
/ 21 мая 2019

Похоже, у вас нет Карты, прикрепленной к вашему Клиенту, поэтому подписка не оплачивается при ее создании!

Если вы уже создали своего клиента, вы должны сделать что-то вроде этого:

# 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
...