From eb48197f672b98f8ebca4e7f0c0992a3a83c2196 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Wed, 11 Jul 2018 20:42:59 +0530 Subject: [PATCH] billing: Get upcoming invoice only if there is a live subscription. --- zilencer/views.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/zilencer/views.py b/zilencer/views.py index 49c1d52962..a2ee96ed69 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -26,7 +26,7 @@ from zerver.views.push_notifications import validate_token from zilencer.lib.stripe import STRIPE_PUBLISHABLE_KEY, StripeError, \ do_create_customer_with_payment_source, do_subscribe_customer_to_plan, \ get_stripe_customer, get_upcoming_invoice, payment_source, \ - get_seat_count + get_seat_count, extract_current_subscription from zilencer.models import RemotePushDeviceToken, RemoteZulipServer, \ Customer, Plan @@ -205,25 +205,28 @@ def billing_home(request: HttpRequest) -> HttpResponse: context = {'admin_access': True} stripe_customer = get_stripe_customer(customer.stripe_customer_id) + subscription = extract_current_subscription(stripe_customer) - if stripe_customer.subscriptions: - subscription = stripe_customer.subscriptions.data[0] + if subscription: plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname] seat_count = subscription.quantity # Need user's timezone to do this properly renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format( dt=timestamp_to_datetime(subscription.current_period_end)) + upcoming_invoice = get_upcoming_invoice(customer.stripe_customer_id) renewal_amount = subscription.plan.amount * subscription.quantity / 100. - else: - plan_name = "Zulip Free" # nocoverage -- no way to get here yet - renewal_date = '' # nocoverage -- no way to get here yet - renewal_amount = 0 # nocoverage -- no way to get here yet - - prorated_credits = 0 - prorated_charges = get_upcoming_invoice(customer.stripe_customer_id).amount_due / 100. - renewal_amount - if prorated_charges < 0: - prorated_credits = -prorated_charges # nocoverage -- no way to get here yet - prorated_charges = 0 # nocoverage -- no way to get here yet + prorated_credits = 0 + prorated_charges = upcoming_invoice.amount_due / 100. - renewal_amount + if prorated_charges < 0: + prorated_credits = -prorated_charges # nocoverage -- no way to get here yet + prorated_charges = 0 # nocoverage + else: # nocoverage -- no way to get here yet + plan_name = "Zulip Free" + seat_count = 0 + renewal_date = '' + renewal_amount = 0 + prorated_credits = 0 + prorated_charges = 0 payment_method = None source = payment_source(stripe_customer)