From 27ffcc65764b5091ecb2fa3434cc4bac6168970e Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Tue, 14 Nov 2023 02:20:29 +0000 Subject: [PATCH] bililng: Fix incorrect price per license on billing page. There is a discrepancy between price per license shown on billing and upgrade page for annual billing frequency due to difference in methods used to calculate the amount. To fix it, we use the same method on both the pages. I didn't use the helpers.format_money directly here since that would create a visual delay in showing the price per license which will not be nice considering that will be the only thing on this page being updated that way. The stripe fixture used is same as free_trial_upgrade_by_card--Customer.retrieve.3.json --- corporate/lib/stripe.py | 14 ++++++++++++++ ...ial_upgrade_by_card--Customer.retrieve.4.json | Bin 0 -> 1842 bytes corporate/tests/test_stripe.py | 10 ++++++++++ corporate/views/billing_page.py | 15 +++++++-------- web/src/billing/helpers.ts | 1 + 5 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 corporate/tests/stripe_fixtures/free_trial_upgrade_by_card--Customer.retrieve.4.json diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 407920411e..6910d3ec1c 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -68,6 +68,20 @@ STRIPE_API_VERSION = "2020-08-27" stripe.api_version = STRIPE_API_VERSION +# This function imitates the behavior of the format_money in billing/helpers.ts +def format_money(cents: float) -> str: + # allow for small floating point errors + cents = math.ceil(cents - 0.001) + if cents % 100 == 0: + precision = 0 + else: + precision = 2 + + dollars = cents / 100 + # Format the number as a string with the correct number of decimal places + return f"{dollars:.{precision}f}" + + def get_latest_seat_count(realm: Realm) -> int: return get_seat_count(realm, extra_non_guests_count=0, extra_guests_count=0) diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_card--Customer.retrieve.4.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_card--Customer.retrieve.4.json new file mode 100644 index 0000000000000000000000000000000000000000..87ad38f284ee51389ca9965fab382870f62f7c9b GIT binary patch literal 1842 zcmaJ?O>f&U488AH1UhZNFeE`vJFP%^7_hF`hV9Uz5EPkC)XI`7$!XG}|9vE7J5H9) zmmvJ+Bl-05ZZZ+7sv@K$K2Kb0?MD)=lvPdxil+QuM^G6m{6Cw{ru@x{qKy%p-jAIt zs8wsT)c?^X-$sr1aYW-l1GPx7LKo`y2jQ!uEqGRwilR zvg;5K8f7f`Y*b?*e__{8+Be0;P&Y!iNq)cnd3Eva>&@j78=G}nxAjJY3=wLxBTKeA zRVT>VxHVz-g18}a7%=-r)&^`fh7?sjE~iTM3%Tq*2=Y`n`GtYpTA)#HYWJ*bHC`ExiduI$U8@$_sK`%op>qql5h9 z;~9LnxVma(X@a){3{;BX0gXg=hkj5?rv}I-G)vl(!Eg*=-b4Ud$ppwQAlszYl<4Rp zQX7Jgg|2dJ;cTEMWsww)Qw@m-r&GS0_r-Q64m6av_=DRm3=L_ z`*i)hy}Sx=HNc67w}qx0*Fb$7135xfDl6*oP@Z4igQntf*^anms4?#Y;Zn|p0qDF% z?S1Cj54R|g!Yja4T1(GN+Y=W$vfJ7E#JDz2fpL^=Wa+yQDbBSBQ)axFgy}j;+!F#2 z4t80Zj(Zc(bHJ}ed29}C7na2~s9`ZCj3Ve)Nm{R!1?uPaj#X)6LMYeJ10b^6N!URX X7*a> None: user = self.example_user("hamlet") diff --git a/corporate/views/billing_page.py b/corporate/views/billing_page.py index 1f851e24fe..fd5550beda 100644 --- a/corporate/views/billing_page.py +++ b/corporate/views/billing_page.py @@ -14,6 +14,7 @@ from corporate.lib.stripe import ( do_change_plan_status, downgrade_at_the_end_of_billing_cycle, downgrade_now_without_creating_additional_invoices, + format_money, get_latest_seat_count, make_end_of_cycle_updates_if_needed, renewal_amount, @@ -194,15 +195,13 @@ def billing_home( ) billing_frequency = CustomerPlan.BILLING_SCHEDULES[plan.billing_schedule] - price_per_license_int = plan.price_per_license - if price_per_license_int is not None and billing_frequency == "Annual": - price_per_license_int = int(price_per_license_int / 12) - price_per_license = ( - cents_to_dollar_string(price_per_license_int) - if price_per_license_int is not None - else None - ) + if plan.price_per_license is None: + price_per_license = "" + elif billing_frequency == "Annual": + price_per_license = format_money(plan.price_per_license / 12) + else: + price_per_license = format_money(plan.price_per_license) context.update( plan_name=plan.name, diff --git a/web/src/billing/helpers.ts b/web/src/billing/helpers.ts index d435635d06..fc216f90e6 100644 --- a/web/src/billing/helpers.ts +++ b/web/src/billing/helpers.ts @@ -89,6 +89,7 @@ export function create_ajax_request( }); } +// This function imitates the behavior of the format_money in views/billing_page.py export function format_money(cents: number): string { // allow for small floating point errors cents = Math.ceil(cents - 0.001);