From d229948a43f4f2dc0cbaac76e8adb1a6670a2b01 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Wed, 22 Aug 2018 22:47:05 -0700 Subject: [PATCH] billing: Make source optional when creating stripe customer. --- stubs/stripe/__init__.pyi | 2 +- zilencer/lib/stripe.py | 11 ++++++----- zilencer/tests/test_stripe.py | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/stubs/stripe/__init__.pyi b/stubs/stripe/__init__.pyi index 3563c53a56..b3b2f4e2e5 100644 --- a/stubs/stripe/__init__.pyi +++ b/stubs/stripe/__init__.pyi @@ -19,7 +19,7 @@ class Customer: @staticmethod def create(description: str, email: str, metadata: Dict[str, Any], - source: str) -> Customer: + source: Optional[str]) -> Customer: ... @staticmethod diff --git a/zilencer/lib/stripe.py b/zilencer/lib/stripe.py index ff93fba90d..00cb15be7a 100644 --- a/zilencer/lib/stripe.py +++ b/zilencer/lib/stripe.py @@ -136,7 +136,7 @@ def extract_current_subscription(stripe_customer: stripe.Customer) -> Any: return None @catch_stripe_errors -def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer: +def do_create_customer(user: UserProfile, stripe_token: Optional[str]=None) -> stripe.Customer: realm = user.realm # We could do a better job of handling race conditions here, but if two # people from a realm try to upgrade at exactly the same time, the main @@ -154,9 +154,10 @@ def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) RealmAuditLog.objects.create( realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED, event_time=event_time) - RealmAuditLog.objects.create( - realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED, - event_time=event_time) + if stripe_token is not None: + RealmAuditLog.objects.create( + realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED, + event_time=event_time) Customer.objects.create( realm=realm, stripe_customer_id=stripe_customer.id, @@ -231,7 +232,7 @@ def do_subscribe_customer_to_plan(stripe_customer: stripe.Customer, stripe_plan_ def process_initial_upgrade(user: UserProfile, plan: Plan, seat_count: int, stripe_token: str) -> None: customer = Customer.objects.filter(realm=user.realm).first() if customer is None: - stripe_customer = do_create_customer_with_payment_source(user, stripe_token) + stripe_customer = do_create_customer(user, stripe_token=stripe_token) else: stripe_customer = do_replace_payment_source(user, stripe_token) do_subscribe_customer_to_plan( diff --git a/zilencer/tests/test_stripe.py b/zilencer/tests/test_stripe.py index 72c46c44c6..bcde8bb21e 100644 --- a/zilencer/tests/test_stripe.py +++ b/zilencer/tests/test_stripe.py @@ -17,7 +17,7 @@ from zerver.lib.test_classes import ZulipTestCase from zerver.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp from zerver.models import Realm, UserProfile, get_realm, RealmAuditLog from zilencer.lib.stripe import catch_stripe_errors, \ - do_create_customer_with_payment_source, do_subscribe_customer_to_plan, \ + do_subscribe_customer_to_plan, \ get_seat_count, extract_current_subscription, sign_string, unsign_string, \ get_next_billing_log_entry, run_billing_processor_one_step, \ BillingError, StripeCardError, StripeConnectionError