How does app plan upgrade and downgrade code flow work?

Hi,

My app has 3 plans… Free, Lite($129) & Pro($349).
I have some confusion in code implementation for the following scenarios as the shopify documentation is not really helpful. Please help me.

Scenario 1

  1. The merchant installs the app and chooses Lite($129) plan.
  2. He uses the plan for 5 days and then upgrades to Pro($349) plan.

Scenario 2

  1. The merchant installs the app and chooses Pro($349) plan.
  2. He uses the plan for 5 days and then downgrades to Lite($129) plan.

Scenario 2

  1. The merchant installs the app and chooses Lite($129) plan.
  2. He uses the plan for 5 days and then downgrades to Free plan.

Question: How the refund/credit takes place for the merchant. Who would calculate the prorated amount and initiate the credit/debit.

Can anyone give me the code flow please???

Thanks in advance!!