Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stripe: Error on missing invoice for invoice-billed free trial.
Aman Agrawalcfe589e725b1
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex d8ede99a5da..258b8ca8692 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -2508,7 +2508,11 @@ class BillingSession(ABC):# Check if user has already paid for the plan by invoice.if not plan.charge_automatically:last_sent_invoice = Invoice.objects.filter(plan=plan).order_by("-id").first()- if last_sent_invoice and last_sent_invoice.status == Invoice.PAID:+ # An invoice-billed, free trial plan should have an invoice+ # created when it was created (see process_initial_upgrade).+ if last_sent_invoice is None:+ raise BillingError(f"Invoice-billed free trial has no invoice: {plan}.")+ if last_sent_invoice.status == Invoice.PAID:# This will create invoice for any additional licenses that user has at the time of# switching from free trial to paid plan since they already paid for the plan's this billing cycle.is_renewal = Falsediff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 7de3eb0eba6..30cf691093b 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -1787,6 +1787,37 @@ class StripeTest(StripeTestCase):# No additional ledger entries are created.self.assertEqual(before_ledger_count, after_ledger_count)+ def test_make_end_of_cycle_updates_errors_without_free_trial_invoice(self) -> None:+ realm = get_realm("zulip")+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_123")+ free_trial_end_date = self.now + timedelta(days=30)+ plan = CustomerPlan.objects.create(+ customer=customer,+ tier=CustomerPlan.TIER_CLOUD_STANDARD,+ status=CustomerPlan.FREE_TRIAL,+ # Billed by invoice rather than charged automatically.+ charge_automatically=False,+ billing_cycle_anchor=self.now,+ billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,+ price_per_license=8000,+ next_invoice_date=free_trial_end_date,+ )+ LicenseLedger.objects.create(+ plan=plan,+ is_renewal=True,+ event_time=self.now,+ licenses=10,+ licenses_at_next_renewal=10,+ )+ # No Invoice object exists for the plan.+ billing_session = RealmBillingSession(realm=realm)+ with self.assertRaises(BillingError) as billing_context:+ billing_session.make_end_of_cycle_updates_if_needed(plan, free_trial_end_date)+ self.assertEqual(+ f"Invoice-billed free trial has no invoice: {plan}.",+ billing_context.exception.error_description,+ )+@mock_stripe(tested_timestamp_fields=["created"])def test_free_trial_upgrade_by_invoice_with_additional_users_after_payment(self, *mocks: Mock