f
This commit is contained in:
BIN
app/main/api/api.exe
Normal file
BIN
app/main/api/api.exe
Normal file
Binary file not shown.
@@ -63,8 +63,11 @@ type (
|
||||
OrderNo string `json:"order_no" validate:"required"`
|
||||
}
|
||||
PaymentCheckResp {
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
WxSyncError string `json:"wx_sync_error,optional"` // query_order 失败时微信返回
|
||||
WxOrderStatus int `json:"wx_order_status,optional"` // 微信侧订单 status
|
||||
WxOrderDetail string `json:"wx_order_detail,optional"` // query_order 原始 order 摘要(排查用)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -42,46 +43,76 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单失败: %v", findErr)
|
||||
}
|
||||
|
||||
resp = &types.PaymentCheckResp{Type: "query", Status: order.Status}
|
||||
|
||||
// xpay 轮询:pending 时主动查微信单并到账(不通知微信发货)
|
||||
if order.Status == model.OrderStatusPending && model.IsXpayOrder(order) &&
|
||||
l.svcCtx.XpayService != nil && l.svcCtx.XpayService.Enabled() {
|
||||
if syncErr := l.syncXpayOrderStatus(order); syncErr != nil {
|
||||
l.Errorf("[xpay] 轮询查单失败 order_no=%s err=%v", req.OrderNo, syncErr)
|
||||
} else {
|
||||
order, _ = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
wxInfo, syncErr := l.syncXpayOrderStatus(order)
|
||||
resp.WxOrderStatus = wxInfo.Status
|
||||
resp.WxOrderDetail = wxInfo.RawOrder
|
||||
if syncErr != nil {
|
||||
resp.WxSyncError = syncErr.Error()
|
||||
l.Errorf("[xpay] 轮询查单失败 order_no=%s local_status=%s wx_status=%d wx_detail=%s err=%v",
|
||||
req.OrderNo, order.Status, wxInfo.Status, wxInfo.RawOrder, syncErr)
|
||||
} else if wxInfo.Status > 0 && !service.IsXpayPaidStatus(wxInfo.Status) {
|
||||
l.Infof("[xpay] 轮询查单未到账 order_no=%s wx_status=%d wx_detail=%s",
|
||||
req.OrderNo, wxInfo.Status, wxInfo.RawOrder)
|
||||
}
|
||||
order, _ = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
resp.Status = order.Status
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{Type: "query", Status: order.Status}, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) error {
|
||||
type xpaySyncInfo struct {
|
||||
Status int
|
||||
RawOrder string
|
||||
}
|
||||
|
||||
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) (xpaySyncInfo, error) {
|
||||
info := xpaySyncInfo{}
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
return info, err
|
||||
}
|
||||
if order.UserId != userID {
|
||||
return errors.New("无权查询此订单")
|
||||
return info, errors.New("无权查询此订单")
|
||||
}
|
||||
|
||||
openid, err := l.svcCtx.XpayService.GetWxMiniOpenID(l.ctx, l.svcCtx.UserAuthModel, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
return info, err
|
||||
}
|
||||
|
||||
l.Infof("[xpay] check sync start order_no=%s openid=%s env=%d", order.OrderNo, openid, l.svcCtx.XpayService.Env())
|
||||
|
||||
status, err := l.svcCtx.XpayService.QueryOrder(l.ctx, openid, order.OrderNo, "")
|
||||
if err != nil {
|
||||
return err
|
||||
return info, err
|
||||
}
|
||||
info.Status = status.Status
|
||||
info.RawOrder = status.RawOrder
|
||||
|
||||
if service.IsXpayPaidStatus(status.Status) {
|
||||
_, fulfillErr := fulfillQueryOrderPaid(l.ctx, l.svcCtx, order, status.WxOrderID, status.PaidFee)
|
||||
return fulfillErr
|
||||
if fulfillErr != nil {
|
||||
return info, fulfillErr
|
||||
}
|
||||
l.Infof("[xpay] check sync paid order_no=%s wx_order_id=%s paid_fee=%d",
|
||||
order.OrderNo, status.WxOrderID, status.PaidFee)
|
||||
return info, nil
|
||||
}
|
||||
|
||||
if service.IsXpayClosedStatus(status.Status) {
|
||||
order.Status = "closed"
|
||||
return l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order)
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
return info, updateErr
|
||||
}
|
||||
l.Infof("[xpay] check sync closed order_no=%s wx_status=%d", order.OrderNo, status.Status)
|
||||
return info, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
return info, nil
|
||||
}
|
||||
|
||||
@@ -125,11 +125,15 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
}
|
||||
xpayParams, buildErr := l.svcCtx.XpayService.BuildPayParams(l.ctx, userID, paymentTypeResp.outTradeNo, paymentTypeResp.productEn, paymentTypeResp.amount)
|
||||
if buildErr != nil {
|
||||
logx.WithContext(l.ctx).Errorf("[xpay] BuildPayParams FAIL user=%s order_no=%s amount=%.2f product_en=%s env=%d err=%v",
|
||||
userID, paymentTypeResp.outTradeNo, paymentTypeResp.amount, paymentTypeResp.productEn, l.svcCtx.XpayService.Env(), buildErr)
|
||||
if strings.Contains(buildErr.Error(), "过期") || strings.Contains(buildErr.Error(), "会话") {
|
||||
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, buildErr.Error()), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "构造虚拟支付参数失败: %v", buildErr)
|
||||
}
|
||||
logx.WithContext(l.ctx).Infof("[xpay] BuildPayParams OK user=%s order_no=%s env=%d signData=%s",
|
||||
userID, paymentTypeResp.outTradeNo, l.svcCtx.XpayService.Env(), xpayParams.SignData)
|
||||
prepayData = xpayParams
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,20 @@ type xpayOrderStatus struct {
|
||||
LeftFee int64 `json:"left_fee"`
|
||||
OrderID string `json:"order_id"`
|
||||
WxOrderID string `json:"wx_order_id"`
|
||||
ErrMsg string `json:"err_msg"`
|
||||
RawOrder string `json:"-"` // query_order 原始 order JSON,便于日志排查
|
||||
}
|
||||
|
||||
// XpayAPIError 微信 xpay 接口返回 errcode != 0
|
||||
type XpayAPIError struct {
|
||||
URI string
|
||||
ErrCode int
|
||||
ErrMsg string
|
||||
RawBody string
|
||||
}
|
||||
|
||||
func (e *XpayAPIError) Error() string {
|
||||
return fmt.Sprintf("xpay %s errcode=%d errmsg=%s body=%s", e.URI, e.ErrCode, e.ErrMsg, e.RawBody)
|
||||
}
|
||||
|
||||
func (s *XpayService) callAPI(ctx context.Context, uri, bodyJSON, sessionKey string, needSignature bool) (*xpayAPIResp, error) {
|
||||
@@ -220,18 +234,32 @@ func (s *XpayService) callAPI(ctx context.Context, uri, bodyJSON, sessionKey str
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
logx.WithContext(ctx).Infof("[xpay] req uri=%s env=%d body=%s", uri, s.Env(), bodyJSON)
|
||||
|
||||
httpResp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("[xpay] http err uri=%s err=%v", uri, err)
|
||||
return nil, err
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
respStr := string(respBody)
|
||||
|
||||
var apiResp xpayAPIResp
|
||||
if err := json.Unmarshal(respBody, &apiResp); err != nil {
|
||||
return nil, fmt.Errorf("解析 xpay 响应失败: %w, body=%s", err, string(respBody))
|
||||
logx.WithContext(ctx).Errorf("[xpay] parse err uri=%s http=%d body=%s err=%v", uri, httpResp.StatusCode, respStr, err)
|
||||
return nil, fmt.Errorf("解析 xpay 响应失败: %w, body=%s", err, respStr)
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[xpay] resp uri=%s http=%d errcode=%d errmsg=%s body=%s",
|
||||
uri, httpResp.StatusCode, apiResp.ErrCode, apiResp.ErrMsg, respStr)
|
||||
if apiResp.ErrCode != 0 {
|
||||
return &apiResp, &XpayAPIError{
|
||||
URI: uri,
|
||||
ErrCode: apiResp.ErrCode,
|
||||
ErrMsg: apiResp.ErrMsg,
|
||||
RawBody: respStr,
|
||||
}
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[xpay] call uri=%s body=%s", uri, string(respBody))
|
||||
return &apiResp, nil
|
||||
}
|
||||
|
||||
@@ -250,11 +278,12 @@ func (s *XpayService) QueryOrder(ctx context.Context, openid, orderNo, sessionKe
|
||||
// 官方文档:query_order 仅需 pay_sig,不需用户态 signature
|
||||
apiResp, err := s.callAPI(ctx, "/xpay/query_order", bodyJSON, "", false)
|
||||
if err != nil {
|
||||
if xpayErr, ok := err.(*XpayAPIError); ok {
|
||||
logx.WithContext(ctx).Errorf("[xpay] query_order FAIL order_no=%s openid=%s errcode=%d errmsg=%s raw=%s",
|
||||
orderNo, openid, xpayErr.ErrCode, xpayErr.ErrMsg, xpayErr.RawBody)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if apiResp.ErrCode != 0 {
|
||||
return nil, fmt.Errorf("query_order errcode=%d errmsg=%s", apiResp.ErrCode, apiResp.ErrMsg)
|
||||
}
|
||||
|
||||
orderRaw := apiResp.Order
|
||||
if len(orderRaw) == 0 {
|
||||
@@ -262,8 +291,11 @@ func (s *XpayService) QueryOrder(ctx context.Context, openid, orderNo, sessionKe
|
||||
}
|
||||
var status xpayOrderStatus
|
||||
if len(orderRaw) > 0 {
|
||||
status.RawOrder = string(orderRaw)
|
||||
_ = json.Unmarshal(orderRaw, &status)
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[xpay] query_order order_no=%s status=%d paid_fee=%d left_fee=%d wx_order_id=%s err_msg=%s raw=%s",
|
||||
orderNo, status.Status, status.PaidFee, status.LeftFee, status.WxOrderID, status.ErrMsg, status.RawOrder)
|
||||
return &status, nil
|
||||
}
|
||||
|
||||
@@ -306,11 +338,17 @@ func (s *XpayService) RefundOrder(ctx context.Context, openid, orderNo, refundOr
|
||||
|
||||
apiResp, err := s.callAPI(ctx, "/xpay/refund_order", bodyJSON, "", false)
|
||||
if err != nil {
|
||||
if xpayErr, ok := err.(*XpayAPIError); ok {
|
||||
if xpayErr.ErrCode == 268490004 || xpayErr.ErrCode == 268490014 {
|
||||
logx.WithContext(ctx).Infof("[xpay] refund_order idempotent order_no=%s errcode=%d", orderNo, xpayErr.ErrCode)
|
||||
return nil
|
||||
}
|
||||
logx.WithContext(ctx).Errorf("[xpay] refund_order FAIL order_no=%s errcode=%d errmsg=%s raw=%s",
|
||||
orderNo, xpayErr.ErrCode, xpayErr.ErrMsg, xpayErr.RawBody)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if apiResp.ErrCode != 0 && apiResp.ErrCode != 268490004 && apiResp.ErrCode != 268490014 {
|
||||
return fmt.Errorf("refund_order errcode=%d errmsg=%s", apiResp.ErrCode, apiResp.ErrMsg)
|
||||
}
|
||||
_ = apiResp
|
||||
logx.WithContext(ctx).Infof("[xpay] refund_order OK order_no=%s refund_order_id=%s fee=%d", orderNo, refundOrderID, refundFeeFen)
|
||||
return nil
|
||||
}
|
||||
@@ -330,13 +368,18 @@ func (s *XpayService) NotifyProvideGoods(ctx context.Context, openid, orderNo, w
|
||||
return err
|
||||
}
|
||||
|
||||
apiResp, err := s.callAPI(ctx, "/xpay/notify_provide_goods", bodyJSON, sessionKey, true)
|
||||
_, err = s.callAPI(ctx, "/xpay/notify_provide_goods", bodyJSON, sessionKey, true)
|
||||
if err != nil {
|
||||
if xpayErr, ok := err.(*XpayAPIError); ok {
|
||||
if xpayErr.ErrCode == 268490004 {
|
||||
logx.WithContext(ctx).Infof("[xpay] notify_provide_goods idempotent order_no=%s", orderNo)
|
||||
return nil
|
||||
}
|
||||
logx.WithContext(ctx).Errorf("[xpay] notify_provide_goods FAIL order_no=%s errcode=%d errmsg=%s raw=%s",
|
||||
orderNo, xpayErr.ErrCode, xpayErr.ErrMsg, xpayErr.RawBody)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if apiResp.ErrCode != 0 && apiResp.ErrCode != 268490004 {
|
||||
return fmt.Errorf("notify_provide_goods errcode=%d errmsg=%s", apiResp.ErrCode, apiResp.ErrMsg)
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[xpay] notify_provide_goods OK order_no=%s", orderNo)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1928,8 +1928,11 @@ type PaymentCheckReq struct {
|
||||
}
|
||||
|
||||
type PaymentCheckResp struct {
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
WxSyncError string `json:"wx_sync_error,optional"` // query_order 失败时微信返回
|
||||
WxOrderStatus int `json:"wx_order_status,optional"` // 微信侧订单 status
|
||||
WxOrderDetail string `json:"wx_order_detail,optional"` // query_order 原始 order 摘要(排查用)
|
||||
}
|
||||
|
||||
type PaymentReq struct {
|
||||
|
||||
Reference in New Issue
Block a user