Issue#4 : fix(cart) display error for invalid or expired coupon codes #11

Merged
amit merged 1 commits from fix/issue-4-expired-coupon-validation into main 2026-10-06 12:18:04 +00:00
2 changed files with 55 additions and 10 deletions

View File

@ -466,9 +466,18 @@ export default function App() {
currency: currency
}, { headers });
setPricingCalc(res.data);
} catch (err) {
const calcData = res.data;
if (appliedCoupon && (!calcData.coupon_applied || calcData.coupon_applied === 'None')) {
calcData.coupon_error = calcData.coupon_error || 'Invalid or expired coupon code.';
}
setPricingCalc(calcData);
} catch (err: any) {
console.error('Pricing engine calculation error:', err);
const errorMsg = err.response?.data?.detail || 'Invalid or expired coupon code.';
setPricingCalc((prev: any) => ({
...(prev || {}),
coupon_error: errorMsg
}));
} finally {
setLoadingPricing(false);
}
@ -525,6 +534,7 @@ export default function App() {
const removePromoCode = () => {
setAppliedCoupon(null);
setCouponCode('');
setPricingCalc((prev: any) => (prev ? { ...prev, coupon_error: null } : null));
};
const isModuleUnlocked = (code: string) => {
@ -574,10 +584,17 @@ export default function App() {
setCheckoutLoading(true);
setVerificationResult(null);
const isCouponValid = Boolean(
pricingCalc &&
pricingCalc.coupon_applied &&
pricingCalc.coupon_applied !== 'None' &&
!pricingCalc.coupon_error
);
try {
const orderRes = await axios.post(`${API_BASE}/api/checkout/create-order`, {
cart_item_codes: cart.map(item => item.code),
coupon_code: appliedCoupon || undefined,
coupon_code: isCouponValid ? appliedCoupon : undefined,
currency: currency
}, {
headers: { 'Authorization': `Bearer ${userToken}` }

View File

@ -141,9 +141,11 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
</button>
</div>
{appliedCoupon &&
(pricingCalc?.coupon_error ? (
(pricingCalc?.coupon_error || !pricingCalc?.coupon_applied || pricingCalc?.coupon_applied === 'None' ? (
<div className="flex justify-between items-center bg-rose-50 border border-rose-200 text-rose-700 text-xs px-3 py-2 rounded-xl font-bold">
<span className="flex items-center gap-1">⚠️ {pricingCalc.coupon_error}</span>
<span className="flex items-center gap-1">
⚠️ {pricingCalc?.coupon_error || 'Invalid or expired coupon code.'}
</span>
<button
onClick={removePromoCode}
className="text-rose-700 hover:text-rose-900 font-bold ml-2 cursor-pointer"
@ -183,18 +185,44 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
: `$${pricingCalc.base_subtotal}`}
</span>
</div>
{pricingCalc.total_discounts > 0 && (
{pricingCalc.bundle_discount_amount > 0 && (
<div className="flex justify-between text-xs text-emerald-700 font-bold">
<span>Total Discounts</span>
<span>Bundle Discount</span>
<span>
-{currency === 'INR'
? `₹${pricingCalc.total_discounts}`
? `₹${pricingCalc.bundle_discount_amount}`
: currency === 'EUR'
? `€${pricingCalc.total_discounts}`
: `$${pricingCalc.total_discounts}`}
? `€${pricingCalc.bundle_discount_amount}`
: `$${pricingCalc.bundle_discount_amount}`}
</span>
</div>
)}
{pricingCalc.coupon_discount_amount > 0 && (
<div className="flex justify-between text-xs text-emerald-700 font-bold">
<span>Coupon Discount ({appliedCoupon})</span>
<span>
-{currency === 'INR'
? `₹${pricingCalc.coupon_discount_amount}`
: currency === 'EUR'
? `€${pricingCalc.coupon_discount_amount}`
: `$${pricingCalc.coupon_discount_amount}`}
</span>
</div>
)}
{pricingCalc.total_discounts > 0 &&
!pricingCalc.bundle_discount_amount &&
!pricingCalc.coupon_discount_amount && (
<div className="flex justify-between text-xs text-emerald-700 font-bold">
<span>Total Discounts</span>
<span>
-{currency === 'INR'
? `₹${pricingCalc.total_discounts}`
: currency === 'EUR'
? `€${pricingCalc.total_discounts}`
: `$${pricingCalc.total_discounts}`}
</span>
</div>
)}
{pricingCalc.convenience_fee > 0 && (
<div className="flex justify-between text-xs text-slate-600 font-bold">
<span>Gateway fee ({pricingCalc.convenience_fee_rate}%)</span>