billing: Include account balance in prorated credits calculation.

This commit is contained in:
Rishi Gupta 2018-08-30 17:47:28 -07:00
parent bebd93b571
commit b7c326a161
1 changed files with 10 additions and 10 deletions

View File

@ -244,6 +244,7 @@ def billing_home(request: HttpRequest) -> HttpResponse:
stripe_customer = stripe_get_customer(customer.stripe_customer_id) stripe_customer = stripe_get_customer(customer.stripe_customer_id)
subscription = extract_current_subscription(stripe_customer) subscription = extract_current_subscription(stripe_customer)
prorated_charges = stripe_customer.account_balance
if subscription: if subscription:
plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname] plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname]
seat_count = subscription.quantity seat_count = subscription.quantity
@ -251,12 +252,8 @@ def billing_home(request: HttpRequest) -> HttpResponse:
renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format( renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(
dt=timestamp_to_datetime(subscription.current_period_end)) dt=timestamp_to_datetime(subscription.current_period_end))
upcoming_invoice = stripe_get_upcoming_invoice(customer.stripe_customer_id) upcoming_invoice = stripe_get_upcoming_invoice(customer.stripe_customer_id)
renewal_amount = subscription.plan.amount * subscription.quantity / 100. renewal_amount = subscription.plan.amount * subscription.quantity
prorated_credits = 0 prorated_charges += upcoming_invoice.total - renewal_amount
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
# Can only get here by subscribing and then downgrading. We don't support downgrading # Can only get here by subscribing and then downgrading. We don't support downgrading
# yet, but keeping this code here since we will soon. # yet, but keeping this code here since we will soon.
else: # nocoverage else: # nocoverage
@ -264,7 +261,10 @@ def billing_home(request: HttpRequest) -> HttpResponse:
seat_count = 0 seat_count = 0
renewal_date = '' renewal_date = ''
renewal_amount = 0 renewal_amount = 0
prorated_credits = 0 prorated_credits = 0
if prorated_charges < 0: # nocoverage
prorated_credits = -prorated_charges
prorated_charges = 0 prorated_charges = 0
payment_method = None payment_method = None
@ -275,10 +275,10 @@ def billing_home(request: HttpRequest) -> HttpResponse:
'plan_name': plan_name, 'plan_name': plan_name,
'seat_count': seat_count, 'seat_count': seat_count,
'renewal_date': renewal_date, 'renewal_date': renewal_date,
'renewal_amount': '{:,.2f}'.format(renewal_amount), 'renewal_amount': '{:,.2f}'.format(renewal_amount / 100.),
'payment_method': payment_method, 'payment_method': payment_method,
'prorated_charges': '{:,.2f}'.format(prorated_charges), 'prorated_charges': '{:,.2f}'.format(prorated_charges / 100.),
'prorated_credits': '{:,.2f}'.format(prorated_credits), 'prorated_credits': '{:,.2f}'.format(prorated_credits / 100.),
}) })
return render(request, 'zilencer/billing.html', context=context) return render(request, 'zilencer/billing.html', context=context)