To immediately charge your users the prorated price difference when the user upgrades, use the proration_behavior='always_invoice'
option when modifying the user's subscription. Quoting from the docs:
... In order to always invoice immediately for prorations, pass always_invoice.
Python example:
stripe.Subscription.modify(
subscription_id,
items=[{
'id': item_id,
'plan': plan_id,
}],
# Charge the prorated amount immediately.
proration_behavior='always_invoice',
)
This immediately charges the user a prorated amount when the user changes to a more expensive plan. However, when the user changes to a cheaper plan, the user will be given a prorated credit, making the user's next bill cheaper by the prorated amount.
Scenarios
In these scenarios, suppose the billing date is on the first day of each month. Further suppose that the month has 30 days.
Scenario 1: The user is subscribed to a $5 monthly plan. The user decides to upgrade to a $30 plan on the 22nd day of the month. With 8 days left in the month, the unused portion of the $5 plan is approximately (8 / 30) * 5 = $1.33
, and the prorated amount for the $30 plan is (8 / 30) * 30 = $8.00
. So the user will be charged 8.00 - 1.33 = $6.67
immediately.
Scenario 2: The user is subscribed to a $30 monthly plan. The user decides to downgrade to a $5 plan on the 22nd day of the month. With 8 days left in the month, the unused portion of the $30 plan is approximately (8 / 30) * 30 = $8.00
, and the prorated amount for the $5 plan is (8 / 30) * 5 = $1.33
. In this case, the user will not be charged, but the user's next bill (on the next billing date) will be reduced by 8.00 - 1.33 = $6.67
.
The example calculations above are only approximate because Stripe prorates to the exact second, not by day.
Side notes
dearsina's answer is correct, but involves at least three API calls rather than one. To explicitly follow the steps in that answer, you could do this instead:
# Step 1 in dearsina's answer: Modify the subscription.
modified_subscription = stripe.Subscription.modify(
subscription_id,
items=[{
'id': item_id,
'plan': plan_id,
}],
)
# Steps 2 and 3: Create and retrieve invoice.
invoice = stripe.Invoice.create(customer=modified_subscription.customer)
# Steps 4 and 5: Finalize and pay invoice.
stripe.Invoice.pay(invoice.id)
But why do this when you could do all the steps in one API call?