fixed-issue(32): improved cupon validaation and removed payment gateway surcharge #11

Merged
amit merged 1 commits from cupon-validation-fix into main 2026-10-14 09:28:52 +00:00
5 changed files with 40 additions and 25 deletions

2
.git_api_info Normal file
View File

@ -0,0 +1,2 @@
Branch: main
Commit: 29279bfd72a6fa296d1aa05a825bd88ca1f36c91

View File

@ -101,7 +101,7 @@ class CartRepository:
if now < s_date:
return None, "This coupon code is not active yet."
if now > e_date:
return None, "Invalid or expired coupon code."
return None, "This coupon code has expired."
if coupon.usage_limit > 0 and coupon.usage_count >= coupon.usage_limit:
return None, "Coupon usage limit reached."
metadata = getattr(coupon, "metadata_json", {}) if hasattr(coupon, "metadata_json") else {}
@ -110,7 +110,16 @@ class CartRepository:
currency_values = metadata.get("currency_values") if isinstance(metadata, dict) else None
if isinstance(currency_values, dict) and currency.upper() in currency_values:
value = Decimal(str(currency_values[currency.upper()]))
return PricingCoupon(code=coupon.code, category=coupon.coupon_category, discount_type=coupon.discount_type, value=value, description=coupon.description, max_discount_limit=Decimal(coupon.max_discount_limit)), None
min_spend = Decimal(str(coupon.min_purchase_amount or 0))
return PricingCoupon(
code=coupon.code,
category=coupon.coupon_category,
discount_type=coupon.discount_type,
value=value,
description=coupon.description,
max_discount_limit=Decimal(coupon.max_discount_limit or 0),
min_purchase_amount=min_spend,
), None
async def get_owned_categories(self, user_id: UUID) -> list[str]:
rows = (await self.session.execute(select(UserEntitlement, Module).join(Module, Module.id == UserEntitlement.module_id).where(UserEntitlement.user_id == user_id, UserEntitlement.status == EntitlementStatus.ACTIVE))).all()

View File

@ -23,6 +23,7 @@ class PricingCoupon:
value: Decimal
description: str | None = None
max_discount_limit: Decimal = Decimal("0")
min_purchase_amount: Decimal = Decimal("0")
class PricingEngine:
"""Preserves the legacy bundle/loyalty/fee rules while sourcing prices from commerce.access_plans."""
@ -55,18 +56,20 @@ class PricingEngine:
coupon_discount = Decimal("0")
coupon_text = "None"
if coupon:
coupon_text = f"Code: {coupon.code} - {coupon.description or ''}".rstrip(" -")
if coupon.discount_type in {"flat", "flat_amount"}:
coupon_discount = min(coupon.value, after_bundle)
elif coupon.discount_type in {"percentage", "percent"}:
coupon_discount = after_bundle * (coupon.value / Decimal("100"))
if coupon.max_discount_limit > 0:
coupon_discount = min(coupon_discount, coupon.max_discount_limit)
if coupon.min_purchase_amount > 0 and after_bundle < coupon.min_purchase_amount:
coupon_error = f"Minimum cart spend of {curr} {coupon.min_purchase_amount} required to use coupon {coupon.code}."
coupon_text = "None"
coupon_discount = Decimal("0")
else:
coupon_text = f"Code: {coupon.code} - {coupon.description or ''}".rstrip(" -")
if coupon.discount_type in {"flat", "flat_amount"}:
coupon_discount = min(coupon.value, after_bundle)
elif coupon.discount_type in {"percentage", "percent"}:
coupon_discount = after_bundle * (coupon.value / Decimal("100"))
if coupon.max_discount_limit > 0:
coupon_discount = min(coupon_discount, coupon.max_discount_limit)
net = max(Decimal("0"), after_bundle - coupon_discount)
fee_rate = Decimal("0.02") if curr == "INR" else Decimal("0.03")
fee = q(net * fee_rate)
gst = q(fee * Decimal("0.18")) if curr == "INR" else Decimal("0")
grand = q(net + fee + gst)
grand = q(net)
return {
"currency": curr,
"base_subtotal": float(q(base)),
@ -76,10 +79,10 @@ class PricingEngine:
"coupon_discount_amount": float(q(coupon_discount)),
"coupon_error": coupon_error,
"total_discounts": float(q(bundle + coupon_discount)),
"convenience_fee_rate": float(fee_rate * 100),
"convenience_fee": float(fee),
"taxable_amount": float(fee),
"gst_amount": float(gst),
"convenience_fee_rate": 0.0,
"convenience_fee": 0.0,
"taxable_amount": 0.0,
"gst_amount": 0.0,
"grand_total": float(grand),
"cart_items": [{"code": i.code, "name": i.name, "category": i.category, "price": float(q(i.price))} for i in items],
}

View File

@ -17,7 +17,8 @@ dependencies = [
"uvicorn[standard]>=0.20.0",
"python-dotenv>=1.0.0",
"PyYAML>=6.0",
"SQLAlchemy>=2.0.0",
"SQLAlchemy[asyncio]>=2.0.0",
"greenlet>=3.0.0",
"asyncpg>=0.28.0",
"python-jose[cryptography]>=3.3.0",
"passlib[bcrypt]>=1.7.4",

View File

@ -57,8 +57,8 @@ class TestCartCurrencyResolution(unittest.IsolatedAsyncioTestCase):
))
self.assertEqual(calc.currency, "USD")
self.assertEqual(calc.base_subtotal, 5.0)
self.assertEqual(calc.convenience_fee, 0.15)
self.assertEqual(calc.grand_total, 5.15)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.grand_total, 5.0)
self.assertEqual(len(calc.cart_items), 1)
self.assertEqual(calc.cart_items[0].code, "POINTS_ACU")
@ -69,9 +69,9 @@ class TestCartCurrencyResolution(unittest.IsolatedAsyncioTestCase):
))
self.assertEqual(calc.currency, "INR")
self.assertEqual(calc.base_subtotal, 300.0)
self.assertEqual(calc.convenience_fee, 6.0)
self.assertEqual(calc.gst_amount, 1.08)
self.assertEqual(calc.grand_total, 307.08)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.gst_amount, 0.0)
self.assertEqual(calc.grand_total, 300.0)
async def test_multi_item_usd_cart(self):
calc = await self.service.calculate(CartCalculationRequest(
@ -80,8 +80,8 @@ class TestCartCurrencyResolution(unittest.IsolatedAsyncioTestCase):
))
self.assertEqual(calc.currency, "USD")
self.assertEqual(calc.base_subtotal, 8.0)
self.assertEqual(calc.convenience_fee, 0.24)
self.assertEqual(calc.grand_total, 8.24)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.grand_total, 8.0)
self.assertEqual(len(calc.cart_items), 2)
async def test_unknown_plan_raises_error(self):