Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Update create_stripe_customer to not attach payment method.
Lauryn Menard1f28837bd7c4
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 7955ed75cbb..902a7130952 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -402,22 +402,16 @@ class BillingSession(ABC):pass@catch_stripe_errors- def create_stripe_customer(self, payment_method: Optional[str] = None) -> Customer:+ def create_stripe_customer(self) -> Customer:stripe_customer_data = self.get_data_for_stripe_customer()stripe_customer = stripe.Customer.create(description=stripe_customer_data.description,email=stripe_customer_data.email,metadata=stripe_customer_data.metadata,- payment_method=payment_method,- )- stripe.Customer.modify(- stripe_customer.id, invoice_settings={"default_payment_method": payment_method})event_time = timestamp_to_datetime(stripe_customer.created)with transaction.atomic():self.write_to_audit_log(AuditLogEventType.STRIPE_CUSTOMER_CREATED, event_time)- if payment_method is not None:- self.write_to_audit_log(AuditLogEventType.STRIPE_CARD_CHANGED, event_time)customer = self.update_or_create_customer(stripe_customer.id)return customer@@ -448,11 +442,15 @@ class BillingSession(ABC):def update_or_create_stripe_customer(self, payment_method: Optional[str] = None) -> Customer:customer = self.get_customer()if customer is None or customer.stripe_customer_id is None:+ # A stripe.PaymentMethod should be attached to a stripe.Customer via+ # a stripe.SetupIntent or stripe.PaymentIntent. Here we just want to+ # create a new stripe.Customer.+ assert payment_method is None# We could do a better job of handling race conditions here, but if two# people try to upgrade at exactly the same time, the main bad thing that- # will happen is that we will create an extra stripe customer that we can+ # will happen is that we will create an extra stripe.Customer that we can# delete or ignore.- return self.create_stripe_customer(payment_method=payment_method)+ return self.create_stripe_customer()if payment_method is not None:self.replace_payment_method(customer.stripe_customer_id, payment_method, True)return customerdiff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 399c7d5721c..2b436e3a284 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -3849,16 +3849,11 @@ class StripeTest(StripeTestCase):self.assertFalse(customer_has_credit_card_as_default_payment_method(customer))billing_session = RealmBillingSession(iago)- customer = billing_session.create_stripe_customer()+ customer = billing_session.update_or_create_stripe_customer()self.assertFalse(customer_has_credit_card_as_default_payment_method(customer))- customer = billing_session.create_stripe_customer(- payment_method=create_payment_method(- self.get_test_card_number(- attaches_to_customer=True, charge_succeeds=True, card_provider="visa"- )- ).id,- )+ self.login_user(iago)+ self.upgrade()self.assertTrue(customer_has_credit_card_as_default_payment_method(customer))@@ -4317,9 +4312,7 @@ class BillingHelpersTest(ZulipTestCase):"corporate.lib.stripe.BillingSession.create_stripe_customer", return_value="returned") as mocked1:billing_session = RealmBillingSession(user)- returned = billing_session.update_or_create_stripe_customer(- payment_method="payment_method_id"- )+ returned = billing_session.update_or_create_stripe_customer()mocked1.assert_called_once()self.assertEqual(returned, "returned")@@ -4329,9 +4322,7 @@ class BillingHelpersTest(ZulipTestCase):"corporate.lib.stripe.BillingSession.create_stripe_customer", return_value="returned") as mocked2:billing_session = RealmBillingSession(user)- returned = billing_session.update_or_create_stripe_customer(- payment_method="payment_method_id"- )+ returned = billing_session.update_or_create_stripe_customer()mocked2.assert_called_once()self.assertEqual(returned, "returned")@@ -4340,7 +4331,7 @@ class BillingHelpersTest(ZulipTestCase):# Customer exists, replace payment sourcewith patch("corporate.lib.stripe.BillingSession.replace_payment_method") as mocked3:billing_session = RealmBillingSession(user)- returned_customer = billing_session.update_or_create_stripe_customer("token")+ returned_customer = billing_session.update_or_create_stripe_customer("pm_card_visa")mocked3.assert_called_once()self.assertEqual(returned_customer, customer)