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

Commit 3ecef75

Browse files
fix: Use utc time consistently and cleanup some logic
1 parent 2570160 commit 3ecef75

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

services/billing.py

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ def delete_subscription(self, owner: Owner):
167167

168168
# we give an auto-refund grace period of 24 hours for a monthly subscription or 72 hours for a yearly subscription
169169
current_subscription_datetime = datetime.fromtimestamp(
170-
subscription["current_period_start"]
170+
subscription["current_period_start"], tz=datetime.timezone.utc
171+
)
172+
difference_from_now = (
173+
datetime.now(datetime.timezone.utc) - current_subscription_datetime
171174
)
172-
difference_from_now = datetime.now() - current_subscription_datetime
173175

174176
subscription_plan_interval = getattr(
175177
getattr(subscription, "plan", None), "interval", None
@@ -178,49 +180,60 @@ def delete_subscription(self, owner: Owner):
178180
subscription_plan_interval == "month" and difference_from_now.days < 1
179181
) or (subscription_plan_interval == "year" and difference_from_now.days < 3)
180182

181-
customer = stripe.Customer.retrieve(owner.stripe_customer_id)
182-
# we are giving customers 2 autorefund instances
183-
autorefunds_remaining = int(
184-
customer["metadata"].get("autorefunds_remaining", "2")
185-
)
183+
autorefunds_remaining = 0
184+
if within_refund_grace_period:
185+
customer = stripe.Customer.retrieve(owner.stripe_customer_id)
186+
# we are currently giving customers 2 autorefund instances
187+
autorefunds_remaining = int(
188+
customer["metadata"].get("autorefunds_remaining", "2")
189+
)
186190

187-
if within_refund_grace_period and autorefunds_remaining > 0:
188-
stripe.Subscription.cancel(owner.stripe_subscription_id)
191+
if autorefunds_remaining > 0:
192+
stripe.Subscription.cancel(owner.stripe_subscription_id)
189193

190-
invoices_list = stripe.Invoice.list(
191-
subscription=owner.stripe_subscription_id, status="paid"
192-
)
193-
created_refund = False
194-
# there could be multiple invoices that need to be refunded such as if the user increased seats within the grace period
195-
for invoice in invoices_list["data"]:
196194
start_of_last_period = (
197195
current_subscription_datetime - relativedelta(months=1)
198196
if subscription_plan_interval == "month"
199197
else current_subscription_datetime - relativedelta(years=1)
200198
)
201-
202-
# refund if all of the following are true: the invoice has a charge, it has been fully paid,
203-
# the creation time was before the start of the current subscription's start, and the creation time was after the start of the last period
204-
invoice_created_datetime = datetime.fromtimestamp(invoice["created"])
205-
if (
206-
invoice["charge"] is not None
207-
and invoice["amount_remaining"] == 0
208-
and invoice_created_datetime < current_subscription_datetime
209-
and invoice_created_datetime >= start_of_last_period
210-
):
211-
stripe.Refund.create(invoice["charge"])
212-
created_refund = True
213-
if created_refund == True:
214-
stripe.Customer.modify(
215-
owner.stripe_customer_id,
216-
balance=0,
217-
metadata={"autorefunds_remaining": str(autorefunds_remaining - 1)},
218-
)
219-
log.info(
220-
f"Autorefunded owner id #{owner.ownerid} for stripe id #{owner.stripe_customer_id}. They have {str(autorefunds_remaining - 1)} remaining."
199+
invoices_list = stripe.Invoice.list(
200+
subscription=owner.stripe_subscription_id,
201+
status="paid",
202+
created={
203+
"created.gte": start_of_last_period.timestamp(),
204+
"created.lt": current_subscription_datetime.timestamp(),
205+
},
221206
)
222-
else:
223-
# outside of the grace period, we schedule a cancellation at the end of the period with no refund
207+
created_refund = False
208+
# there could be multiple invoices that need to be refunded such as if the user increased seats within the grace period
209+
for invoice in invoices_list["data"]:
210+
# refund if the invoice has a charge and it has been fully paid
211+
if (
212+
invoice["charge"] is not None
213+
and invoice["amount_remaining"] == 0
214+
):
215+
stripe.Refund.create(invoice["charge"])
216+
created_refund = True
217+
if created_refund == True:
218+
stripe.Customer.modify(
219+
owner.stripe_customer_id,
220+
balance=0,
221+
metadata={
222+
"autorefunds_remaining": str(autorefunds_remaining - 1)
223+
},
224+
)
225+
log.info(
226+
f"Autorefunded owner id #{owner.ownerid} for stripe id #{owner.stripe_customer_id}. They have {str(autorefunds_remaining - 1)} remaining.",
227+
extra=dict(
228+
owner_id=owner.ownerid,
229+
user_id=self.requesting_user.ownerid,
230+
subscription_id=owner.stripe_subscription_id,
231+
customer_id=owner.stripe_customer_id,
232+
),
233+
)
234+
235+
# schedule a cancellation at the end of the paid period with no refund
236+
if not within_refund_grace_period or autorefunds_remaining <= 0:
224237
stripe.Subscription.modify(
225238
owner.stripe_subscription_id,
226239
cancel_at_period_end=True,

0 commit comments

Comments
 (0)