# Refunding credit balance to customer after subscription downgrade or cancellation

When a subscription is upgraded, Stripe will automatically calculate the prorated amount that the customer owes. For example, if a customer is moved from a US$10.00/mo subscription price to a US$100.00/mo subscription price in the middle of their billing cycle, they are owed a US$5.00 credit for the first price and they owe US$50.00 for the new price. This means that they owe a total of US$45.00 for the subscription upgrade. This amount can be charged at the end of the billing cycle or immediately by creating an invoice.
When a subscription is downgraded or cancelled, the customer may be owed a credit. If a customer moves from a US$100.00/mo subscription price to a US$10.00/mo subscription price in the middle of the billing cycle, they are owed US$50.00 for the first price and owe US$5.00 for the new price. A total of US$45.00 is now owed to the customer for the subscription downgrade, and that credit ends up being added to their credit balance to be applied to future invoices.
Some merchants may want to refund that credit amount directly to their customers' original method of payment. Today, this is not something that happens automatically and instead can be actioned by the merchant.
## Refunding the customer's credit balance
* [Retrieve](https://stripe.com/docs/api/customers/retrieve) the customer's credit balance and/or sum [pending invoice items](https://stripe.com/docs/api/invoiceitems/list#list_invoiceitems-pending)
* Using the List Invoices [API](https://stripe.com/docs/api#list_invoices), pass the subscription ID in the `subscription` parameter to view invoices for that subscription ID only
* You'll then need to [expand](https://stripe.com/docs/api#expanding_objects) the associated charge
Then, for each invoice:
* Locate the associated charge
* Create a [refund](https://stripe.com/docs/api#create_refund) for up to the amount of the customer's credit balance
* If the charge amount is smaller than the customer's credit balance, refund the full charge amount, and then subtract the full charge amount from the customer's credit balance to get the remaining balance to refund. For example: If the customer's credit balance = £100, and the latest subscription charge = £40, the remaining credit balance to refund = £60.
* Move to the next invoice in the list until the full credit has been refunded
* Update the customer's account balance back to £0.00 via the [API](https://stripe.com/docs/api/customers/update#update_customer-balance) by [deleting](https://stripe.com/docs/api#delete_invoiceitem) the pending invoice items, or by modifying the balance directly through the [Dashboard](https://stripe.com/docs/billing/customer/balance#modifying)
### Additional resources
* [Customer balance: Subscriptions](https://stripe.com/docs/billing/customer/balance)
* [Customer balance: Invoicing](https://stripe.com/docs/invoicing/customer/balance)
* [Upgrading & Downgrading subscriptions](https://stripe.com/docs/billing/subscriptions/upgrade-downgrade)
