Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit ad4d771

Browse files
fix: Only possibly refund invoices that were paid within grace for current period
1 parent a3f452d commit ad4d771

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

services/billing.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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"])

0 commit comments

Comments
 (0)