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.

corporate: Add `create_stripe_payment_intent` to BillingSession class.

Lauryn Menard6f7b75c9a354
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 6ebd430f9e4..7955ed75cbb 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -327,6 +327,14 @@ class StripeCustomerData:
metadata: Dict[str, Any]
+@dataclass
+class StripePaymentIntentData:
+ amount: int
+ description: str
+ plan_name: str
+ email: str
+
+
class AuditLogEventType(Enum):
STRIPE_CUSTOMER_CREATED = 1
STRIPE_CARD_CHANGED = 2
@@ -371,6 +379,18 @@ class BillingSession(ABC):
def get_data_for_stripe_customer(self) -> StripeCustomerData:
pass
+ @abstractmethod
+ def update_data_for_checkout_session_and_payment_intent(
+ self, metadata: Dict[str, Any]
+ ) -> Dict[str, Any]:
+ pass
+
+ @abstractmethod
+ def get_data_for_stripe_payment_intent(
+ self, price_per_license: int, licenses: int
+ ) -> StripePaymentIntentData:
+ pass
+
@abstractmethod
def update_or_create_customer(
self, stripe_customer_id: Optional[str] = None, *, defaults: Optional[Dict[str, Any]] = None
@@ -437,6 +457,29 @@ class BillingSession(ABC):
self.replace_payment_method(customer.stripe_customer_id, payment_method, True)
return customer
+ def create_stripe_payment_intent(
+ self, price_per_license: int, licenses: int, metadata: Dict[str, Any]
+ ) -> PaymentIntent:
+ customer = self.get_customer()
+ assert customer is not None and customer.stripe_customer_id is not None
+ payment_intent_data = self.get_data_for_stripe_payment_intent(price_per_license, licenses)
+ stripe_payment_intent = stripe.PaymentIntent.create(
+ amount=payment_intent_data.amount,
+ currency="usd",
+ customer=customer.stripe_customer_id,
+ description=payment_intent_data.description,
+ receipt_email=payment_intent_data.email,
+ confirm=False,
+ statement_descriptor=payment_intent_data.plan_name,
+ metadata=metadata,
+ )
+ payment_intent = PaymentIntent.objects.create(
+ customer=customer,
+ stripe_payment_intent_id=stripe_payment_intent.id,
+ status=PaymentIntent.get_status_integer_from_status_text(stripe_payment_intent.status),
+ )
+ return payment_intent
+
def create_stripe_checkout_session(
self,
metadata: Dict[str, Any],
@@ -590,6 +633,35 @@ class RealmBillingSession(BillingSession):
)
return realm_stripe_customer_data
+ @override
+ def update_data_for_checkout_session_and_payment_intent(
+ self, metadata: Dict[str, Any]
+ ) -> Dict[str, Any]:
+ updated_metadata = dict(
+ user_email=self.user.delivery_email,
+ realm_id=self.realm.id,
+ realm_str=self.realm.string_id,
+ user_id=self.user.id,
+ **metadata,
+ )
+ return updated_metadata
+
+ @override
+ def get_data_for_stripe_payment_intent(
+ self, price_per_license: int, licenses: int
+ ) -> StripePaymentIntentData:
+ # Support requests do not set any stripe billing information.
+ assert self.support_session is False
+ amount = price_per_license * licenses
+ description = f"Upgrade to Zulip Cloud Standard, ${price_per_license/100} x {licenses}"
+ plan_name = "Zulip Cloud Standard"
+ return StripePaymentIntentData(
+ amount=amount,
+ description=description,
+ plan_name=plan_name,
+ email=self.user.delivery_email,
+ )
+
@override
def update_or_create_customer(
self, stripe_customer_id: Optional[str] = None, *, defaults: Optional[Dict[str, Any]] = None
diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py
index 547cbf48f86..23157736720 100644
--- a/corporate/views/upgrade.py
+++ b/corporate/views/upgrade.py
@@ -29,7 +29,6 @@ from corporate.lib.stripe import (
from corporate.lib.support import get_support_url
from corporate.models import (
CustomerPlan,
- PaymentIntent,
Session,
ZulipSponsorshipRequest,
get_current_plan_by_customer,
@@ -97,7 +96,7 @@ def setup_upgrade_checkout_session_and_payment_intent(
price_per_license = get_price_per_license(
plan_tier, billing_schedule, customer.default_discount
)
- metadata = {
+ general_metadata = {
"billing_modality": billing_modality,
"billing_schedule": billing_schedule,
"licenses": licenses,
@@ -105,11 +104,10 @@ def setup_upgrade_checkout_session_and_payment_intent(
"price_per_license": price_per_license,
"seat_count": seat_count,
"type": "upgrade",
- "user_email": user.delivery_email,
- "realm_id": user.realm.id,
- "realm_str": user.realm.string_id,
- "user_id": user.id,
}
+ updated_metadata = billing_session.update_data_for_checkout_session_and_payment_intent(
+ general_metadata
+ )
if free_trial:
if onboarding:
session_type = Session.FREE_TRIAL_UPGRADE_FROM_ONBOARDING_PAGE
@@ -118,23 +116,12 @@ def setup_upgrade_checkout_session_and_payment_intent(
payment_intent = None
else:
session_type = Session.UPGRADE_FROM_BILLING_PAGE
- stripe_payment_intent = stripe.PaymentIntent.create(
- amount=price_per_license * licenses,
- currency="usd",
- customer=customer.stripe_customer_id,
- description=f"Upgrade to Zulip Cloud Standard, ${price_per_license/100} x {licenses}",
- receipt_email=user.delivery_email,
- confirm=False,
- statement_descriptor="Zulip Cloud Standard",
- metadata=metadata,
- )
- payment_intent = PaymentIntent.objects.create(
- customer=customer,
- stripe_payment_intent_id=stripe_payment_intent.id,
- status=PaymentIntent.get_status_integer_from_status_text(stripe_payment_intent.status),
+ payment_intent = billing_session.create_stripe_payment_intent(
+ price_per_license, licenses, updated_metadata
)
+
stripe_session = billing_session.create_stripe_checkout_session(
- metadata, session_type, payment_intent
+ updated_metadata, session_type, payment_intent
)
return stripe_session