package handler import ( "strconv" "time" "affiliate_dash/internal/middleware" "affiliate_dash/internal/pkg/response" "affiliate_dash/internal/service" "github.com/gin-gonic/gin" ) func (h *MerchantHandler) ListAPIClients(c *gin.Context) { clients, err := h.merchantSvc.ListAPIClients(middleware.GetMerchantID(c)) if err != nil { response.ServerError(c, err.Error()) return } response.OK(c, clients) } type apiClientReq struct { Name string `json:"name" binding:"required"` Scopes string `json:"scopes" binding:"required"` SignatureVersion string `json:"signature_version"` ExpiresAt string `json:"expires_at"` } func (h *MerchantHandler) CreateAPIClient(c *gin.Context) { var req apiClientReq if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "参数错误:name 与 scopes 必填") return } var expiresAt *time.Time if req.ExpiresAt != "" { value, err := time.Parse(time.RFC3339, req.ExpiresAt) if err != nil { response.BadRequest(c, "expires_at 必须是 RFC3339 时间") return } expiresAt = &value } credential, err := h.merchantSvc.CreateAPIClient(middleware.GetMerchantID(c), service.CreateAPIClientInput{ Name: req.Name, Scopes: req.Scopes, SignatureVersion: req.SignatureVersion, ExpiresAt: expiresAt, }, middleware.GetUserID(c)) if err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, credential) } type statusReq struct { Status string `json:"status" binding:"required"` } func (h *MerchantHandler) UpdateAPIClientStatus(c *gin.Context) { var req statusReq if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "参数错误") return } id, _ := strconv.ParseUint(c.Param("id"), 10, 64) if err := h.merchantSvc.UpdateAPIClientStatus(middleware.GetMerchantID(c), uint(id), req.Status, middleware.GetUserID(c)); err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, nil) } // DeleteAPIClient DELETE /api/merchant/api-clients/:id func (h *MerchantHandler) DeleteAPIClient(c *gin.Context) { id, _ := strconv.ParseUint(c.Param("id"), 10, 64) if err := h.merchantSvc.DeleteAPIClient(middleware.GetMerchantID(c), uint(id), middleware.GetUserID(c)); err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, nil) } func (h *MerchantHandler) ListCallbacks(c *gin.Context) { subscription, err := h.callbackSvc.GetSubscription(middleware.GetMerchantID(c)) if err != nil { response.ServerError(c, err.Error()) return } response.OK(c, subscription) } type callbackReq struct { Name string `json:"name"` URL string `json:"url" binding:"required"` Events string `json:"events" binding:"required"` Status string `json:"status"` RotateSecret bool `json:"rotate_secret"` } func (h *MerchantHandler) CreateCallback(c *gin.Context) { var req callbackReq if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "参数错误:url、events 必填") return } credential, err := h.callbackSvc.CreateSubscription(middleware.GetMerchantID(c), service.CreateCallbackInput{ Name: req.Name, URL: req.URL, Events: req.Events, Status: req.Status, RotateSecret: req.RotateSecret, }, middleware.GetUserID(c)) if err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, credential) }