55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
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 {
|
||
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
|
||
}
|
||
}
|
||
|
||
order.Status = "paid"
|
||
order.PayTime = sql.NullTime{Time: time.Now(), Valid: true}
|
||
if platformOrderID != "" {
|
||
order.PlatformOrderId = sql.NullString{String: platformOrderID, Valid: true}
|
||
}
|
||
if order.PaymentScene == "" || order.PaymentScene == model.PaymentSceneApp {
|
||
order.PaymentScene = model.PaymentSceneMiniProgram
|
||
}
|
||
|
||
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
|
||
}
|