Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Move `update_billing_method...` to BillingSession class.
Lauryn Menardec2b00e8c4ed
diff --git a/analytics/tests/test_support_views.py b/analytics/tests/test_support_views.pyindex abbea473e68..9a8256c0daa 100644--- a/analytics/tests/test_support_views.py+++ b/analytics/tests/test_support_views.py@@ -419,7 +419,7 @@ class TestSupportEndpoint(ZulipTestCase):result,)- @mock.patch("analytics.views.support.update_billing_method_of_current_plan")+ @mock.patch("analytics.views.support.update_realm_billing_method")def test_change_billing_method(self, m: mock.Mock) -> None:cordelia = self.example_user("cordelia")self.login_user(cordelia)diff --git a/analytics/views/support.py b/analytics/views/support.pyindex 2118aad66db..475f2cef1ce 100644--- a/analytics/views/support.py+++ b/analytics/views/support.py@@ -58,13 +58,13 @@ if settings.BILLING_ENABLED:get_latest_seat_count,make_end_of_cycle_updates_if_needed,switch_realm_from_standard_to_plus_plan,- update_billing_method_of_current_plan,void_all_open_invoices,)from corporate.lib.support import (approve_realm_sponsorship,attach_discount_to_realm,get_discount_for_realm,+ update_realm_billing_method,update_realm_sponsorship_status,)from corporate.models import (@@ -239,14 +239,14 @@ def support(context["success_message"] = f"{realm.string_id} deactivated."elif billing_method is not None:if billing_method == "send_invoice":- update_billing_method_of_current_plan(+ update_realm_billing_method(realm, charge_automatically=False, acting_user=acting_user)context["success_message"] = f"Billing method of {realm.string_id} updated to pay by invoice."elif billing_method == "charge_automatically":- update_billing_method_of_current_plan(+ update_realm_billing_method(realm, charge_automatically=True, acting_user=acting_user)context[diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 37f41e7513a..f11870ae0bb 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -332,6 +332,7 @@ class AuditLogEventType(Enum):DISCOUNT_CHANGED = 4SPONSORSHIP_APPROVED = 5SPONSORSHIP_PENDING_STATUS_CHANGED = 6+ BILLING_METHOD_CHANGED = 7class BillingSessionAuditLogEventError(Exception):@@ -463,6 +464,19 @@ class BillingSession(ABC):extra_data={"sponsorship_pending": sponsorship_pending},)+ def update_billing_method_of_current_plan(self, charge_automatically: bool) -> None:+ customer = self.get_customer()+ if customer is not None:+ plan = get_current_plan_by_customer(customer)+ if plan is not None:+ plan.charge_automatically = charge_automatically+ plan.save(update_fields=["charge_automatically"])+ self.write_to_audit_log(+ event_type=AuditLogEventType.BILLING_METHOD_CHANGED,+ event_time=timezone_now(),+ extra_data={"charge_automatically": charge_automatically},+ )+class RealmBillingSession(BillingSession):def __init__(self, user: UserProfile, realm: Optional[Realm] = None) -> None:@@ -493,6 +507,8 @@ class RealmBillingSession(BillingSession):return RealmAuditLog.REALM_SPONSORSHIP_APPROVEDelif event_type is AuditLogEventType.SPONSORSHIP_PENDING_STATUS_CHANGED:return RealmAuditLog.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED+ elif event_type is AuditLogEventType.BILLING_METHOD_CHANGED:+ return RealmAuditLog.REALM_BILLING_METHOD_CHANGEDelse:raise BillingSessionAuditLogEventError(event_type)@@ -1308,19 +1324,3 @@ def switch_realm_from_standard_to_plus_plan(realm: Realm) -> None:plus_plan = get_current_plan_by_realm(realm)assert plus_plan is not None # for mypyinvoice_plan(plus_plan, plan_switch_time)---def update_billing_method_of_current_plan(- realm: Realm, charge_automatically: bool, *, acting_user: Optional[UserProfile]-) -> None:- plan = get_current_plan_by_realm(realm)- if plan is not None:- plan.charge_automatically = charge_automatically- plan.save(update_fields=["charge_automatically"])- RealmAuditLog.objects.create(- realm=realm,- acting_user=acting_user,- event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED,- event_time=timezone_now(),- extra_data={"charge_automatically": charge_automatically},- )diff --git a/corporate/lib/support.py b/corporate/lib/support.pyindex de36dae11ab..08113e88758 100644--- a/corporate/lib/support.py+++ b/corporate/lib/support.py@@ -41,3 +41,10 @@ def update_realm_sponsorship_status() -> None:billing_session = RealmBillingSession(acting_user, realm)billing_session.update_customer_sponsorship_status(sponsorship_pending)+++def update_realm_billing_method(+ realm: Realm, charge_automatically: bool, *, acting_user: UserProfile+) -> None:+ billing_session = RealmBillingSession(acting_user, realm)+ billing_session.update_billing_method_of_current_plan(charge_automatically)diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 383222f8a9a..b2db3224e6f 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -72,7 +72,6 @@ from corporate.lib.stripe import (stripe_get_customer,switch_realm_from_standard_to_plus_plan,unsign_string,- update_billing_method_of_current_plan,update_license_ledger_for_automanaged_plan,update_license_ledger_for_manual_plan,update_license_ledger_if_needed,@@ -82,6 +81,7 @@ from corporate.lib.support import (approve_realm_sponsorship,attach_discount_to_realm,get_discount_for_realm,+ update_realm_billing_method,update_realm_sponsorship_status,)from corporate.models import (@@ -3849,41 +3849,6 @@ class StripeTest(StripeTestCase):self.assert_in_success_response(["Request sponsorship"], result)- def test_update_billing_method_of_current_plan(self) -> None:- realm = get_realm("zulip")- customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_12345")- plan = CustomerPlan.objects.create(- customer=customer,- status=CustomerPlan.ACTIVE,- billing_cycle_anchor=timezone_now(),- billing_schedule=CustomerPlan.ANNUAL,- tier=CustomerPlan.STANDARD,- )- self.assertEqual(plan.charge_automatically, False)-- iago = self.example_user("iago")- update_billing_method_of_current_plan(realm, True, acting_user=iago)- plan.refresh_from_db()- self.assertEqual(plan.charge_automatically, True)- realm_audit_log = RealmAuditLog.objects.filter(- event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED- ).last()- assert realm_audit_log is not None- expected_extra_data = {"charge_automatically": plan.charge_automatically}- self.assertEqual(realm_audit_log.acting_user, iago)- self.assertEqual(realm_audit_log.extra_data, expected_extra_data)-- update_billing_method_of_current_plan(realm, False, acting_user=iago)- plan.refresh_from_db()- self.assertEqual(plan.charge_automatically, False)- realm_audit_log = RealmAuditLog.objects.filter(- event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED- ).last()- assert realm_audit_log is not None- expected_extra_data = {"charge_automatically": plan.charge_automatically}- self.assertEqual(realm_audit_log.acting_user, iago)- self.assertEqual(realm_audit_log.extra_data, expected_extra_data)-@mock_stripe()def test_customer_has_credit_card_as_default_payment_method(self, *mocks: Mock) -> None:iago = self.example_user("iago")@@ -5062,3 +5027,38 @@ class TestSupportBillingHelpers(StripeTestCase):expected_extra_data = {"sponsorship_pending": True}self.assertEqual(realm_audit_log.extra_data, expected_extra_data)self.assertEqual(realm_audit_log.acting_user, iago)++ def test_update_realm_billing_method(self) -> None:+ realm = get_realm("zulip")+ customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_12345")+ plan = CustomerPlan.objects.create(+ customer=customer,+ status=CustomerPlan.ACTIVE,+ billing_cycle_anchor=timezone_now(),+ billing_schedule=CustomerPlan.ANNUAL,+ tier=CustomerPlan.STANDARD,+ )+ self.assertEqual(plan.charge_automatically, False)++ iago = self.example_user("iago")+ update_realm_billing_method(realm, True, acting_user=iago)+ plan.refresh_from_db()+ self.assertEqual(plan.charge_automatically, True)+ realm_audit_log = RealmAuditLog.objects.filter(+ event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED+ ).last()+ assert realm_audit_log is not None+ expected_extra_data = {"charge_automatically": plan.charge_automatically}+ self.assertEqual(realm_audit_log.acting_user, iago)+ self.assertEqual(realm_audit_log.extra_data, expected_extra_data)++ update_realm_billing_method(realm, False, acting_user=iago)+ plan.refresh_from_db()+ self.assertEqual(plan.charge_automatically, False)+ realm_audit_log = RealmAuditLog.objects.filter(+ event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED+ ).last()+ assert realm_audit_log is not None+ expected_extra_data = {"charge_automatically": plan.charge_automatically}+ self.assertEqual(realm_audit_log.acting_user, iago)+ self.assertEqual(realm_audit_log.extra_data, expected_extra_data)