From 53c1e3e00b6900a328053b28461d8aca5538d4ba Mon Sep 17 00:00:00 2001 From: wolter <11@gmail> Date: Fri, 21 Aug 2026 11:39:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(psbc=20callback):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E9=82=AE=E5=82=A8=E6=94=AF=E4=BB=98=E5=9B=9E=E8=B0=83=E7=9A=84?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E6=A0=BC=E5=BC=8F=E4=B8=BAjson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原来直接返回字符串的错误和成功响应,改为统一使用YouChuOrderNotifyResponse结构体返回json格式响应,规范回调接口的返回数据结构 --- app/http/controllers/front/psbc.go | 44 +++++++++++++++++++++++------- app/http/entities/front/psbc.go | 8 ++++++ 2 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 app/http/entities/front/psbc.go diff --git a/app/http/controllers/front/psbc.go b/app/http/controllers/front/psbc.go index e1aef3e..766115b 100644 --- a/app/http/controllers/front/psbc.go +++ b/app/http/controllers/front/psbc.go @@ -3,6 +3,7 @@ package front import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/http/entities/front" "PaymentCenter/app/models/paychannelmodel" "PaymentCenter/app/services" "PaymentCenter/app/services/thirdpay/thirdpay_notify" @@ -18,18 +19,24 @@ import ( // PsbcCallback 邮储支付回调 func PsbcCallback(c *gin.Context) { + notifyRsp := front.YouChuOrderNotifyResponse{} + logger.Info(c, "PsbcCallback-回调数据", c.Request) payChannelId := c.Param("payChannelId") logger.Info(c, "PsbcCallback-回调数据payChannelId", payChannelId) if payChannelId == "" { - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "支付渠道ID为空" + c.JSON(http.StatusOK, notifyRsp) return } body, err := io.ReadAll(c.Request.Body) if err != nil { logger.Error(c, "PsbcCallback-读取请求体失败", err.Error()) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "读取请求体失败" + c.JSON(http.StatusOK, notifyRsp) return } bodyStr := string(body) @@ -41,7 +48,9 @@ func PsbcCallback(c *gin.Context) { code := services.PayChannelGet(&payChannelModel) if code == errorcode.PayChannelNotFound { logger.Error(c, "PsbcCallback-回调数据未获取到支付配置,404") - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "未获取到支付配置" + c.JSON(http.StatusOK, notifyRsp) return } @@ -49,12 +58,16 @@ func PsbcCallback(c *gin.Context) { err = json.Unmarshal([]byte(payChannelModel.ExtJson), &psbcConfig) if err != nil { logger.Error(c, "PsbcCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error())) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "解析支付配置错误" + c.JSON(http.StatusOK, notifyRsp) return } if psbcConfig.MchtNo == "" || psbcConfig.PrivateKey == "" || psbcConfig.BankKey == "" { logger.Error(c, "PsbcCallback-回调数据解析支付配置错误,解析出来的信息为空") - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "解析支付配置错误" + c.JSON(http.StatusOK, notifyRsp) return } psbcConfig.AppID = payChannelModel.AppId @@ -62,7 +75,9 @@ func PsbcCallback(c *gin.Context) { result, err := paymentService.PsbcVerifyAndParseNotify(bodyStr, psbcConfig) if err != nil { logger.Error(c, "PsbcCallback-回调数据验签失败", err.Error()) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "验签失败" + c.JSON(http.StatusOK, notifyRsp) return } @@ -108,14 +123,18 @@ func PsbcCallback(c *gin.Context) { msg = "支付失败" default: logger.Error(c, "PsbcCallback-订单状态异常", status) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "订单状态异常" + c.JSON(http.StatusOK, notifyRsp) return } orderId, err := strconv.ParseInt(reqTraceId, 10, 64) if err != nil { logger.Error(c, "PsbcCallback 发生错误", fmt.Sprintf("订单号转换失败,错误信息:%s", err.Error())) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "订单号转换失败" + c.JSON(http.StatusOK, notifyRsp) return } @@ -135,10 +154,15 @@ func PsbcCallback(c *gin.Context) { if notifyResult.ErrCode != errorcode.Success { logger.Error(c, "PsbcCallback-回调执行失败,失败原因:", fmt.Sprintf("%+v", notifyResult)) - c.String(http.StatusBadRequest, "%s", "fail") + notifyRsp.RespCode = "000001" + notifyRsp.RespMsg = "交易失败" + errorcode.GetMsg(notifyResult.ErrCode, "") + c.JSON(http.StatusOK, notifyRsp) return } - c.String(http.StatusOK, "%s", "success") + c.JSON(http.StatusOK, front.YouChuOrderNotifyResponse{ + RespCode: "000000", + RespMsg: "交易成功", + }) return } diff --git a/app/http/entities/front/psbc.go b/app/http/entities/front/psbc.go new file mode 100644 index 0000000..e3f2bcf --- /dev/null +++ b/app/http/entities/front/psbc.go @@ -0,0 +1,8 @@ +package front + +type YouChuOrderNotifyResponse struct { + RespCode string `json:"respCode"` //000000 表示通知成功 + RespMsg string `json:"respMsg"` + Data interface{} `json:"data"` + LinkUrl string `json:"linkUrl"` +}