From ca42c43bd2e4bb8d937ab128da6777568b9911c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=AE=8F?= <389838709@qq.com> Date: Wed, 7 Aug 2024 10:22:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E8=B0=83=20=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E4=B8=8B=E6=B8=B8=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/thirdpay/notify/notify.go | 6 ++-- app/third/paymentService/ali_service.go | 42 +++++++++++++++++++++- app/third/paymentService/wechat_service.go | 39 +++++++++++++++++++- 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/app/services/thirdpay/notify/notify.go b/app/services/thirdpay/notify/notify.go index f1a92aa..5369f76 100644 --- a/app/services/thirdpay/notify/notify.go +++ b/app/services/thirdpay/notify/notify.go @@ -85,7 +85,7 @@ func (o *OrderNotify) handle() (res *OrderNotifyResp) { return &OrderNotifyResp{ OrderId: o.OrderId, Send: true, - ErrCode: o.ErrCode, + ErrCode: o.code, Content: o, } } @@ -110,7 +110,7 @@ func (o *OrderNotify) setBody() *OrderNotifySendContent { CompleteTime: o.CompleteTime, Status: o.order.Status, Msg: o.Msg, - ErrCode: o.ErrCode, + ErrCode: o.code, AppId: o.order.AppId, ChannelId: o.order.PayChannelId, MerchantId: o.order.MerchantId, @@ -118,7 +118,7 @@ func (o *OrderNotify) setBody() *OrderNotifySendContent { } func (o *OrderNotify) updateOrder() { - if o.ErrCode != errorcode.Success { + if o.code != errorcode.Success { o.order.Status = common.ORDER_STATUS_FAILED } else { o.order.Status = common.ORDER_STATUS_PAYED diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index cba07fa..f549a3c 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -1,9 +1,13 @@ package paymentService import ( + "PaymentCenter/app/constants/common" + "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/services/thirdpay/notify" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/config" "context" + "encoding/json" "errors" "fmt" "github.com/go-pay/gopay" @@ -99,7 +103,43 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { return err } - // todo 拼装数据触发下游回调,数据待定 + // 拼装数据触发下游回调,触发下游回调 + orderId, _ := strconv.Atoi(notifyReq.Get("out_trade_no")) + payerTotal, _ := strconv.Atoi(notifyReq.Get("buyer_pay_amount")) + // 订单状态 + tradeStatus := notifyReq.Get("trade_status") + errCode := 0 + msg := "" + switch tradeStatus { + case "TRADE_CLOSED": + errCode = errorcode.ParamError + msg = "未付款交易超时关闭,或支付完成后全额退款。" + case "TRADE_SUCCESS": + errCode = errorcode.Success + msg = "交易支付成功。" + case "TRADE_FINISHED": + errCode = errorcode.Success + msg = "交易结束,不可退款。" + } + if errCode == 0 { + // 等待买家付款,不走后续回调 + return errors.New("订单状态异常,无法进行后续回调") + } + + res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, payerTotal, msg) + merchantCallback, _ := json.Marshal(res) + // 记录日志 + go func() { + payCallback, _ := json.Marshal(notifyReq) + payParam := "" + saveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback)) + }() + + if res.ErrCode != errorcode.Success { + logger.Error(context.Background(), "ALiCallBack 发生错误", fmt.Sprintf("回调时,下游处理订单失败,返回数据为:%s", string(merchantCallback))) + return errors.New("回调时,下游处理订单失败") + } + return nil } diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 65c11e6..3979b15 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -1,9 +1,13 @@ package paymentService import ( + "PaymentCenter/app/constants/common" + "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/services/thirdpay/notify" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/config" "context" + "encoding/json" "errors" "fmt" "github.com/go-pay/gopay" @@ -135,7 +139,40 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { if err != nil { return err } - // todo 返回触发下游回调的格式 + + errCode := 0 + msg := "" + // 订单状态 + switch CallBackInfo.TradeState { + case "SUCCESS": + errCode = errorcode.Success + msg = "支付成功" + case "CLOSED": + errCode = errorcode.ParamError + msg = "已关闭" + case "REVOKED": + errCode = errorcode.ParamError + msg = "已撤销(付款码支付)" + case "PAYERROR": + errCode = errorcode.ParamError + msg = "支付失败(其他原因,如银行返回失败)" + } + + // 触发下游回调的格式 + orderId, _ := strconv.Atoi(CallBackInfo.OutTradeNo) + res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, int(CallBackInfo.Amount.PayerTotal), msg) + merchantCallback, _ := json.Marshal(res) + // 记录日志 + go func() { + payCallback, _ := json.Marshal(CallBackInfo) + payParam := "" + saveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback)) + }() + + if res.ErrCode != errorcode.Success { + logger.Error(context.Background(), "WxPayCallBack 发生错误", fmt.Sprintf("回调时,下游处理订单失败,返回数据为:%s", string(merchantCallback))) + return errors.New("回调时,下游处理订单失败") + } return nil }