The most confusing aspect to upgrading or downgrading plans is how existing payments are factored into the amount to be invoiced. By default, Stripe prorates subscription costs.
For example, say the customer in the above code is billed on the 1st of each month and the above code is executed exactly half-way through the plan’s billing cycle (i.e., on the 15th of March). In that case, the customer has already paid $10, and used half of the current billing cycle. When the customer is switched to the new plan, with a cost of $25, the unused portion of the previous plan results in a credit of $5. The cost of the new plan for the rest of the billing cycle (i.e., the rest of March) will be $12.50. Therefore, the total prorated cost of switching from the cheaper plan to the most expensive plan is $7.50. This amount will be added to the next invoice.
The next invoice, on April 1st, will also reflect the payment required on the new plan for that upcoming month: $25. Thus, the April 1st invoice would be for a total of $32.50.
If the new plan is less expensive than the existing plan, the prorated credit will be larger than the new amount due. For example, if the customer is switching from the $25/month plan to the $10/month plan exactly halfway through the billing cycle, the customer will have a net credit of $7.50 resulting from a credit of $12.50 for the unused portion of the old plan less a charge of $5 for the remaining time on the new plan. The next invoice, at the start of the next billing period, will reflect the $7.50 credit and the $10 due for a full, upcoming month on the new plan, resulting in a net charge of of $2.50.