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

Commit f691ddc

Browse files
chore: more formatting
1 parent 6a8d42c commit f691ddc

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

services/billing.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,40 @@ 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_timestamp = subscription["current_period_start"]
170-
current_subscription_datetime = datetime.fromtimestamp(subscription["current_period_start"])
170+
current_subscription_datetime = datetime.fromtimestamp(
171+
subscription["current_period_start"]
172+
)
171173
differenceFromNow = datetime.now() - current_subscription_datetime
172174

173-
subscription_plan_interval = subscription.plan.interval if subscription.plan is not None else None
174-
within_refund_grace_period = (subscription_plan_interval == "month" and differenceFromNow.days < 1) or (subscription_plan_interval == "year" and differenceFromNow.days < 3)
175+
subscription_plan_interval = (
176+
subscription.plan.interval if subscription.plan is not None else None
177+
)
178+
within_refund_grace_period = (
179+
subscription_plan_interval == "month" and differenceFromNow.days < 1
180+
) or (subscription_plan_interval == "year" and differenceFromNow.days < 3)
175181
if within_refund_grace_period:
176182
stripe.Subscription.cancel(owner.stripe_subscription_id)
177183

178-
invoices_list = stripe.Invoice.list(subscription=owner.stripe_subscription_id, status="paid")
184+
invoices_list = stripe.Invoice.list(
185+
subscription=owner.stripe_subscription_id, status="paid"
186+
)
179187
created_refund = False
180188
# there could be multiple invoices that need to be refunded such as if the user increased seats within the grace period
181189
for invoice in invoices_list["data"]:
182-
start_of_last_period = current_subscription_datetime - relativedelta(months=1) if subscription_plan_interval == "month" else current_subscription_datetime - relativedelta(years=1)
190+
start_of_last_period = (
191+
current_subscription_datetime - relativedelta(months=1)
192+
if subscription_plan_interval == "month"
193+
else current_subscription_datetime - relativedelta(years=1)
194+
)
183195

184196
# refund if the invoice has a charge, it has been fully paid, 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
185197
invoice_created_datetime = datetime.fromtimestamp(invoice["created"])
186-
if invoice["charge"] is not None and invoice["amount_remaining"] == 0 and invoice_created_datetime < current_subscription_datetime and invoice_created_datetime >= start_of_last_period:
198+
if (
199+
invoice["charge"] is not None
200+
and invoice["amount_remaining"] == 0
201+
and invoice_created_datetime < current_subscription_datetime
202+
and invoice_created_datetime >= start_of_last_period
203+
):
187204
stripe.Refund.create(invoice["charge"])
188205
created_refund = True
189206
if created_refund == True:

services/tests/test_billing.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,34 @@
129129
}
130130
]
131131

132+
132133
class MockSubscriptionPlan(object):
133134
def __init__(self, params):
134135
self.id = params["new_plan"]
135-
self.interval = 'year'
136+
self.interval = "year"
137+
136138

137139
class MockSubscription(object):
138140
def __init__(self, subscription_params):
139141
self.schedule = subscription_params["schedule_id"]
140142
self.current_period_start = subscription_params["start_date"]
141143
self.current_period_end = subscription_params["end_date"]
142-
self.plan = MockSubscriptionPlan(subscription_params["plan"]) if subscription_params.get("plan") is not None else None
144+
self.plan = (
145+
MockSubscriptionPlan(subscription_params["plan"])
146+
if subscription_params.get("plan") is not None
147+
else None
148+
)
143149
self.items = {
144150
"data": [
145151
{
146152
"quantity": subscription_params["quantity"],
147153
"id": subscription_params["id"],
148-
"plan": {"id": subscription_params["name"], "interval": subscription_params.get("plan", {}).get("interval", "month")},
154+
"plan": {
155+
"id": subscription_params["name"],
156+
"interval": subscription_params.get("plan", {}).get(
157+
"interval", "month"
158+
),
159+
},
149160
}
150161
]
151162
}
@@ -163,6 +174,7 @@ def __init__(self, subscription_params):
163174
def __getitem__(self, key):
164175
return getattr(self, key)
165176

