billing: Separate initial upgrade logic to another function.

This commit is contained in:
Vishnu Ks
2018-07-27 19:07:04 +05:30
committed by Rishi Gupta
parent 97d93ec8cb
commit 8643c681f0
2 changed files with 29 additions and 24 deletions

View File

@@ -9,6 +9,7 @@ from django.conf import settings
from django.db import transaction
from django.utils.translation import ugettext as _
from django.core.signing import Signer
from django.core import signing
import stripe
from zerver.lib.exceptions import JsonableError
@@ -177,3 +178,27 @@ def do_subscribe_customer_to_plan(stripe_customer: stripe.Customer, stripe_plan_
event_time=timestamp_to_datetime(stripe_subscription.created),
requires_billing_update=True,
extra_data=ujson.dumps({'quantity': current_seat_count}))
def process_initial_upgrade(user: UserProfile, plan: str, signed_seat_count: str,
salt: str, stripe_token: str) -> Optional[str]:
if plan not in [Plan.CLOUD_ANNUAL, Plan.CLOUD_MONTHLY]:
billing_logger.warning("Tampered plan during realm upgrade. user: %s, realm: %s (%s)."
% (user.id, user.realm.id, user.realm.string_id))
return "Something went wrong. Please contact support@zulipchat.com"
try:
seat_count = int(unsign_string(signed_seat_count, salt))
except signing.BadSignature:
billing_logger.warning("Tampered seat count during realm upgrade. user: %s, realm: %s (%s)."
% (user.id, user.realm.id, user.realm.string_id))
return "Something went wrong. Please contact support@zulipchat.com"
stripe_customer = do_create_customer_with_payment_source(user, stripe_token)
do_subscribe_customer_to_plan(
stripe_customer=stripe_customer,
stripe_plan_id=Plan.objects.get(nickname=plan).stripe_plan_id,
seat_count=seat_count,
# TODO: billing address details are passed to us in the request;
# use that to calculate taxes.
tax_percent=0)
# TODO: check for errors and raise/send to frontend
return None