This repository was archived by the owner on Jun 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -171,10 +171,22 @@ def cancel_and_refund(
171171 "created.lt" : int (current_subscription_datetime .timestamp ()),
172172 },
173173 )
174+ invoice_grace_period_start = (
175+ current_subscription_datetime - relativedelta (days = 1 )
176+ if subscription_plan_interval == "month"
177+ else current_subscription_datetime - relativedelta (days = 3 )
178+ )
179+ # we only want to refund the invoices for the latest, current period
180+ recently_paid_invoices_list = [
181+ invoice
182+ for invoice in invoices_list ["data" ]
183+ if invoice ["status_transitions" ]["paid_at" ] is not None
184+ and invoice ["status_transitions" ]["paid_at" ] >= invoice_grace_period_start
185+ ]
174186
175187 created_refund = False
176188 # there could be multiple invoices that need to be refunded such as if the user increased seats within the grace period
177- for invoice in invoices_list [ "data" ] :
189+ for invoice in recently_paid_invoices_list :
178190 # refund if the invoice has a charge and it has been fully paid
179191 if invoice ["charge" ] is not None and invoice ["amount_remaining" ] == 0 :
180192 stripe .Refund .create (invoice ["charge" ])
You can’t perform that action at this time.
0 commit comments