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.

billing: Improve error handling when invoicing plans as needed.

Lauryn Menardec89e5afd252
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 38c3b114f09..e7564a91ef7 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -5515,42 +5515,78 @@ def check_remote_server_audit_log_data(
return True
+def review_and_maybe_invoice_plan(
+ plan: CustomerPlan,
+ event_time: datetime,
+) -> None:
+ remote_server: RemoteZulipServer | None = None
+ if plan.customer.realm is not None:
+ billing_session: BillingSession = RealmBillingSession(realm=plan.customer.realm)
+ elif plan.customer.remote_realm is not None:
+ remote_realm = plan.customer.remote_realm
+ remote_server = remote_realm.server
+ billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
+ elif plan.customer.remote_server is not None:
+ remote_server = plan.customer.remote_server
+ billing_session = RemoteServerBillingSession(remote_server=remote_server)
+
+ if (
+ plan.fixed_price is not None
+ and plan.end_date is not None
+ and not plan.reminder_to_review_plan_email_sent
+ ):
+ maybe_send_fixed_price_plan_renewal_reminder_email(plan, billing_session)
+
+ try_to_invoice_plan = True
+ if remote_server:
+ # We need the audit log data from the remote server to be
+ # current enough for license checks on paid plans.
+ try_to_invoice_plan = check_remote_server_audit_log_data(
+ remote_server, plan, billing_session
+ )
+
+ if try_to_invoice_plan:
+ # plan.next_invoice_date can be None after calling invoice_plan.
+ while plan.next_invoice_date is not None and plan.next_invoice_date <= event_time:
+ billing_session.invoice_plan(plan, plan.next_invoice_date)
+ plan.refresh_from_db()
+
+
def invoice_plans_as_needed(event_time: datetime | None = None) -> None:
+ failed_customer_ids = set()
if event_time is None:
event_time = timezone_now()
# For complimentary access plans with status SWITCH_PLAN_TIER_AT_PLAN_END, we need
# to invoice the complimentary access plan followed by the new plan on the same day,
# hence ordering by ID.
for plan in CustomerPlan.objects.filter(next_invoice_date__lte=event_time).order_by("id"):
- remote_server: RemoteZulipServer | None = None
- if plan.customer.realm is not None:
- billing_session: BillingSession = RealmBillingSession(realm=plan.customer.realm)
- elif plan.customer.remote_realm is not None:
- remote_realm = plan.customer.remote_realm
- remote_server = remote_realm.server
- billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
- elif plan.customer.remote_server is not None:
- remote_server = plan.customer.remote_server
- billing_session = RemoteServerBillingSession(remote_server=remote_server)
-
- if (
- plan.fixed_price is not None
- and plan.end_date is not None
- and not plan.reminder_to_review_plan_email_sent
- ):
- maybe_send_fixed_price_plan_renewal_reminder_email(plan, billing_session)
-
- try_to_invoice_plan = True
- if remote_server:
- try_to_invoice_plan = check_remote_server_audit_log_data(
- remote_server, plan, billing_session
- )
+ if plan.customer.id in failed_customer_ids: # nocoverage
+ # We've already had a failure for this customer in this
+ # invoicing attempt; skip it so we can process others,
+ # without incorrectly processing other plans on this same
+ # customer.
+ continue
- if try_to_invoice_plan:
- # plan.next_invoice_date can be None after calling invoice_plan.
- while plan.next_invoice_date is not None and plan.next_invoice_date <= event_time:
- billing_session.invoice_plan(plan, plan.next_invoice_date)
- plan.refresh_from_db()
+ try:
+ review_and_maybe_invoice_plan(plan, event_time)
+ except Exception as e:
+ failed_customer_ids.add(plan.customer.id)
+ if isinstance(e, BillingError):
+ billing_logger.exception(
+ "Invoicing failed: Customer.id: %s, CustomerPlan.id: %s, BillingError: %s",
+ plan.customer.id,
+ plan.id,
+ e.error_description,
+ )
+ elif isinstance(e, AssertionError): # nocoverage
+ billing_logger.exception(
+ "Invoicing failed due to AssertionError: Customer.id: %s, CustomerPlan.id: %s",
+ plan.customer.id,
+ plan.id,
+ stack_info=True,
+ )
+ else:
+ billing_logger.exception(e, stack_info=True) # nocoverage
def is_realm_on_free_trial(realm: Realm) -> bool:
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 71d376a29e4..0e6b6d84a69 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -2174,12 +2174,15 @@ class StripeTest(StripeTestCase):
customer.minimum_licenses = None
customer.save()
- with self.assertRaises(BillingError) as context:
+ with self.assertLogs("corporate.stripe", level="ERROR") as m:
invoice_plans_as_needed(self.next_year + timedelta(days=366))
- error_message = context.exception.error_description
- self.assertEqual(
- error_message,
+ self.assertIn(
+ f"ERROR:corporate.stripe:Invoicing failed: Customer.id: {customer.id}",
+ m.output[0],
+ )
+ self.assertIn(
"Renewal licenses (6) less than minimum licenses (10) required for plan Zulip Cloud Plus.",
+ m.output[0],
)
def test_upgrade_with_tampered_seat_count(self) -> None:
@@ -6013,11 +6016,15 @@ class InvoiceTest(StripeTestCase):
"name-extra-user",
acting_user=None,
)
- with self.assertRaises(BillingError) as context:
+ with self.assertLogs("corporate.stripe", level="ERROR") as m:
invoice_plans_as_needed(self.next_year)
- self.assertRegex(
- context.exception.error_description,
+ self.assertIn(
+ "ERROR:corporate.stripe:Invoicing failed: Customer.id:",
+ m.output[0],
+ )
+ self.assertIn(
"Customer has not manually updated plan for current license count:",
+ m.output[0],
)
@mock_stripe()