billing: Make source optional when creating stripe customer.

This commit is contained in:
Rishi Gupta 2018-08-22 22:47:05 -07:00
parent 4ad8f20c28
commit d229948a43
3 changed files with 8 additions and 7 deletions

View File

@ -19,7 +19,7 @@ class Customer:
@staticmethod @staticmethod
def create(description: str, email: str, metadata: Dict[str, Any], def create(description: str, email: str, metadata: Dict[str, Any],
source: str) -> Customer: source: Optional[str]) -> Customer:
... ...
@staticmethod @staticmethod

View File

@ -136,7 +136,7 @@ def extract_current_subscription(stripe_customer: stripe.Customer) -> Any:
return None return None
@catch_stripe_errors @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 realm = user.realm
# We could do a better job of handling race conditions here, but if two # 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 # people from a realm try to upgrade at exactly the same time, the main
@ -154,6 +154,7 @@ def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str)
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED, realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED,
event_time=event_time) event_time=event_time)
if stripe_token is not None:
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED, realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED,
event_time=event_time) event_time=event_time)
@ -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: def process_initial_upgrade(user: UserProfile, plan: Plan, seat_count: int, stripe_token: str) -> None:
customer = Customer.objects.filter(realm=user.realm).first() customer = Customer.objects.filter(realm=user.realm).first()
if customer is None: 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: else:
stripe_customer = do_replace_payment_source(user, stripe_token) stripe_customer = do_replace_payment_source(user, stripe_token)
do_subscribe_customer_to_plan( do_subscribe_customer_to_plan(

View File

@ -17,7 +17,7 @@ from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp from zerver.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp
from zerver.models import Realm, UserProfile, get_realm, RealmAuditLog from zerver.models import Realm, UserProfile, get_realm, RealmAuditLog
from zilencer.lib.stripe import catch_stripe_errors, \ 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_seat_count, extract_current_subscription, sign_string, unsign_string, \
get_next_billing_log_entry, run_billing_processor_one_step, \ get_next_billing_log_entry, run_billing_processor_one_step, \
BillingError, StripeCardError, StripeConnectionError BillingError, StripeCardError, StripeConnectionError