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

Commit 5495e13

Browse files
fix: Race condition in subscription creation webhook handlers (#959)
1 parent 311922d commit 5495e13

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

billing/tests/test_views.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,29 +1150,51 @@ def test_subscription_schedule_updated_logs_changes_to_schedule(
11501150
assert self.owner.plan == original_plan
11511151
assert self.owner.plan_user_count == original_quantity
11521152

1153-
def test_checkout_session_completed_sets_stripe_customer_id(self):
1153+
def test_checkout_session_completed_sets_stripe_ids(self):
11541154
self.owner.stripe_customer_id = None
11551155
self.owner.save()
11561156

1157-
expected_id = "fhjtwoo40"
1157+
expected_customer_id = "cus_1234"
1158+
expected_subscription_id = "sub_7890"
11581159

11591160
self._send_event(
11601161
payload={
11611162
"type": "checkout.session.completed",
11621163
"data": {
11631164
"object": {
1164-
"customer": expected_id,
1165+
"customer": expected_customer_id,
11651166
"client_reference_id": str(self.owner.ownerid),
1167+
"subscription": expected_subscription_id,
11661168
}
11671169
},
11681170
}
11691171
)
11701172

11711173
self.owner.refresh_from_db()
1172-
assert self.owner.stripe_customer_id == expected_id
1174+
assert self.owner.stripe_customer_id == expected_customer_id
1175+
assert self.owner.stripe_subscription_id == expected_subscription_id
11731176

11741177
@patch("billing.views.stripe.Subscription.modify")
11751178
def test_customer_update_but_not_payment_method(self, subscription_modify_mock):
1179+
payment_method = "pm_123"
1180+
self._send_event(
1181+
payload={
1182+
"type": "customer.updated",
1183+
"data": {
1184+
"object": {
1185+
"invoice_settings": {"default_payment_method": None},
1186+
"subscriptions": {
1187+
"data": [{"default_payment_method": payment_method}]
1188+
},
1189+
}
1190+
},
1191+
}
1192+
)
1193+
1194+
subscription_modify_mock.assert_not_called()
1195+
1196+
@patch("billing.views.stripe.Subscription.modify")
1197+
def test_customer_update_but_payment_method_is_same(self, subscription_modify_mock):
11761198
payment_method = "pm_123"
11771199
self._send_event(
11781200
payload={

billing/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ def customer_updated(self, customer: stripe.Customer) -> None:
376376
new_default_payment_method = customer["invoice_settings"][
377377
"default_payment_method"
378378
]
379+
380+
if new_default_payment_method is None:
381+
return
382+
379383
for subscription in customer.get("subscriptions", {}).get("data", []):
380384
if new_default_payment_method == subscription["default_payment_method"]:
381385
continue
@@ -401,6 +405,7 @@ def checkout_session_completed(
401405
)
402406
owner = Owner.objects.get(ownerid=checkout_session.client_reference_id)
403407
owner.stripe_customer_id = checkout_session.customer
408+
owner.stripe_subscription_id = checkout_session.subscription
404409
owner.save()
405410

406411
self._log_updated([owner])

0 commit comments

Comments
 (0)