Merge pull request 'fixed-issue(32): improved cupon validaation and removed payment gateway surcharge' (#11) from cupon-validation-fix into main

Reviewed-on: https://10.10.0.4/AcuPedia/acupedia-api/pulls/11
Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
Amit Salunke 2026-10-14 09:28:51 +00:00
commit 1d50a9d2bc
5 changed files with 35 additions and 17 deletions

2
.git_api_info Normal file
View File

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

View File

@ -118,7 +118,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 {}
@ -127,7 +127,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,13 +56,18 @@ 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)
grand = q(net)
return {

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):