london_utku
london_utku

Reputation: 1312

How to change subscription from PlanA to PlanB using Stripe with Python/Django

I can create a Stripe subscription for a customer using the following code :

            subscription = stripe.Subscription.create(
                customer=stripe_customer_id,
                items=[
                    { "plan": stripe_plan_A },
                ]
            )

Here is what I see on Stripe Dashboard.

enter image description here

I want to change the upgrade the subscription to stripe_plan_B but while keeping rest of the configuration same.

I have experimented with the following code to change the plan from stripe_plan_A to stripe_plan_B; however this following command results in having two subscriptions at the same time, rather than a single subscription.

                stripe.Subscription.modify(
                    subscription.id,
                    items=[
                        { "plan": selected_membership.stripe_plan_B },
                    ]
                )

As you can see below, we can have two subscriptions at the same time for the same user. (Stripe docs say you can have n number of subscriptions)

enter image description here

Is there any suggestion for this, so I can fluently change between the plans ?

Upvotes: 0

Views: 594

Answers (2)

london_utku
london_utku

Reputation: 1312

I have found the following answer in Stripe's page enter link description here:

subscription = stripe.Subscription.retrieve('sub...')

stripe.Subscription.modify(
  subscription.id,
  cancel_at_period_end=False,
  proration_behavior='create_prorations',
  items=[{
    'id': subscription['items']['data'][0].id,
    'price': 'price_...',
  }]
)

Upvotes: 0

yuting
yuting

Reputation: 1694

Price replaces Plan and Price is the recommended for creating a subscription. I'd recommend refering to the migration Price to Plan migration here: https://stripe.com/docs/billing/migration/migrating-prices

You can update the subscription directly with subscription item ID. By setting items[0][id] you are indicating that you want to update an existing SubscriptionItem, and can update it to use a different price. For example,

sub = stripe.Subscription.modify(
  sub.id, 
  items=[
    {"id": {{SUB_ITEM_ID}}, "price": {{PRICE_B_ID}} },
  ],
)

Alternatively, you can also delete the existing SubscriptionItem with deleted parameter to true and add the new SubscriptionItem. For example,

sub = stripe.Subscription.modify(
  sub.id, 
  items=[
    {"id": {{SUB_ITEM_ID}}, "deleted": "true" },
    {"price": {{PRICE_B_ID}} },
  ],
)

Upvotes: 1

Related Questions