Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stripe: Add missing stripe_customer_id assertions.

Anders Kaseorgb195dc5a891e
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index aee228639b6..4b6d71fa1da 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -871,6 +871,7 @@ class BillingSession(ABC):
on_free_trial: bool = False,
current_plan_id: Optional[int] = None,
) -> stripe.Invoice:
+ assert customer.stripe_customer_id is not None
plan_name = CustomerPlan.name_from_tier(plan_tier)
assert price_per_license is None or fixed_price is None
price_args: PriceArgs = {}
@@ -1217,6 +1218,7 @@ class BillingSession(ABC):
) -> Dict[str, Any]:
metadata = self.get_metadata_for_stripe_update_card()
customer = self.update_or_create_stripe_customer()
+ assert customer.stripe_customer_id is not None
# URL when user cancels the card update session.
base_cancel_url = f"{self.billing_session_url}/upgrade/"
@@ -3002,6 +3004,7 @@ class BillingSession(ABC):
# layer of defense to avoid creating any invoices for customers not on
# paid plan. It saves a DB query too.
if plan.is_a_paid_plan():
+ assert plan.customer.stripe_customer_id is not None
if plan.invoicing_status == CustomerPlan.INVOICING_STATUS_INITIAL_INVOICE_TO_BE_SENT:
invoiced_through_id = -1
licenses_base = None
@@ -3067,7 +3070,6 @@ class BillingSession(ABC):
plan.invoiced_through = ledger_entry
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_STARTED
plan.save(update_fields=["invoicing_status", "invoiced_through"])
- assert plan.customer.stripe_customer_id is not None
invoice_period = {
"start": datetime_to_timestamp(ledger_entry.event_time),
"end": datetime_to_timestamp(
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 2472ef7b2c5..8da4e41ac9e 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -2027,6 +2027,7 @@ class StripeTest(StripeTestCase):
# Get the last generated invoice for Hamlet
customer = get_customer_by_realm(get_realm("zulip"))
assert customer is not None
+ assert customer.stripe_customer_id is not None
[hamlet_invoice] = iter(stripe.Invoice.list(customer=customer.stripe_customer_id))
self.login_user(othello)
@@ -6052,6 +6053,7 @@ class TestSupportBillingHelpers(StripeTestCase):
self.add_card_and_upgrade(user)
customer = Customer.objects.first()
assert customer is not None
+ assert customer.stripe_customer_id is not None
[charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id))
self.assertEqual(1200 * self.seat_count, charge.amount)
stripe_customer_id = customer.stripe_customer_id
@@ -6151,6 +6153,7 @@ class TestSupportBillingHelpers(StripeTestCase):
self.add_card_and_upgrade(user)
customer = billing_session.get_customer()
assert customer is not None
+ assert customer.stripe_customer_id is not None
[charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id))
self.assertEqual(4000 * min_licenses, charge.amount)