Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stripe: Add link_stripe_customer_id helper to BillingSession.
Lauryn Menard97e499cfaa8b
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 66636027aa9..38d2d7ce188 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -1145,6 +1145,24 @@ class BillingSession(ABC):self.replace_payment_method(customer.stripe_customer_id, payment_method, True)return customer+ # Callers of this function should check if overwriting an existing stripe_customer_id+ # on the Customer object is allowed for that specific billing action.+ def link_stripe_customer_id(self, new_stripe_customer_id: str) -> None:+ customer = self.get_customer()+ assert customer is not None+ old_stripe_customer_id = customer.stripe_customer_id+ customer.stripe_customer_id = new_stripe_customer_id+ customer.save(update_fields=["stripe_customer_id"])+ self.write_to_audit_log(+ event_type=BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED,+ event_time=timezone_now(),+ extra_data={+ "old_value": old_stripe_customer_id,+ "new_value": new_stripe_customer_id,+ "property": "stripe_customer_id",+ },+ )+def create_stripe_invoice_and_charge(self,metadata: dict[str, Any],@@ -1540,8 +1558,7 @@ class BillingSession(ABC):# invoices are manually created first in stripe, it's important# for our billing page to have our Customer object correctly# linked to the customer in stripe.- customer.stripe_customer_id = str(invoice_customer_id)- customer.save(update_fields=["stripe_customer_id"])+ self.link_stripe_customer_id(str(invoice_customer_id))fixed_price_plan_params["sent_invoice_id"] = sent_invoice_idInvoice.objects.create(diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex dc4aef62242..f2c34521b7a 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -8580,6 +8580,15 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):self.assertEqual(fixed_price_plan_offer.sent_invoice_id, sent_invoice_id)self.assertEqual(fixed_price_plan_offer.get_plan_status_as_text(), "Configured")+ audit_log = RemoteRealmAuditLog.objects.filter(+ remote_realm=self.remote_realm,+ event_type=AuditLogEventType.CUSTOMER_PROPERTY_CHANGED,+ ).last()+ assert audit_log is not None+ self.assertEqual(audit_log.remote_realm, self.remote_realm)+ self.assertIsNone(audit_log.extra_data["old_value"])+ self.assertEqual(audit_log.extra_data["property"], "stripe_customer_id")+invoice = Invoice.objects.get(stripe_invoice_id=sent_invoice_id)self.assertEqual(invoice.status, Invoice.SENT)@@ -10503,6 +10512,15 @@ class TestRemoteServerBillingFlow(StripeTestCase, RemoteServerTestCase):self.assertEqual(fixed_price_plan_offer.sent_invoice_id, sent_invoice_id)self.assertEqual(fixed_price_plan_offer.get_plan_status_as_text(), "Configured")+ audit_log = RemoteZulipServerAuditLog.objects.filter(+ server=self.server,+ event_type=AuditLogEventType.CUSTOMER_PROPERTY_CHANGED,+ ).last()+ assert audit_log is not None+ self.assertEqual(audit_log.server, self.server)+ self.assertIsNone(audit_log.extra_data["old_value"])+ self.assertEqual(audit_log.extra_data["property"], "stripe_customer_id")+invoice = Invoice.objects.get(stripe_invoice_id=sent_invoice_id)self.assertEqual(invoice.status, Invoice.SENT)