2026-06-06 11:52:06 +08:00
|
|
|
|
package pay
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
|
|
|
|
"qnc-server/app/main/model"
|
|
|
|
|
|
"qnc-server/pkg/lzkit/lzUtils"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// fulfillQueryOrderPaid 幂等将查询订单标记为已支付并触发报告生成
|
|
|
|
|
|
// wechatPaidFen:微信侧实付金额(分),>0 时须与订单 amount 一致
|
|
|
|
|
|
func fulfillQueryOrderPaid(ctx context.Context, svcCtx *svc.ServiceContext, order *model.Order, platformOrderID string, wechatPaidFen int64) (credited bool, err error) {
|
|
|
|
|
|
if order.Status == "paid" {
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
orderFen := lzUtils.ToWechatAmount(order.Amount)
|
|
|
|
|
|
if wechatPaidFen > 0 && wechatPaidFen != orderFen {
|
2026-06-06 17:03:08 +08:00
|
|
|
|
if model.IsXpayOrder(order) {
|
|
|
|
|
|
order.Amount = lzUtils.RoundMoney(float64(wechatPaidFen) / 100)
|
|
|
|
|
|
orderFen = wechatPaidFen
|
|
|
|
|
|
logx.WithContext(ctx).Infof("[xpay] 同步微信实付金额 order_no=%s amount=%.2f fen=%d", order.OrderNo, order.Amount, wechatPaidFen)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logx.WithContext(ctx).Errorf("[xpay] 金额不一致 order_no=%s order_fen=%d wechat_fen=%d", order.OrderNo, orderFen, wechatPaidFen)
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
2026-06-06 11:52:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
order.Status = "paid"
|
|
|
|
|
|
order.PayTime = sql.NullTime{Time: time.Now(), Valid: true}
|
|
|
|
|
|
if platformOrderID != "" {
|
|
|
|
|
|
order.PlatformOrderId = sql.NullString{String: platformOrderID, Valid: true}
|
|
|
|
|
|
}
|
2026-06-06 17:03:08 +08:00
|
|
|
|
if order.PaymentScene == "" || order.PaymentScene == model.PaymentSceneApp {
|
|
|
|
|
|
order.PaymentScene = model.PaymentSceneMiniProgram
|
2026-06-06 11:52:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if updateErr := svcCtx.OrderModel.UpdateWithVersion(ctx, nil, order); updateErr != nil {
|
|
|
|
|
|
return false, updateErr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if asyncErr := svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
|
|
|
|
|
logx.WithContext(ctx).Errorf("[xpay] SendQueryTask 失败 order_id=%s err=%v", order.Id, asyncErr)
|
|
|
|
|
|
return true, asyncErr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logx.WithContext(ctx).Infof("[xpay] 订单到账 order_no=%s order_id=%s", order.OrderNo, order.Id)
|
|
|
|
|
|
return true, nil
|
|
|
|
|
|
}
|