diff --git a/app/http/controllers/front/payment_controller.go b/app/http/controllers/front/payment_controller.go index 05a0a0c..962815f 100644 --- a/app/http/controllers/front/payment_controller.go +++ b/app/http/controllers/front/payment_controller.go @@ -6,6 +6,7 @@ import ( "PaymentCenter/app/models/paychannelmodel" "PaymentCenter/app/services" "PaymentCenter/app/third/paymentService" + "bytes" "encoding/json" "fmt" "github.com/gin-gonic/gin" @@ -13,6 +14,7 @@ import ( "github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/wechat/v3" "github.com/qit-team/snow-core/log/logger" + "io" "strconv" "net/http" @@ -20,7 +22,7 @@ import ( // WxCallback 微信支付回调 func WxCallback(c *gin.Context) { - logger.Info(c, "WxCallback-回调数据", c.Request) + saveLogger(c, "wx") payChannelId := c.Param("payChannelId") logger.Info(c, "WxCallback-回调数据payChannelId", payChannelId) if payChannelId == "" { @@ -80,7 +82,7 @@ func WxCallback(c *gin.Context) { // AliCallback 支付宝支付回调 func AliCallback(c *gin.Context) { - logger.Info(c, "AliCallback-回调数据", c.Request) + saveLogger(c, "ALI") payChannelId := c.Param("payChannelId") logger.Info(c, "AliCallback-回调数据APPID", payChannelId) if payChannelId == "" { @@ -135,3 +137,27 @@ func AliCallback(c *gin.Context) { c.String(http.StatusOK, "%s", "success") return } + +func saveLogger(c *gin.Context, payType string) { + // 保存请求的方法和URL + method := c.Request.Method + url := c.Request.URL.String() + + // 保存请求头 + headers := make(map[string]string) + for k, v := range c.Request.Header { + headers[k] = v[0] + } + + // 保存请求体 + var bodyBytes []byte + if c.Request.Body != nil { + bodyBytes, _ = io.ReadAll(c.Request.Body) + } + // 由于 io.ReadAll 会将 body 读完,所以需要重新赋值 + c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) + + // 使用 logger 记录请求信息 + logger.Info(c, "支付回调"+payType, fmt.Sprintf("接受的参数:method: %s , url: %s , headers: %s , body: %s", method, url, headers, string(bodyBytes))) + c.Next() +}