Compare commits
No commits in common. "a4d37fcf1acf0444f1927197a185ab80af49bfd9" and "cd10f8548d346126589497a938ae2e62b2803515" have entirely different histories.
a4d37fcf1a
...
cd10f8548d
|
|
@ -46,8 +46,8 @@ class CartRepository:
|
||||||
if len(parts) == 2 and parts[1] in known_currencies:
|
if len(parts) == 2 and parts[1] in known_currencies:
|
||||||
base = parts[0]
|
base = parts[0]
|
||||||
|
|
||||||
for cand in [f"{base}_{curr}", f"{base}_USD" if curr == "EUR" else None, code, base]:
|
for cand in [f"{base}_{curr}", code, base]:
|
||||||
if cand and cand not in cands:
|
if cand not in cands:
|
||||||
cands.append(cand)
|
cands.append(cand)
|
||||||
code_candidates_map[code] = cands
|
code_candidates_map[code] = cands
|
||||||
candidates.update(cands)
|
candidates.update(cands)
|
||||||
|
|
@ -81,15 +81,12 @@ class CartRepository:
|
||||||
continue
|
continue
|
||||||
fee = plan.total_fee if plan.total_fee is not None else plan.module_fee
|
fee = plan.total_fee if plan.total_fee is not None else plan.module_fee
|
||||||
plan_curr = plan.currency_code.upper()
|
plan_curr = plan.currency_code.upper()
|
||||||
if plan_curr == "INR" and curr in {"USD", "EUR"}:
|
if plan_curr == "INR" and curr == "USD":
|
||||||
matches[code] = (plan, max(Decimal("1.00"), (Decimal(str(fee)) / Decimal("80")).quantize(Decimal("1.00"))))
|
matches[code] = (plan, max(Decimal("1.00"), (Decimal(str(fee)) / Decimal("80")).quantize(Decimal("1.00"))))
|
||||||
break
|
break
|
||||||
elif plan_curr in {"USD", "EUR"} and curr == "INR":
|
elif plan_curr == "USD" and curr == "INR":
|
||||||
matches[code] = (plan, (Decimal(str(fee)) * Decimal("80")).quantize(Decimal("1.00")))
|
matches[code] = (plan, (Decimal(str(fee)) * Decimal("80")).quantize(Decimal("1.00")))
|
||||||
break
|
break
|
||||||
elif plan_curr in {"USD", "EUR"} and curr in {"USD", "EUR"}:
|
|
||||||
matches[code] = (plan, Decimal(str(fee)))
|
|
||||||
break
|
|
||||||
elif fee is not None:
|
elif fee is not None:
|
||||||
matches[code] = (plan, Decimal(str(fee)))
|
matches[code] = (plan, Decimal(str(fee)))
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -13,39 +13,12 @@ from app.features.cart.service import CartService
|
||||||
from app.features.payments import generate_next_invoice_number, get_razorpay_service, record_coupon_redemption
|
from app.features.payments import generate_next_invoice_number, get_razorpay_service, record_coupon_redemption
|
||||||
from .schema import CheckoutOrderRequest, CheckoutOrderResponse, CheckoutVerifyRequest
|
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:
|
class CheckoutService:
|
||||||
def __init__(self, *, session: AsyncSession, cart_repository: CartRepository) -> None:
|
def __init__(self, *, session: AsyncSession, cart_repository: CartRepository) -> None:
|
||||||
self.session = session
|
self.session = session
|
||||||
self.cart_service = CartService(cart_repository)
|
self.cart_service = CartService(cart_repository)
|
||||||
|
|
||||||
async def create_order(self, user: AppUser, request: CheckoutOrderRequest) -> CheckoutOrderResponse:
|
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)
|
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:
|
if calc.grand_total <= 0:
|
||||||
raise ValueError("Grand total must be greater than zero to checkout.")
|
raise ValueError("Grand total must be greater than zero to checkout.")
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,6 @@ async def get_public_plans(session: AsyncSession = Depends(get_db_session)) -> l
|
||||||
"billing_period": getattr(p.billing_period, "value", p.billing_period),
|
"billing_period": getattr(p.billing_period, "value", p.billing_period),
|
||||||
"price_inr": 0.0,
|
"price_inr": 0.0,
|
||||||
"price_usd": 0.0,
|
"price_usd": 0.0,
|
||||||
"price_eur": 0.0,
|
|
||||||
"is_active": p.is_active,
|
"is_active": p.is_active,
|
||||||
"badge_text": meta.get("badge_text"),
|
"badge_text": meta.get("badge_text"),
|
||||||
"heading": meta.get("heading"),
|
"heading": meta.get("heading"),
|
||||||
|
|
@ -64,7 +63,6 @@ async def get_public_plans(session: AsyncSession = Depends(get_db_session)) -> l
|
||||||
grp = groups[base_code]
|
grp = groups[base_code]
|
||||||
if is_usd:
|
if is_usd:
|
||||||
grp["price_usd"] = fee
|
grp["price_usd"] = fee
|
||||||
grp["price_eur"] = fee # EUR equals USD for now
|
|
||||||
else:
|
else:
|
||||||
grp["price_inr"] = fee
|
grp["price_inr"] = fee
|
||||||
grp["name"] = p.name
|
grp["name"] = p.name
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
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")
|
|
||||||
Loading…
Reference in New Issue