From fe4d2d397739603069937c3a36a426aa1d3f1076 Mon Sep 17 00:00:00 2001 From: wolter <11@gmail> Date: Thu, 20 Aug 2026 20:09:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=AE=A2=E5=8D=95=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E6=B8=B8=E4=BA=A4=E6=98=93=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 4 +-- app/http/controllers/front/psbc.go | 30 +++++++++++++++++-- app/models/ordersmodel/orders.go | 1 + app/services/thirdpay/do/pay.go | 1 + app/services/thirdpay/pay_query.go | 2 +- .../thirdpay/thirdpay_notify/notify.go | 29 ++++++++++-------- app/third/paymentService/ali_service.go | 2 +- app/third/paymentService/psbc/order.go | 2 -- app/third/paymentService/psbc/types.go | 15 ++++++---- app/third/paymentService/psbc_service.go | 28 ++--------------- app/third/paymentService/wechat_service.go | 2 +- 11 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index 6325835..42d9b85 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -87,7 +87,7 @@ func closeOrder() { } orderIds = append(orderIds, orderInfo.Id) // 回调通知下游 - notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, response.Code, common.ORDER_STATUS_CLOSE, 0, 0, "长时间未支付关闭订单") + notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, response.Code, common.ORDER_STATUS_CLOSE, 0, 0, "长时间未支付关闭订单", "") //utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult) if notifyResult.ErrCode != errorcode.Success { utils.Log(nil, "关闭订单,回调下游失败", fmt.Sprintf("%#v", notifyResult)) @@ -305,7 +305,7 @@ func queryRefundOrder() { msg = "退款失败" } // 回调通知下游 todo - notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.RefundFee), int(result.Result.RefundFee), msg) + notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.RefundFee), int(result.Result.RefundFee), msg, result.Result.TransactionId) //utils.Log(nil, "主动查询退款订单状态,回调下游", notifyResult) if notifyResult.ErrCode != errorcode.Success { utils.Log(nil, "查询退款订单状态,回调下游失败", notifyResult) diff --git a/app/http/controllers/front/psbc.go b/app/http/controllers/front/psbc.go index 2d71bbc..e1aef3e 100644 --- a/app/http/controllers/front/psbc.go +++ b/app/http/controllers/front/psbc.go @@ -59,13 +59,39 @@ func PsbcCallback(c *gin.Context) { } psbcConfig.AppID = payChannelModel.AppId - reqTraceId, amountTotal, payerTotal, status, err := paymentService.PsbcVerifyAndParseNotify(bodyStr, psbcConfig) + result, err := paymentService.PsbcVerifyAndParseNotify(bodyStr, psbcConfig) if err != nil { logger.Error(c, "PsbcCallback-回调数据验签失败", err.Error()) c.String(http.StatusBadRequest, "%s", "fail") return } + reqTraceId := result.ReqTraceId + var amountTotal int64 + var payerTotal int64 + var status string + if result.TxnAmt != "" { + amountFloat, err := strconv.ParseFloat(result.TxnAmt, 64) + if err == nil { + amountTotal = int64(amountFloat * 100) + payerTotal = amountTotal + } + } + + orderSta := "" + if result.OrderSta != "" { + orderSta = result.OrderSta + } + + switch orderSta { + case "03": + status = "SUCCESS" + case "04", "05": + status = "FAIL" + default: + status = "NOTPAY" + } + logger.Info(c, "PsbcCallback-解析邮储回调结果", fmt.Sprintf("reqTraceId:%s, amountTotal:%d, payerTotal:%d, status:%s", reqTraceId, amountTotal, payerTotal, status)) var orderStatus int @@ -93,7 +119,7 @@ func PsbcCallback(c *gin.Context) { return } - notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderId, errCode, orderStatus, int(amountTotal), int(payerTotal), msg) + notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderId, errCode, orderStatus, int(amountTotal), int(payerTotal), msg, result.OrderNo) notifyResultJson, _ := json.Marshal(notifyResult) go func() { diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go index f281045..7a653c9 100644 --- a/app/models/ordersmodel/orders.go +++ b/app/models/ordersmodel/orders.go @@ -31,6 +31,7 @@ type Orders struct { UpdateTime time.Time `xorm:"'update_time' timestamp updated"` Status int `xorm:"'status' TINYINT"` DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` + TransactionId string `xorm:"'transaction_id' varchar(50)"` // 支付成功后返回的第三方订单号 } type OrdersBackendList struct { diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index ce4fb25..b8cb20b 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -240,6 +240,7 @@ func (w *Pay) Refund() { RefundOrderId: w.Order.Id, RefundReason: w.PayParam.Desc, RefundAmount: int64(w.PayParam.Amount), + TransactionId: w.Order.TransactionId, } if refundFunc, ok = RefundWayList[w.PayParam.Channel.ChannelType]; !ok { w.PayCode = errorcode.PayChannelNotBuild diff --git a/app/services/thirdpay/pay_query.go b/app/services/thirdpay/pay_query.go index 3ed3300..1ff5c8a 100644 --- a/app/services/thirdpay/pay_query.go +++ b/app/services/thirdpay/pay_query.go @@ -79,7 +79,7 @@ func OrderQueryAndNotify(ctx context.Context, orderInfo *ordersmodel.OrdersLeftP msg = "订单关闭" } // 回调通知下游 - notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.AmountTotal), int(result.Result.PayerTotal), msg) + notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.AmountTotal), int(result.Result.PayerTotal), msg, result.Result.TransactionId) //utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult) if notifyResult.ErrCode != errorcode.Success { utils.Log(nil, "主动查询订单支付状态,回调下游失败", fmt.Sprintf("%+v", notifyResult)) diff --git a/app/services/thirdpay/thirdpay_notify/notify.go b/app/services/thirdpay/thirdpay_notify/notify.go index 0ad83d6..4139daa 100644 --- a/app/services/thirdpay/thirdpay_notify/notify.go +++ b/app/services/thirdpay/thirdpay_notify/notify.go @@ -24,12 +24,13 @@ type OrderNotify struct { Code int app *appmodel.App - OrderId int64 - Status int //订单状态 - ActualAmount int // 支付上游返回的订单金额,单位为分。该参数的值为支付时传入的 - PayerTotal int // 用户实际支付金额,单位为分 - Msg string - CompleteTime time.Time + OrderId int64 + Status int //订单状态 + ActualAmount int // 支付上游返回的订单金额,单位为分。该参数的值为支付时传入的 + PayerTotal int // 用户实际支付金额,单位为分 + Msg string + TransactionId string // 支付成功后返回的第三方订单号 + CompleteTime time.Time } type OrderNotifyResp struct { @@ -57,14 +58,15 @@ type OrderNotifySendContent struct { // actualAmount 订单支付总金额,支付时传入 // payerTotal 用户实际支付金额,(有优惠券的情况下,payerTotal = actualAmount - 优惠金额) -func NewOrderNotifyWithHandle(orderId int64, code int, Status int, actualAmount int, payerTotal int, msg string) *OrderNotifyResp { +func NewOrderNotifyWithHandle(orderId int64, code int, Status int, actualAmount int, payerTotal int, msg string, transactionId string) *OrderNotifyResp { orderNotify := &OrderNotify{ - OrderId: orderId, - Status: Status, - ActualAmount: actualAmount, - Msg: msg, - Code: code, - PayerTotal: payerTotal, + OrderId: orderId, + Status: Status, + ActualAmount: actualAmount, + Msg: msg, + Code: code, + PayerTotal: payerTotal, + TransactionId: transactionId, } return orderNotify.Handle() } @@ -190,6 +192,7 @@ func (o *OrderNotify) updateOrder() { o.order.Status = o.Status o.order.ActualAmount = o.ActualAmount o.order.PayerTotal = o.PayerTotal + o.order.TransactionId = o.TransactionId o.Code = services.OrderUpdate(o.order, "status") return } diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index 7140a91..131e47d 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -197,7 +197,7 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { return errors.New("订单状态异常,无法进行后续回调") } - res := thirdpay_notify.NewOrderNotifyWithHandle(int64(orderId), errCode, orderStatus, amountTotal, payerTotal, msg) + res := thirdpay_notify.NewOrderNotifyWithHandle(int64(orderId), errCode, orderStatus, amountTotal, payerTotal, msg, notifyReq.Get("trade_no")) merchantCallback, _ := json.Marshal(res) // 记录日志 go func() { diff --git a/app/third/paymentService/psbc/order.go b/app/third/paymentService/psbc/order.go index 73a1112..f3e6349 100644 --- a/app/third/paymentService/psbc/order.go +++ b/app/third/paymentService/psbc/order.go @@ -48,8 +48,6 @@ func (c *Client) OrderQuery(orderNo string) (*OrderQueryResponse, error) { return nil, fmt.Errorf("加密请求失败: %v", err) } - BusiMainId = "202607301754241710448735" - encryptedBytes, _ := json.Marshal(encryptedReq) url := c.cfg.OrderHost + c.cfg.MerchantId + ".htm?partnerTxSriNo=" + BusiMainId diff --git a/app/third/paymentService/psbc/types.go b/app/third/paymentService/psbc/types.go index 5ca24cc..1ad6fe8 100644 --- a/app/third/paymentService/psbc/types.go +++ b/app/third/paymentService/psbc/types.go @@ -78,16 +78,19 @@ type RefundRequest struct { // RefundResponse 退款响应 type RefundResponse struct { - RespCode string `json:"code"` - RespMsg string `json:"respMsg"` + MchtNo string `json:"mchtNo"` + ReqDate string `json:"reqDate"` + ReqTraceId string `json:"reqTraceId"` RespCd string `json:"respCd"` - TxnCode string `json:"txnCode"` + RespCode string `json:"respCode"` + RespDesc string `json:"respDesc"` + RespMsg string `json:"respMsg"` SourceId string `json:"sourceId"` - ReqTraceId string `json:"ReqTraceId"` + Success bool `json:"success"` + TxnAmt string `json:"txnAmt"` + TxnCode string `json:"txnCode"` RefundOrderNo string `json:"refundOrderNo"` RefundOrderSta string `json:"refundOrderSta"` - MchtNo string `json:"mchtNo"` - TxnAmt string `json:"txnAmt"` } // BillQueryRequest 对账单查询请求 diff --git a/app/third/paymentService/psbc_service.go b/app/third/paymentService/psbc_service.go index 6eedaf2..43917f5 100644 --- a/app/third/paymentService/psbc_service.go +++ b/app/third/paymentService/psbc_service.go @@ -187,36 +187,14 @@ func PsbcCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (O }, nil } -func PsbcVerifyAndParseNotify(rawBody string, psbcConfig PsbcPay) (reqTraceId string, amountTotal int64, payerTotal int64, status string, err error) { +func PsbcVerifyAndParseNotify(rawBody string, psbcConfig PsbcPay) (result *psbc.YouChuOrderNotifyRequest, err error) { psbcClient := PsbcInitClient(psbcConfig) - result, err := psbcClient.VerifyAndParseNotify(rawBody) + result, err = psbcClient.VerifyAndParseNotify(rawBody) if err != nil { logger.Error(context.Background(), "PsbcVerifyAndParseNotify 发生错误", fmt.Sprintf("验签解密失败,错误信息:%s", err.Error())) return } - if result.TxnAmt != "" { - amountFloat, err := strconv.ParseFloat(result.TxnAmt, 64) - if err == nil { - amountTotal = int64(amountFloat * 100) - payerTotal = amountTotal - } - } - - orderSta := "" - if result.OrderSta != "" { - orderSta = result.OrderSta - } - - switch orderSta { - case "03": - status = "SUCCESS" - case "04", "05": - status = "FAIL" - default: - status = "NOTPAY" - } - - return result.ReqTraceId, amountTotal, payerTotal, status, nil + return result, nil } diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 95843ed..f59495a 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -214,7 +214,7 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // 触发下游回调的格式 orderId, _ := strconv.Atoi(CallBackInfo.OutTradeNo) - res := thirdpay_notify.NewOrderNotifyWithHandle(int64(orderId), errCode, orderStatus, int(CallBackInfo.Amount.Total), int(CallBackInfo.Amount.PayerTotal), msg) + res := thirdpay_notify.NewOrderNotifyWithHandle(int64(orderId), errCode, orderStatus, int(CallBackInfo.Amount.Total), int(CallBackInfo.Amount.PayerTotal), msg, CallBackInfo.TransactionId) merchantCallback, _ := json.Marshal(res) // 记录日志 go func() {