From 7e08a07e29c16219a5a591f1b44b8ff2630bc086 Mon Sep 17 00:00:00 2001 From: EUForest Date: Fri, 6 Feb 2026 23:14:04 +0800 Subject: [PATCH] fix: resolve balance payment issue with coupon and fee calculation This commit fixes the inconsistent calculation logic between order preview and actual order creation, which caused balance payment failures when using coupons. Changes: - Standardized fee calculation order in both preCreateOrderLogic and purchaseLogic - Moved gift amount deduction after fee calculation to ensure correct total - Removed premature gift amount deduction before transaction in purchaseLogic - Gift amount is now only deducted within the database transaction The calculation order is now unified: 1. Apply coupon discount 2. Calculate handling fee based on post-coupon amount 3. Deduct gift amount from total (including fee) This ensures the preview amount matches the actual payment amount. --- .../logic/public/order/preCreateOrderLogic.go | 25 ++++++++-------- internal/logic/public/order/purchaseLogic.go | 29 +++++++++---------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/internal/logic/public/order/preCreateOrderLogic.go b/internal/logic/public/order/preCreateOrderLogic.go index 7c86330..e0e48a4 100644 --- a/internal/logic/public/order/preCreateOrderLogic.go +++ b/internal/logic/public/order/preCreateOrderLogic.go @@ -117,18 +117,6 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r couponAmount = calculateCoupon(amount, couponInfo) } amount -= couponAmount - - var deductionAmount int64 - // Check user deduction amount - if u.GiftAmount > 0 { - if u.GiftAmount >= amount { - deductionAmount = amount - amount = 0 - } else { - deductionAmount = u.GiftAmount - amount -= u.GiftAmount - } - } var feeAmount int64 if req.Payment != 0 { payment, err := l.svcCtx.PaymentModel.FindOne(l.ctx, req.Payment) @@ -139,8 +127,19 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r // Calculate the handling fee if amount > 0 { feeAmount = calculateFee(amount, payment) + amount += feeAmount + } + } + // Calculate gift amount deduction after fee calculation + var deductionAmount int64 + if u.GiftAmount > 0 && amount > 0 { + if u.GiftAmount >= amount { + deductionAmount = amount + amount = 0 + } else { + deductionAmount = u.GiftAmount + amount -= u.GiftAmount } - amount += feeAmount } resp = &types.PreOrderResponse{ diff --git a/internal/logic/public/order/purchaseLogic.go b/internal/logic/public/order/purchaseLogic.go index 06f611c..67769ea 100644 --- a/internal/logic/public/order/purchaseLogic.go +++ b/internal/logic/public/order/purchaseLogic.go @@ -130,19 +130,6 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P } // Calculate the handling fee amount -= coupon - var deductionAmount int64 - // Check user deduction amount - if u.GiftAmount > 0 { - if u.GiftAmount >= amount { - deductionAmount = amount - amount = 0 - u.GiftAmount -= deductionAmount - } else { - deductionAmount = u.GiftAmount - amount -= u.GiftAmount - u.GiftAmount = 0 - } - } // find payment method payment, err := l.svcCtx.PaymentModel.FindOne(l.ctx, req.Payment) if err != nil { @@ -155,6 +142,17 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P feeAmount = calculateFee(amount, payment) amount += feeAmount } + // Calculate gift amount deduction after fee calculation + var deductionAmount int64 + if u.GiftAmount > 0 && amount > 0 { + if u.GiftAmount >= amount { + deductionAmount = amount + amount = 0 + } else { + deductionAmount = u.GiftAmount + amount -= u.GiftAmount + } + } // query user is new purchase or renewal isNew, err := l.svcCtx.OrderModel.IsUserEligibleForNewOrder(l.ctx, u.Id) if err != nil { @@ -200,9 +198,10 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P } } - // update user deduction && Pre deduction ,Return after canceling the order + // update user gift amount and create deduction record if orderInfo.GiftAmount > 0 { - // update user deduction && Pre deduction ,Return after canceling the order + // deduct gift amount from user + u.GiftAmount -= orderInfo.GiftAmount if e := l.svcCtx.UserModel.Update(l.ctx, u, db); e != nil { l.Errorw("[Purchase] Database update error", logger.Field("error", e.Error()), logger.Field("user", u)) return e