Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bililng: Fix incorrect price per license on billing page.
Aman Agrawal27ffcc65764b
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 407920411ec..6910d3ec1ca 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.jsonnew file mode 100644index 00000000000..87ad38f284e--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_card--Customer.retrieve.4.json@@ -0,0 +1,79 @@+{+ "address": null,+ "balance": 0,+ "created": 1010000001,+ "currency": null,+ "default_currency": null,+ "default_source": null,+ "delinquent": false,+ "description": "zulip (Zulip Dev)",+ "discount": null,+ "email": "[email protected]",+ "id": "cus_NORMALIZED0001",+ "invoice_prefix": "NORMA01",+ "invoice_settings": {+ "custom_fields": null,+ "default_payment_method": {+ "billing_details": {+ "address": {+ "city": null,+ "country": null,+ "line1": null,+ "line2": null,+ "postal_code": null,+ "state": null+ },+ "email": null,+ "name": null,+ "phone": null+ },+ "card": {+ "brand": "visa",+ "checks": {+ "address_line1_check": null,+ "address_postal_code_check": null,+ "cvc_check": "pass"+ },+ "country": "US",+ "exp_month": 11,+ "exp_year": 2024,+ "fingerprint": "NORMALIZED000001",+ "funding": "credit",+ "generated_from": null,+ "last4": "4242",+ "networks": {+ "available": [+ "visa"+ ],+ "preferred": null+ },+ "three_d_secure_usage": {+ "supported": true+ },+ "wallet": null+ },+ "created": 1010000002,+ "customer": "cus_NORMALIZED0001",+ "id": "pm_1OAbdVDEQaroqDjsOzvEMpeM",+ "livemode": false,+ "metadata": {},+ "object": "payment_method",+ "type": "card"+ },+ "footer": null,+ "rendering_options": null+ },+ "livemode": false,+ "metadata": {+ "realm_id": "1",+ "realm_str": "zulip"+ },+ "name": null,+ "next_invoice_sequence": 1,+ "object": "customer",+ "phone": null,+ "preferred_locales": [],+ "shipping": null,+ "tax_exempt": "none",+ "test_clock": null+}diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 49a6aff7a5d..4e507ab80eb 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -1208,6 +1208,16 @@ class StripeTest(StripeTestCase):invoice_plans_as_needed(add_months(free_trial_end_date, 12))[invoice0, invoice1, invoice2] = stripe.Invoice.list(customer=stripe_customer.id)+ # Check /billing/ has correct information for fixed price customers.+ plan.fixed_price = 127+ plan.price_per_license = None+ plan.save(update_fields=["fixed_price", "price_per_license"])+ with patch("corporate.views.billing_page.timezone_now", return_value=self.now):+ response = self.client_get("/billing/")+ self.assert_in_success_response(["$1.27"], response)+ # Don't show price breakdown+ self.assert_not_in_success_response(["{self.seat_count} x"], response)+@mock_stripe(tested_timestamp_fields=["created"])def test_free_trial_upgrade_by_card_from_onboarding_page(self, *mocks: Mock) -> None:user = self.example_user("hamlet")diff --git a/corporate/views/billing_page.py b/corporate/views/billing_page.pyindex 1f851e24fe7..fd5550beda2 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.tsindex d435635d066..fc216f90e65 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.pyexport function format_money(cents: number): string {// allow for small floating point errorscents = Math.ceil(cents - 0.001);