177+
166178
class StripeServiceTests(TestCase):
167179
def setUp(self):
168180
self.user = OwnerFactory()
@@ -317,7 +329,11 @@ def test_delete_subscription_without_schedule_modifies_subscription_to_delete_at
317329
@patch("services.billing.stripe.Subscription.retrieve")
318330
@patch("services.billing.stripe.SubscriptionSchedule.release")
319331
def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscription_at_end_of_billing_cycle_if_valid_plan(
320-
self, schedule_release_mock, retrieve_subscription_mock, modify_mock, create_refund_mock
332+
self,
333+
schedule_release_mock,
334+
retrieve_subscription_mock,
335+
modify_mock,
336+
create_refund_mock,
321337
):
322338
plan = PlanName.CODECOV_PRO_YEARLY.value
323339
stripe_subscription_id = "sub_1K77Y5GlVGuVgOrkJrLjRnne"
@@ -361,7 +377,14 @@ def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscri
361377
@patch("services.billing.stripe.Subscription.retrieve")
362378
@patch("services.billing.stripe.SubscriptionSchedule.release")
363379
def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscription_with_grace_month_refund_if_valid_plan(
364-
self, schedule_release_mock, retrieve_subscription_mock, cancel_sub_mock, list_invoice_mock, create_refund_mock, modify_customer_mock, modify_sub_mock
380+
self,
381+
schedule_release_mock,
382+
retrieve_subscription_mock,
383+
cancel_sub_mock,
384+
list_invoice_mock,
385+
create_refund_mock,
386+
modify_customer_mock,
387+
modify_sub_mock,
365388
):
366389
with open("./services/tests/samples/stripe_invoice.json") as f:
367390
stripe_invoice_response = json.load(f)
@@ -387,16 +410,20 @@ def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscri
387410
"new_quantity": 7,
388411
"subscription_id": "sub_123",
389412
"interval": "month",
390-
}
413+
},
391414
}
392415

393416
retrieve_subscription_mock.return_value = MockSubscription(subscription_params)
394417
self.stripe.delete_subscription(owner)
395418
schedule_release_mock.assert_called_once_with(stripe_schedule_id)
396419
cancel_sub_mock.assert_called_once_with(stripe_subscription_id)
397-
list_invoice_mock.assert_called_once_with(subscription=stripe_subscription_id, status="paid")
420+
list_invoice_mock.assert_called_once_with(
421+
subscription=stripe_subscription_id, status="paid"
422+
)
398423
self.assertEqual(create_refund_mock.call_count, 2)
399-
modify_customer_mock.assert_called_once_with(owner.stripe_customer_id, balance=0)
424+
modify_customer_mock.assert_called_once_with(
425+
owner.stripe_customer_id, balance=0
426+
)
400427
modify_sub_mock.assert_not_called()
401428

402429
owner.refresh_from_db()
@@ -414,7 +441,14 @@ def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscri
414441
@patch("services.billing.stripe.Subscription.retrieve")
415442
@patch("services.billing.stripe.SubscriptionSchedule.release")
416443
def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscription_with_grace_year_refund_if_valid_plan(
417-
self, schedule_release_mock, retrieve_subscription_mock, cancel_sub_mock, list_invoice_mock, create_refund_mock, modify_customer_mock, modify_sub_mock
444+
self,
445+
schedule_release_mock,
446+
retrieve_subscription_mock,
447+
cancel_sub_mock,
448+
list_invoice_mock,
449+
create_refund_mock,
450+
modify_customer_mock,
451+
modify_sub_mock,
418452
):
419453
with open("./services/tests/samples/stripe_invoice.json") as f:
420454
stripe_invoice_response = json.load(f)
@@ -440,16 +474,20 @@ def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscri
440474
"new_quantity": 7,
441475
"subscription_id": "sub_123",
442476
"interval": "year",
443-
}
477+
},
444478
}
445479

446480
retrieve_subscription_mock.return_value = MockSubscription(subscription_params)
447481
self.stripe.delete_subscription(owner)
448482
schedule_release_mock.assert_called_once_with(stripe_schedule_id)
449483
cancel_sub_mock.assert_called_once_with(stripe_subscription_id)
450-
list_invoice_mock.assert_called_once_with(subscription=stripe_subscription_id, status="paid")
484+
list_invoice_mock.assert_called_once_with(
485+
subscription=stripe_subscription_id, status="paid"
486+
)
451487
self.assertEqual(create_refund_mock.call_count, 2)
452-
modify_customer_mock.assert_called_once_with(owner.stripe_customer_id, balance=0)
488+
modify_customer_mock.assert_called_once_with(
489+
owner.stripe_customer_id, balance=0
490+
)
453491
modify_sub_mock.assert_not_called()
454492

455493
owner.refresh_from_db()

0 commit comments

Comments
 (0)