Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Move `approve_sponsorship` to BillingSession class.
Lauryn Menardd06062c17926
diff --git a/analytics/views/support.py b/analytics/views/support.pyindex 18c7bbf8d99..294423096c1 100644--- a/analytics/views/support.py+++ b/analytics/views/support.py@@ -52,7 +52,6 @@ if settings.ZILENCER_ENABLED:from zilencer.models import RemoteZulipServerif settings.BILLING_ENABLED:- from corporate.lib.stripe import approve_sponsorship as do_approve_sponsorshipfrom corporate.lib.stripe import (downgrade_at_the_end_of_billing_cycle,downgrade_now_without_creating_additional_invoices,@@ -63,7 +62,11 @@ if settings.BILLING_ENABLED:update_sponsorship_status,void_all_open_invoices,)- from corporate.lib.support import attach_discount_to_realm, get_discount_for_realm+ from corporate.lib.support import (+ approve_realm_sponsorship,+ attach_discount_to_realm,+ get_discount_for_realm,+ )from corporate.models import (Customer,CustomerPlan,@@ -257,7 +260,7 @@ def support(update_sponsorship_status(realm, False, acting_user=acting_user)context["success_message"] = f"{realm.string_id} is no longer pending sponsorship."elif approve_sponsorship:- do_approve_sponsorship(realm, acting_user=acting_user)+ approve_realm_sponsorship(realm, acting_user=acting_user)context["success_message"] = f"Sponsorship approved for {realm.string_id}"elif modify_plan is not None:if modify_plan == "downgrade_at_billing_cycle_end":diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 7719fc01ea1..24f6745dc55 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -330,6 +330,7 @@ class AuditLogEventType(Enum):STRIPE_CARD_CHANGED = 2CUSTOMER_PLAN_CREATED = 3DISCOUNT_CHANGED = 4+ SPONSORSHIP_APPROVED = 5class BillingSessionAuditLogEventError(Exception):@@ -367,6 +368,10 @@ class BillingSession(ABC):) -> Customer:pass+ @abstractmethod+ def approve_sponsorship(self) -> None:+ pass+@catch_stripe_errorsdef create_stripe_customer(self, payment_method: Optional[str] = None) -> Customer:stripe_customer_data = self.get_data_for_stripe_customer()@@ -471,6 +476,8 @@ class RealmBillingSession(BillingSession):return RealmAuditLog.CUSTOMER_PLAN_CREATEDelif event_type is AuditLogEventType.DISCOUNT_CHANGED:return RealmAuditLog.REALM_DISCOUNT_CHANGED+ elif event_type is AuditLogEventType.SPONSORSHIP_APPROVED:+ return RealmAuditLog.REALM_SPONSORSHIP_APPROVEDelse:raise BillingSessionAuditLogEventError(event_type)@@ -533,6 +540,39 @@ class RealmBillingSession(BillingSession):)return customer+ @override+ def approve_sponsorship(self) -> None:+ # Sponsorship approval is only a support admin action.+ assert self.support_session++ from zerver.actions.message_send import internal_send_private_message+ from zerver.actions.realm_settings import do_change_realm_plan_type++ do_change_realm_plan_type(self.realm, Realm.PLAN_TYPE_STANDARD_FREE, acting_user=self.user)+ customer = self.get_customer()+ if customer is not None and customer.sponsorship_pending:+ customer.sponsorship_pending = False+ customer.save(update_fields=["sponsorship_pending"])+ self.write_to_audit_log(+ event_type=AuditLogEventType.SPONSORSHIP_APPROVED, event_time=timezone_now()+ )+ notification_bot = get_system_bot(settings.NOTIFICATION_BOT, self.realm.id)+ for user in self.realm.get_human_billing_admin_and_realm_owner_users():+ with override_language(user.default_language):+ # Using variable to make life easier for translators if these details change.+ message = _(+ "Your organization's request for sponsored hosting has been approved! "+ "You have been upgraded to {plan_name}, free of charge. {emoji}\n\n"+ "If you could {begin_link}list Zulip as a sponsor on your website{end_link}, "+ "we would really appreciate it!"+ ).format(+ plan_name="Zulip Cloud Standard",+ emoji=":tada:",+ begin_link="[",+ end_link="](/help/linking-to-zulip-website)",+ )+ internal_send_private_message(notification_bot, user, message)+def stripe_customer_has_credit_card_as_default_payment_method(stripe_customer: stripe.Customer,@@ -1099,39 +1139,6 @@ def update_sponsorship_status()-def approve_sponsorship(realm: Realm, *, acting_user: Optional[UserProfile]) -> None:- from zerver.actions.message_send import internal_send_private_message- from zerver.actions.realm_settings import do_change_realm_plan_type-- do_change_realm_plan_type(realm, Realm.PLAN_TYPE_STANDARD_FREE, acting_user=acting_user)- customer = get_customer_by_realm(realm)- if customer is not None and customer.sponsorship_pending:- customer.sponsorship_pending = False- customer.save(update_fields=["sponsorship_pending"])- RealmAuditLog.objects.create(- realm=realm,- acting_user=acting_user,- event_type=RealmAuditLog.REALM_SPONSORSHIP_APPROVED,- event_time=timezone_now(),- )- notification_bot = get_system_bot(settings.NOTIFICATION_BOT, realm.id)- for user in realm.get_human_billing_admin_and_realm_owner_users():- with override_language(user.default_language):- # Using variable to make life easier for translators if these details change.- message = _(- "Your organization's request for sponsored hosting has been approved! "- "You have been upgraded to {plan_name}, free of charge. {emoji}\n\n"- "If you could {begin_link}list Zulip as a sponsor on your website{end_link}, "- "we would really appreciate it!"- ).format(- plan_name="Zulip Cloud Standard",- emoji=":tada:",- begin_link="[",- end_link="](/help/linking-to-zulip-website)",- )- internal_send_private_message(notification_bot, user, message)--def is_sponsored_realm(realm: Realm) -> bool:return realm.plan_type == Realm.PLAN_TYPE_STANDARD_FREEdiff --git a/corporate/lib/support.py b/corporate/lib/support.pyindex e8855598ed5..34e89234856 100644--- a/corporate/lib/support.py+++ b/corporate/lib/support.py@@ -29,3 +29,8 @@ def get_discount_for_realm(realm: Realm) -> Optional[Decimal]:def attach_discount_to_realm(realm: Realm, discount: Decimal, *, acting_user: UserProfile) -> None:billing_session = RealmBillingSession(acting_user, realm)billing_session.attach_discount_to_customer(discount)+++def approve_realm_sponsorship(realm: Realm, *, acting_user: UserProfile) -> None:+ billing_session = RealmBillingSession(acting_user, realm)+ billing_session.approve_sponsorship()diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 1b222632d1f..12d3f433e43 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -50,7 +50,6 @@ from corporate.lib.stripe import (RealmBillingSession,StripeCardError,add_months,- approve_sponsorship,catch_stripe_errors,compute_plan_parameters,customer_has_credit_card_as_default_payment_method,@@ -80,7 +79,11 @@ from corporate.lib.stripe import (update_sponsorship_status,void_all_open_invoices,)-from corporate.lib.support import attach_discount_to_realm, get_discount_for_realm+from corporate.lib.support import (+ approve_realm_sponsorship,+ attach_discount_to_realm,+ get_discount_for_realm,+)from corporate.models import (Customer,CustomerPlan,@@ -2472,24 +2475,6 @@ class StripeTest(StripeTestCase):# card on file, and should show it# TODO- def test_approve_sponsorship(self) -> None:- user = self.example_user("hamlet")- approve_sponsorship(user.realm, acting_user=user)- realm = get_realm("zulip")- self.assertEqual(realm.plan_type, Realm.PLAN_TYPE_STANDARD_FREE)-- expected_message = (- "Your organization's request for sponsored hosting has been approved! You have been upgraded to Zulip Cloud Standard, free of charge. :tada:"- "\n\nIf you could [list Zulip as a sponsor on your website](/help/linking-to-zulip-website), we would really appreciate it!"- )- sender = get_system_bot(settings.NOTIFICATION_BOT, user.realm_id)- recipient_id = self.example_user("desdemona").recipient_id- message = Message.objects.filter(realm_id=realm.id, sender=sender.id).first()- assert message is not None- self.assertEqual(message.content, expected_message)- self.assertEqual(message.recipient.type, Recipient.PERSONAL)- self.assertEqual(message.recipient_id, recipient_id)-def test_update_sponsorship_status(self) -> None:lear = get_realm("lear")iago = self.example_user("iago")@@ -5057,3 +5042,23 @@ class TestSupportBillingHelpers(StripeTestCase):}self.assertEqual(realm_audit_log.extra_data, expected_extra_data)self.assertEqual(realm_audit_log.acting_user, support_admin)++ def test_approve_realm_sponsorship(self) -> None:+ realm = get_realm("zulip")+ self.assertNotEqual(realm.plan_type, Realm.PLAN_TYPE_STANDARD_FREE)++ support_admin = self.example_user("iago")+ approve_realm_sponsorship(realm, acting_user=support_admin)+ self.assertEqual(realm.plan_type, Realm.PLAN_TYPE_STANDARD_FREE)++ expected_message = (+ "Your organization's request for sponsored hosting has been approved! You have been upgraded to Zulip Cloud Standard, free of charge. :tada:"+ "\n\nIf you could [list Zulip as a sponsor on your website](/help/linking-to-zulip-website), we would really appreciate it!"+ )+ sender = get_system_bot(settings.NOTIFICATION_BOT, realm.id)+ recipient_id = self.example_user("desdemona").recipient_id+ message = Message.objects.filter(realm_id=realm.id, sender=sender.id).first()+ assert message is not None+ self.assertEqual(message.content, expected_message)+ self.assertEqual(message.recipient.type, Recipient.PERSONAL)+ self.assertEqual(message.recipient_id, recipient_id)