fix(psbc callback): 统一邮储支付回调的响应格式为json
将原来直接返回字符串的错误和成功响应,改为统一使用YouChuOrderNotifyResponse结构体返回json格式响应,规范回调接口的返回数据结构
This commit is contained in:
parent
6c28023f7d
commit
53c1e3e00b
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
}
|
||||
Loading…
Reference in New Issue