@@ -148,14 +148,14 @@ def list_filtered_invoices(self, owner: Owner, limit=10):
148148 )
149149 return list (invoices_filtered_by_status_and_total )
150150
151- # cancels a Stripe customer subscription immediately and attempts to refund their payments for the current period
152151 def cancel_and_refund (
153152 self ,
154153 owner ,
155154 current_subscription_datetime ,
156155 subscription_plan_interval ,
157156 autorefunds_remaining ,
158157 ):
158+ # cancels a Stripe customer subscription immediately and attempts to refund their payments for the current period
159159 stripe .Subscription .cancel (owner .stripe_subscription_id )
160160
161161 start_of_last_period = current_subscription_datetime - relativedelta (months = 1 )
@@ -180,12 +180,15 @@ def cancel_and_refund(
180180 },
181181 )
182182
183- # we only want to refund the invoices for the latest, current period
183+ # we only want to refund the invoices PAID recently for the latest, current period. "invoices_list" gives us any invoice
184+ # created over the last month/year based on what period length they are on but the customer could have possibly
185+ # switched from monthly to yearly recently.
184186 recently_paid_invoices_list = [
185187 invoice
186188 for invoice in invoices_list ["data" ]
187189 if invoice ["status_transitions" ]["paid_at" ] is not None
188- and invoice ["status_transitions" ]["paid_at" ] >= int (invoice_grace_period_start .timestamp ())
190+ and invoice ["status_transitions" ]["paid_at" ]
191+ >= int (invoice_grace_period_start .timestamp ())
189192 ]
190193
191194 created_refund = False
@@ -215,7 +218,6 @@ def cancel_and_refund(
215218 ),
216219 )
217220 else :
218- # log that we created no refunds but did cancel them
219221 log .info (
220222 "Grace period cancelled a subscription but did not find any appropriate invoices to autorefund" ,
221223 extra = dict (
0 commit comments