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: Update link_customer_to_stripe_id management command.

Lauryn Menard6c45e0f17973
diff --git a/corporate/management/commands/link_customer_to_stripe_id.py b/corporate/management/commands/link_customer_to_stripe_id.py
index ec9241466cc..fc3784f5954 100644
--- a/corporate/management/commands/link_customer_to_stripe_id.py
+++ b/corporate/management/commands/link_customer_to_stripe_id.py
@@ -4,14 +4,12 @@ from django.conf import settings
from django.core.management.base import CommandError, CommandParser
from typing_extensions import override
-from zerver.lib.management import ZulipBaseCommand
-
if settings.BILLING_ENABLED:
- from corporate.models.customers import Customer
+ from corporate.lib.billing_management import BillingSessionCommand
from corporate.models.plans import get_current_plan_by_customer
-class Command(ZulipBaseCommand):
+class Command(BillingSessionCommand):
help = """Link a Customer object to a Stripe customer ID."""
@override
@@ -22,12 +20,7 @@ class Command(ZulipBaseCommand):
required=True,
help="The ID of the customer in Stripe.",
)
- parser.add_argument(
- "--customer-id",
- dest="customer_id",
- required=True,
- help="The ID of the Customer object in the database.",
- )
+ self.add_billing_entity_args(parser)
@override
def handle(self, *args: Any, **options: Any) -> None:
@@ -35,12 +28,28 @@ class Command(ZulipBaseCommand):
raise CommandError("Billing system not enabled.")
stripe_id = options["stripe_id"]
- customer_id = options["customer_id"]
+ billing_session = self.get_billing_session_from_args(options)
+ customer = billing_session.get_customer()
+
+ if customer is None:
+ print(f"No Customer object for {billing_session.billing_entity_display_name}.")
+ no_customer_object_prompt = input(
+ f"Do you want to create one and link it to Stripe customer with ID {stripe_id}? [Y/n]"
+ )
+ print()
+ if not (
+ no_customer_object_prompt.lower() == "y"
+ or no_customer_object_prompt.lower() == ""
+ or no_customer_object_prompt.lower() == "yes"
+ ):
+ return
- try:
- customer = Customer.objects.get(id=customer_id)
- except Customer.DoesNotExist:
- raise CommandError(f"Customer object with ID {customer_id} does not exist. Aborting.")
+ print("Creating Customer object...")
+ customer = billing_session.update_or_create_customer()
+ print(f"Linking {customer} to Stripe customer with ID {stripe_id}...")
+ billing_session.link_stripe_customer_id(stripe_id)
+ print("Done!")
+ return
plan = get_current_plan_by_customer(customer)
if plan is not None and plan.is_a_paid_plan():
@@ -56,12 +65,8 @@ class Command(ZulipBaseCommand):
or existing_id_prompt.lower() == ""
or existing_id_prompt.lower() == "yes"
):
- # Exit without registering; no need to print anything
- # special, as the "n" reply to the query is clear
- # enough about what happened.
return
print(f"Linking {customer} to Stripe customer with ID {stripe_id}...")
- customer.stripe_customer_id = stripe_id
- customer.save(update_fields=["stripe_customer_id"])
+ billing_session.link_stripe_customer_id(stripe_id)
print("Done!")