This commit is contained in:
Mrx
2026-06-06 17:03:08 +08:00
parent a85436950e
commit 35e9191981
28 changed files with 666 additions and 286 deletions

View File

@@ -0,0 +1,87 @@
package pay
import (
"context"
"strings"
"qnc-server/app/main/api/internal/service"
"qnc-server/app/main/api/internal/svc"
"qnc-server/app/main/model"
)
// XpayDeliverResult 同步 xpay 支付状态(查微信单 → 本地到账 → 异步查报告)
type XpayDeliverResult struct {
Credited bool `json:"credited"`
Notified bool `json:"notified"`
WechatDetail string `json:"wechat_detail"`
Errors []string `json:"errors"`
Message string `json:"message"`
}
// DeliverXpayQueryOrder 按商户订单号同步微信虚拟支付到账(不通知微信发货,发货在报告成功后)
func DeliverXpayQueryOrder(ctx context.Context, svcCtx *svc.ServiceContext, orderNo string) (*XpayDeliverResult, error) {
order, err := svcCtx.OrderModel.FindOneByOrderNo(ctx, orderNo)
if err != nil {
return nil, err
}
resp := &XpayDeliverResult{WechatDetail: "ok"}
if order.Status == model.OrderStatusPaid {
resp.Message = "订单已支付,无需同步"
return resp, nil
}
if !model.IsXpayOrder(order) {
resp.Errors = append(resp.Errors, "非小程序虚拟支付订单")
resp.Message = "非小程序虚拟支付订单"
return resp, nil
}
if svcCtx.XpayService == nil || !svcCtx.XpayService.Enabled() {
resp.Errors = append(resp.Errors, "虚拟支付未启用")
resp.Message = "虚拟支付未启用"
return resp, nil
}
openid, err := svcCtx.XpayService.GetWxMiniOpenID(ctx, svcCtx.UserAuthModel, order.UserId)
if err != nil {
resp.Errors = append(resp.Errors, err.Error())
resp.Message = "获取用户 openid 失败"
return resp, nil
}
status, qErr := svcCtx.XpayService.QueryOrder(ctx, openid, order.OrderNo, "")
if qErr != nil {
resp.Errors = append(resp.Errors, qErr.Error())
resp.WechatDetail = qErr.Error()
resp.Message = "查询微信订单失败"
return resp, nil
}
if !service.IsXpayPaidStatus(status.Status) {
resp.Errors = append(resp.Errors, "微信侧订单未支付")
resp.Message = "微信侧订单未支付"
return resp, nil
}
platformOrderID := status.WxOrderID
credited, fulfillErr := fulfillQueryOrderPaid(ctx, svcCtx, order, platformOrderID, status.PaidFee)
resp.Credited = credited
if fulfillErr != nil {
resp.Errors = append(resp.Errors, fulfillErr.Error())
}
resp.Message = buildXpayDeliverMessage(resp)
return resp, nil
}
func buildXpayDeliverMessage(r *XpayDeliverResult) string {
if len(r.Errors) > 0 {
return strings.Join(r.Errors, "")
}
if r.Credited {
return "同步成功,订单已到账并开始查询报告"
}
return "未执行到账"
}