From 97bb158c8e6b285635e85ad9c1f9e28f2a4ed653 Mon Sep 17 00:00:00 2001 From: Urvish Date: Fri, 20 Nov 2026 16:01:32 +0530 Subject: [PATCH] feat(checkout): enforce strict billing country currency validation and prevent international INR bypass --- app/features/checkout/service.py | 27 ++++++++++++++++ .../unit/test_checkout_currency_validation.py | 32 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/unit/test_checkout_currency_validation.py diff --git a/app/features/checkout/service.py b/app/features/checkout/service.py index e598198..0bfef9d 100644 --- a/app/features/checkout/service.py +++ b/app/features/checkout/service.py @@ -13,12 +13,39 @@ from app.features.cart.service import CartService from app.features.payments import generate_next_invoice_number, get_razorpay_service, record_coupon_redemption from .schema import CheckoutOrderRequest, CheckoutOrderResponse, CheckoutVerifyRequest +EUROPEAN_COUNTRIES = { + "germany", "france", "italy", "spain", "netherlands", "belgium", + "austria", "ireland", "sweden", "denmark", "finland", "portugal", + "greece", "poland", "czech republic", "hungary", "romania", + "iceland", "luxembourg", "croatia", "slovakia", "bulgaria", + "cyprus", "estonia", "latvia", "lithuania", "malta", "slovenia" +} + +def validate_user_country_currency(country: str | None, currency: str) -> None: + c = (country or "India").strip().lower() + curr = (currency or "").strip().upper() + if not curr: + raise ValueError("Currency must be specified for checkout.") + + if c == "india" or c in {"not configured", "none", "null"}: + if curr != "INR": + raise ValueError(f"Users with billing country India must pay in INR (received {curr}).") + elif c in EUROPEAN_COUNTRIES: + if curr not in {"EUR", "USD"}: + raise ValueError(f"Billing country '{country}' must pay in EUR or USD (received {curr}).") + else: + if curr == "INR": + raise ValueError("INR payments are restricted to domestic Indian billing profiles.") + if curr != "USD": + raise ValueError(f"Billing country '{country}' must pay in USD (received {curr}).") + class CheckoutService: def __init__(self, *, session: AsyncSession, cart_repository: CartRepository) -> None: self.session = session self.cart_service = CartService(cart_repository) async def create_order(self, user: AppUser, request: CheckoutOrderRequest) -> CheckoutOrderResponse: + validate_user_country_currency(user.country, request.currency) calc = await self.cart_service.calculate(CartCalculationRequest(cart_item_codes=request.cart_item_codes, coupon_code=request.coupon_code, currency=request.currency), user_id=user.id) if calc.grand_total <= 0: raise ValueError("Grand total must be greater than zero to checkout.") diff --git a/tests/unit/test_checkout_currency_validation.py b/tests/unit/test_checkout_currency_validation.py new file mode 100644 index 0000000..22680d9 --- /dev/null +++ b/tests/unit/test_checkout_currency_validation.py @@ -0,0 +1,32 @@ +import pytest +from app.features.checkout.service import validate_user_country_currency + +def test_india_allows_inr_only(): + validate_user_country_currency("India", "INR") + validate_user_country_currency("INDIA", "inr") + validate_user_country_currency(None, "INR") + validate_user_country_currency("", "INR") + + with pytest.raises(ValueError, match="Users with billing country India must pay in INR"): + validate_user_country_currency("India", "USD") + + with pytest.raises(ValueError, match="Users with billing country India must pay in INR"): + validate_user_country_currency("India", "EUR") + +def test_european_countries_allow_eur_and_usd(): + for eu_country in ["Germany", "France", "Italy", "Spain", "Netherlands", "Cyprus", "Estonia"]: + validate_user_country_currency(eu_country, "EUR") + validate_user_country_currency(eu_country, "USD") + + with pytest.raises(ValueError, match="must pay in EUR or USD"): + validate_user_country_currency(eu_country, "INR") + +def test_rest_of_world_allows_usd_only(): + for country in ["United States", "Canada", "Australia", "Singapore", "United Kingdom"]: + validate_user_country_currency(country, "USD") + + with pytest.raises(ValueError, match="INR payments are restricted"): + validate_user_country_currency(country, "INR") + + with pytest.raises(ValueError, match="must pay in USD"): + validate_user_country_currency(country, "EUR")