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.
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user