优化支付日志和配置删除提示
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -15,15 +15,60 @@ import (
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
func NewHandler(service *Service, options ...HandlerOption) *Handler {
|
||||
handler := &Handler{
|
||||
service: service,
|
||||
logger: zap.NewNop(),
|
||||
}
|
||||
for _, option := range options {
|
||||
option(handler)
|
||||
}
|
||||
if handler.logger == nil {
|
||||
handler.logger = zap.NewNop()
|
||||
}
|
||||
return handler
|
||||
}
|
||||
|
||||
type HandlerOption func(*Handler)
|
||||
|
||||
// WithHandlerLogger 为支付 HTTP 处理器注入结构化日志器。
|
||||
func WithHandlerLogger(logger *zap.Logger) HandlerOption {
|
||||
return func(handler *Handler) {
|
||||
if logger != nil {
|
||||
handler.logger = logger
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) log() *zap.Logger {
|
||||
if h == nil || h.logger == nil {
|
||||
return zap.NewNop()
|
||||
}
|
||||
return h.logger
|
||||
}
|
||||
|
||||
func notifyLogFields(ctx context.Context, provider string, params map[string]string, contentType string, bodySize int, fields ...zap.Field) []zap.Field {
|
||||
base := paymentLogFields(ctx,
|
||||
zap.String("provider", provider),
|
||||
zap.String("third_order_id", params["third_order_id"]),
|
||||
zap.String("provider_order_id", firstNonEmpty(params["provider_order_id"], params["leshua_order_id"], params["schc_order_id"])),
|
||||
zap.String("merchant_refund_id", params["merchant_refund_id"]),
|
||||
zap.String("provider_refund_id", firstNonEmpty(params["provider_refund_id"], params["leshua_refund_id"], params["schc_refund_id"])),
|
||||
zap.String("status", params["status"]),
|
||||
zap.String("amount", params["amount"]),
|
||||
zap.String("content_type", contentType),
|
||||
zap.Int("body_size", bodySize),
|
||||
)
|
||||
return append(base, fields...)
|
||||
}
|
||||
|
||||
func (h *Handler) Start(c *gin.Context) {
|
||||
@@ -103,22 +148,14 @@ func (h *Handler) LeshuaNotify(c *gin.Context) {
|
||||
}
|
||||
rawPayload := string(body)
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
log.Printf(
|
||||
"[payment] leshua notify received third_order_id=%s leshua_order_id=%s status=%s amount=%s content_type=%s raw_payload=%s",
|
||||
params["third_order_id"],
|
||||
params["leshua_order_id"],
|
||||
params["status"],
|
||||
params["amount"],
|
||||
contentType,
|
||||
rawPayload,
|
||||
)
|
||||
h.log().Info("payment notify received", notifyLogFields(c.Request.Context(), "leshua", params, contentType, len(body))...)
|
||||
result, err := h.service.HandleLeshuaNotify(c.Request.Context(), params, rawPayload, contentType)
|
||||
if err != nil || result == nil || !result.OK {
|
||||
log.Printf("[payment] leshua notify failed third_order_id=%s err=%v", params["third_order_id"], err)
|
||||
h.log().Warn("payment notify failed", notifyLogFields(c.Request.Context(), "leshua", params, contentType, len(body), zap.Error(err))...)
|
||||
c.String(http.StatusOK, "FAIL")
|
||||
return
|
||||
}
|
||||
log.Printf("[payment] leshua notify processed third_order_id=%s status=%s", params["third_order_id"], params["status"])
|
||||
h.log().Info("payment notify processed", notifyLogFields(c.Request.Context(), "leshua", params, contentType, len(body))...)
|
||||
c.String(http.StatusOK, result.Message)
|
||||
}
|
||||
|
||||
@@ -136,22 +173,14 @@ func (h *Handler) LakalaNotify(c *gin.Context) {
|
||||
rawPayload := string(body)
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
authorization := c.GetHeader("Authorization")
|
||||
log.Printf(
|
||||
"[payment] lakala notify received third_order_id=%s provider_order_id=%s status=%s amount=%s content_type=%s raw_payload=%s",
|
||||
params["third_order_id"],
|
||||
params["provider_order_id"],
|
||||
params["status"],
|
||||
params["amount"],
|
||||
contentType,
|
||||
rawPayload,
|
||||
)
|
||||
h.log().Info("payment notify received", notifyLogFields(c.Request.Context(), "lakala", params, contentType, len(body))...)
|
||||
result, err := h.service.HandleNotify(c.Request.Context(), "lakala", params, rawPayload, contentType, authorization)
|
||||
if err != nil || result == nil || !result.OK {
|
||||
log.Printf("[payment] lakala notify failed third_order_id=%s err=%v", params["third_order_id"], err)
|
||||
h.log().Warn("payment notify failed", notifyLogFields(c.Request.Context(), "lakala", params, contentType, len(body), zap.Error(err))...)
|
||||
c.JSON(http.StatusOK, gin.H{"code": "FAIL", "message": "失败"})
|
||||
return
|
||||
}
|
||||
log.Printf("[payment] lakala notify processed third_order_id=%s status=%s", params["third_order_id"], params["status"])
|
||||
h.log().Info("payment notify processed", notifyLogFields(c.Request.Context(), "lakala", params, contentType, len(body))...)
|
||||
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "执行成功"})
|
||||
}
|
||||
|
||||
@@ -169,23 +198,14 @@ func (h *Handler) ShunchengNotify(c *gin.Context) {
|
||||
rawPayload := string(body)
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
authorization := c.GetHeader("Authorization")
|
||||
log.Printf(
|
||||
"[payment] shuncheng notify received third_order_id=%s schc_order_id=%s schc_refund_id=%s status=%s amount=%s content_type=%s raw_payload=%s",
|
||||
params["third_order_id"],
|
||||
params["schc_order_id"],
|
||||
params["schc_refund_id"],
|
||||
params["status"],
|
||||
params["amount"],
|
||||
contentType,
|
||||
rawPayload,
|
||||
)
|
||||
h.log().Info("payment notify received", notifyLogFields(c.Request.Context(), "shuncheng", params, contentType, len(body))...)
|
||||
result, err := h.service.HandleNotify(c.Request.Context(), "shuncheng", params, rawPayload, contentType, authorization)
|
||||
if err != nil || result == nil || !result.OK {
|
||||
log.Printf("[payment] shuncheng notify failed third_order_id=%s err=%v", params["third_order_id"], err)
|
||||
h.log().Warn("payment notify failed", notifyLogFields(c.Request.Context(), "shuncheng", params, contentType, len(body), zap.Error(err))...)
|
||||
c.String(http.StatusOK, "FAIL")
|
||||
return
|
||||
}
|
||||
log.Printf("[payment] shuncheng notify processed third_order_id=%s status=%s", params["third_order_id"], params["status"])
|
||||
h.log().Info("payment notify processed", notifyLogFields(c.Request.Context(), "shuncheng", params, contentType, len(body))...)
|
||||
c.String(http.StatusOK, result.Message)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user