#goatandsheep

1 messages · Page 1 of 1 (latest)

clear waveBOT
wheat bronze
#
# upgrade
stripe.SubscriptionSchedule.release(sid=stripe_subscription.schedule)
updated_subscription = stripe_subscription.modify(
    sid=stripe_subscription.id,
    cancel_at_period_end=False,
    proration_behavior='always_invoice',
    items=[{
        'id': stripe_subscription['items']['data'][0].id,
        'price': new_price_id,
    }]
)
#

how can we combine the SubscriptionSchedule.release and Subscription.modify commands?

torn vault
#

Hi there, so you are using subscription schedule to upgrade/downgrade the subscription?

wheat bronze
#

yes. The above code is working for us but it is taking a long time so I'm looking to see if we can combine the release and modify commands

#

i see a number of options like using from_subscription

#

or phases, but I'm not sure what the right course of action is because I don't understand the attributes

#

oh i realized I included the wrong code. updated

torn vault
#

It looks like you are releasing the subscription schedule then modify the subscription directly again?

wheat bronze
#

yeah haha!

torn vault
#

I don't understand why you do that, I thought you want to schedule the downgrade (through subscription schedule I assume) at the end of the billing cycle?

wheat bronze
#

so we're trying to prorate for upgrade (above) and schedule for downgrades. I'll include downgrade code

#
# downgrade
        stripe.Subscription.modify(
            sid=subscription.stripe_id,
            cancel_at_period_end=True,
        )

        subscription_schedule = stripe.SubscriptionSchedule.create(
            customer=stripe_subscription.customer,
            start_date=stripe_subscription.current_period_end,
            end_behavior="release",
            phases=[
                {
                    "items": [{"price": new_price_id}],
                },
            ],
        )
#

I am trying to merge the two commands into one command each, (I'm guessing using stripe.SubscriptionSchedule.create ) for both. Is this possible?

torn vault
#

OK, it looks like you are cancelling a subscription and creating a new subscription. It's not really a downgrade.

wheat bronze
#

how can I schedule a downgrade?

torn vault
#

Alternatively, if the purpose is just to disable proration when downgrading, you can just set proration_behavior to none when updating the subscription, so that your customers are billed the full amount at the new price when the next invoice is generated.

I believe this is a much easier solution

wheat bronze
#

no proration is perfect 👍🏽

#

so downgrade like this?

        # stripe.Subscription.modify(
        #     sid=subscription.stripe_id,
        #     cancel_at_period_end=True,
        # )
        subscription_schedule = stripe.SubscriptionSchedule.create(
            customer=stripe_subscription.customer,
            start_date=stripe_subscription.current_period_end,
            end_behavior="release",
            phases=[
                {
                    "items": [{"price": new_price_id}],
                },
            ],
            from_subscription=subscription.stripe_id,
        )
#

do i need to add the previous part in phases? I've commented out the modify

torn vault
#

You don't need to use Schedule API, just update on the subscription object directly

stripe.Subscription.modify(
            sid=subscription.stripe_id,
            cancel_at_period_end=True,
            proration_behavior='none'
        )
wheat bronze
#

wont cancel_at_period_end cancel the whole subscription?

torn vault
#

Ah yes, you'll need to remove the cancel_at_period_end param

wheat bronze
#

so all i need to do is this and it does the same as all the downgrade code of before? i.e. changes price at end of cycle even if both have different intervals?

        stripe.Subscription.modify(
            sid=subscription.stripe_id,
            items=[{"price": new_price_id}],
        )
torn vault
wheat bronze
#

oh okay. so if they have different billing periods, then is it still possible to do without a schedule?

torn vault
#

By default, switching a customer from a subscription to a subscription of different billing period moves the billing date to the date of the switch. So you'll need to use schedule if you want the billing date to be set differently.

wheat bronze
#

hm... so would this do it then?

        subscription_schedule = stripe.SubscriptionSchedule.create(
            customer=stripe_subscription.customer,
            start_date='now',
            end_behavior="release",
            phases=[
                {
                    'items': [{"price": stripe_subscription.items.data[0].price.id}],
                    'end_date': valid_until,
                },
                {
                    "items": [{"price": new_price_id}],
                },
            ],
            # from_subscription=subscription.stripe_id,
        )
torn vault
#

You can't set both from_subscription and phases at the same time

wheat bronze
#

okay so if I take that out it should do the same thing?

torn vault
#

So two API calls

  1. to create a schedule with from_subscription
  2. to update the schedule with the phases
wheat bronze
#

is that because if I don't create it from_subscription, it won't stop the previous subscription?

torn vault
#

https://stripe.com/docs/api/subscription_schedules/create?lang=java#create_subscription_schedule-from_subscription

Migrate an existing subscription to be managed by a subscription schedule. If this parameter is set, a subscription schedule will be created using the subscription’s item(s), set to auto-renew using the subscription’s interval. When using this parameter, other parameters (such as phase values) cannot be set. To create a subscription schedule with other modifications, we recommend making two separate API calls.

wheat bronze
#

okay let's give up on downgrade then for now. for upgrade we have:

        if stripe_subscription.schedule:
            stripe.SubscriptionSchedule.release(sid=stripe_subscription.schedule)

        updated_subscription = stripe_subscription.modify(
            sid=stripe_subscription.id,
            cancel_at_period_end=False,
            proration_behavior='always_invoice',
            items=[{
                'id': stripe_subscription['items']['data'][0].id,
                'price': new_price_id,
            }]
        )
#

for upgrade we have to always prorate

torn vault
#

Setting always_invoice will invoice the customer immediately. If this is not the behaviour you desire, you can change it to create_proration