#goatandsheep
1 messages · Page 1 of 1 (latest)
# 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?
Hi there, so you are using subscription schedule to upgrade/downgrade the subscription?
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
It looks like you are releasing the subscription schedule then modify the subscription directly again?
yeah haha!
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?
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?
OK, it looks like you are cancelling a subscription and creating a new subscription. It's not really a downgrade.
how can I schedule a downgrade?
You can use from_schedule param to migrate an existing subscription to be managed by a subscription schedule (https://stripe.com/docs/api/subscription_schedules/create#create_subscription_schedule-from_subscription), and use the schedule to manage the downgrade (you can refer to example code here, but instead of creating a new schedule, you should just update the newly created schedule https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#downgrading-subscriptions )
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
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
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'
)
wont cancel_at_period_end cancel the whole subscription?
and I'm reading it says under changing price https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#changing
However, switching a customer from a monthly subscription to a yearly subscription moves the billing date to the date of the switch.
If someone is moving from high-tier monthly to low-tier yearly, will that happen right away then?
Ah yes, you'll need to remove the cancel_at_period_end param
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}],
)
Yes, and don't forget the proration_behavior='none'. The limitation here is this only works if the new price is having the same billing prioid as the old price, otherwise the customer will be billed immediately during the update (https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#immediate-payment)
oh okay. so if they have different billing periods, then is it still possible to do without a schedule?
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.
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,
)
You can't set both from_subscription and phases at the same time
okay so if I take that out it should do the same thing?
So two API calls
- to create a schedule with
from_subscription - to update the schedule with the
phases
is that because if I don't create it from_subscription, it won't stop the previous 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.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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
Setting always_invoice will invoice the customer immediately. If this is not the behaviour you desire, you can change it to create_proration