stripe: Add missing stripe_customer_id assertions.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-04-30 10:43:16 -07:00 committed by Tim Abbott
parent 7b2a4304fc
commit b195dc5a89
2 changed files with 6 additions and 1 deletions

View File

@ -871,6 +871,7 @@ class BillingSession(ABC):
on_free_trial: bool = False, on_free_trial: bool = False,
current_plan_id: Optional[int] = None, current_plan_id: Optional[int] = None,
) -> stripe.Invoice: ) -> stripe.Invoice:
assert customer.stripe_customer_id is not None
plan_name = CustomerPlan.name_from_tier(plan_tier) plan_name = CustomerPlan.name_from_tier(plan_tier)
assert price_per_license is None or fixed_price is None assert price_per_license is None or fixed_price is None
price_args: PriceArgs = {} price_args: PriceArgs = {}
@ -1217,6 +1218,7 @@ class BillingSession(ABC):
) -> Dict[str, Any]: ) -> Dict[str, Any]:
metadata = self.get_metadata_for_stripe_update_card() metadata = self.get_metadata_for_stripe_update_card()
customer = self.update_or_create_stripe_customer() customer = self.update_or_create_stripe_customer()
assert customer.stripe_customer_id is not None
# URL when user cancels the card update session. # URL when user cancels the card update session.
base_cancel_url = f"{self.billing_session_url}/upgrade/" 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 # layer of defense to avoid creating any invoices for customers not on
# paid plan. It saves a DB query too. # paid plan. It saves a DB query too.
if plan.is_a_paid_plan(): 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: if plan.invoicing_status == CustomerPlan.INVOICING_STATUS_INITIAL_INVOICE_TO_BE_SENT:
invoiced_through_id = -1 invoiced_through_id = -1
licenses_base = None licenses_base = None
@ -3067,7 +3070,6 @@ class BillingSession(ABC):
plan.invoiced_through = ledger_entry plan.invoiced_through = ledger_entry
plan.invoicing_status = CustomerPlan.INVOICING_STATUS_STARTED plan.invoicing_status = CustomerPlan.INVOICING_STATUS_STARTED
plan.save(update_fields=["invoicing_status", "invoiced_through"]) plan.save(update_fields=["invoicing_status", "invoiced_through"])
assert plan.customer.stripe_customer_id is not None
invoice_period = { invoice_period = {
"start": datetime_to_timestamp(ledger_entry.event_time), "start": datetime_to_timestamp(ledger_entry.event_time),
"end": datetime_to_timestamp( "end": datetime_to_timestamp(

View File

@ -2027,6 +2027,7 @@ class StripeTest(StripeTestCase):
# Get the last generated invoice for Hamlet # Get the last generated invoice for Hamlet
customer = get_customer_by_realm(get_realm("zulip")) customer = get_customer_by_realm(get_realm("zulip"))
assert customer is not None assert customer is not None
assert customer.stripe_customer_id is not None
[hamlet_invoice] = iter(stripe.Invoice.list(customer=customer.stripe_customer_id)) [hamlet_invoice] = iter(stripe.Invoice.list(customer=customer.stripe_customer_id))
self.login_user(othello) self.login_user(othello)
@ -6052,6 +6053,7 @@ class TestSupportBillingHelpers(StripeTestCase):
self.add_card_and_upgrade(user) self.add_card_and_upgrade(user)
customer = Customer.objects.first() customer = Customer.objects.first()
assert customer is not None assert customer is not None
assert customer.stripe_customer_id is not None
[charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id)) [charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id))
self.assertEqual(1200 * self.seat_count, charge.amount) self.assertEqual(1200 * self.seat_count, charge.amount)
stripe_customer_id = customer.stripe_customer_id stripe_customer_id = customer.stripe_customer_id
@ -6151,6 +6153,7 @@ class TestSupportBillingHelpers(StripeTestCase):
self.add_card_and_upgrade(user) self.add_card_and_upgrade(user)
customer = billing_session.get_customer() customer = billing_session.get_customer()
assert customer is not None assert customer is not None
assert customer.stripe_customer_id is not None
[charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id)) [charge] = iter(stripe.Charge.list(customer=customer.stripe_customer_id))
self.assertEqual(4000 * min_licenses, charge.amount) self.assertEqual(4000 * min_licenses, charge.amount)