From 225e8aaa9739c9e38dcd7626eb94d7e7770a214a Mon Sep 17 00:00:00 2001 From: erio Date: Mon, 6 Apr 2026 02:30:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20resolve=20golangci-lint=20errors=20?= =?UTF-8?q?=E2=80=94=20errcheck,=20gofmt,=20deprecated=20stripe=20API,=20u?= =?UTF-8?q?nused=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/payment/provider/easypay.go | 8 +++--- backend/internal/payment/provider/stripe.go | 26 +++++++------------- backend/internal/server/routes/payment.go | 1 - backend/internal/service/payment_service.go | 22 ----------------- 4 files changed, 13 insertions(+), 44 deletions(-) diff --git a/backend/internal/payment/provider/easypay.go b/backend/internal/payment/provider/easypay.go index f9d36c4786..91d8270024 100644 --- a/backend/internal/payment/provider/easypay.go +++ b/backend/internal/payment/provider/easypay.go @@ -188,7 +188,7 @@ func (e *EasyPay) post(ctx context.Context, endpoint string, params map[string]s if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() return io.ReadAll(resp.Body) } @@ -204,11 +204,11 @@ func easyPaySign(params map[string]string, pkey string) string { var buf strings.Builder for i, k := range keys { if i > 0 { - buf.WriteByte('&') + _ = buf.WriteByte('&') } - buf.WriteString(k + "=" + params[k]) + _, _ = buf.WriteString(k + "=" + params[k]) } - buf.WriteString(pkey) + _, _ = buf.WriteString(pkey) hash := md5.Sum([]byte(buf.String())) return hex.EncodeToString(hash[:]) } diff --git a/backend/internal/payment/provider/stripe.go b/backend/internal/payment/provider/stripe.go index 8167325f00..cc591d5c0f 100644 --- a/backend/internal/payment/provider/stripe.go +++ b/backend/internal/payment/provider/stripe.go @@ -10,7 +10,6 @@ import ( "github.com/Wei-Shaw/sub2api/internal/payment" stripe "github.com/stripe/stripe-go/v82" - "github.com/stripe/stripe-go/v82/client" "github.com/stripe/stripe-go/v82/webhook" ) @@ -29,7 +28,7 @@ type Stripe struct { mu sync.Mutex initialized bool - api *client.API + sc *stripe.Client } // NewStripe creates a new Stripe provider instance. @@ -47,8 +46,7 @@ func (s *Stripe) ensureInit() { s.mu.Lock() defer s.mu.Unlock() if !s.initialized { - s.api = &client.API{} - s.api.Init(s.config["secretKey"], nil) + s.sc = stripe.NewClient(s.config["secretKey"]) s.initialized = true } } @@ -87,10 +85,10 @@ func (s *Stripe) CreatePayment(ctx context.Context, req payment.CreatePaymentReq return nil, fmt.Errorf("stripe create payment: %w", err) } - params := &stripe.PaymentIntentParams{ + params := &stripe.PaymentIntentCreateParams{ Amount: stripe.Int64(amountInCents), Currency: stripe.String(stripeCurrency), - AutomaticPaymentMethods: &stripe.PaymentIntentAutomaticPaymentMethodsParams{ + AutomaticPaymentMethods: &stripe.PaymentIntentCreateAutomaticPaymentMethodsParams{ Enabled: stripe.Bool(true), }, Description: stripe.String(req.Subject), @@ -99,7 +97,7 @@ func (s *Stripe) CreatePayment(ctx context.Context, req payment.CreatePaymentReq params.SetIdempotencyKey(fmt.Sprintf("pi-%s", req.OrderID)) params.Context = ctx - pi, err := s.api.PaymentIntents.New(params) + pi, err := s.sc.V1PaymentIntents.Create(ctx, params) if err != nil { return nil, fmt.Errorf("stripe create payment: %w", err) } @@ -114,10 +112,7 @@ func (s *Stripe) CreatePayment(ctx context.Context, req payment.CreatePaymentReq func (s *Stripe) QueryOrder(ctx context.Context, tradeNo string) (*payment.QueryOrderResponse, error) { s.ensureInit() - params := &stripe.PaymentIntentParams{} - params.Context = ctx - - pi, err := s.api.PaymentIntents.Get(tradeNo, params) + pi, err := s.sc.V1PaymentIntents.Retrieve(ctx, tradeNo, nil) if err != nil { return nil, fmt.Errorf("stripe query order: %w", err) } @@ -189,14 +184,14 @@ func (s *Stripe) Refund(ctx context.Context, req payment.RefundRequest) (*paymen return nil, fmt.Errorf("stripe refund: %w", err) } - params := &stripe.RefundParams{ + params := &stripe.RefundCreateParams{ PaymentIntent: stripe.String(req.TradeNo), Amount: stripe.Int64(amountInCents), Reason: stripe.String(string(stripe.RefundReasonRequestedByCustomer)), } params.Context = ctx - r, err := s.api.Refunds.New(params) + r, err := s.sc.V1Refunds.Create(ctx, params) if err != nil { return nil, fmt.Errorf("stripe refund: %w", err) } @@ -216,10 +211,7 @@ func (s *Stripe) Refund(ctx context.Context, req payment.RefundRequest) (*paymen func (s *Stripe) CancelPayment(ctx context.Context, tradeNo string) error { s.ensureInit() - params := &stripe.PaymentIntentCancelParams{} - params.Context = ctx - - _, err := s.api.PaymentIntents.Cancel(tradeNo, params) + _, err := s.sc.V1PaymentIntents.Cancel(ctx, tradeNo, nil) if err != nil { return fmt.Errorf("stripe cancel payment: %w", err) } diff --git a/backend/internal/server/routes/payment.go b/backend/internal/server/routes/payment.go index 828b68f361..f6e94fefd7 100644 --- a/backend/internal/server/routes/payment.go +++ b/backend/internal/server/routes/payment.go @@ -70,7 +70,6 @@ func RegisterPaymentRoutes( adminOrders.POST("/:id/refund", adminPaymentHandler.ProcessRefund) } - // Subscription Plans plans := adminGroup.Group("/plans") { diff --git a/backend/internal/service/payment_service.go b/backend/internal/service/payment_service.go index 55ef050c5b..929774c034 100644 --- a/backend/internal/service/payment_service.go +++ b/backend/internal/service/payment_service.go @@ -986,28 +986,6 @@ func buildTopUsers(orders []*dbent.PaymentOrder) []TopUserStat { return result } -func (s *PaymentService) sumAmt(ctx context.Context, statuses []string, since time.Time, usePaid bool) (float64, error) { - q := s.entClient.PaymentOrder.Query().Where(paymentorder.StatusIn(statuses...)) - if usePaid { - q = q.Where(paymentorder.PaidAtGTE(since)) - } else { - q = q.Where(paymentorder.CreatedAtGTE(since)) - } - os, err := q.All(ctx) - if err != nil { - return 0, err - } - var t float64 - for _, o := range os { - if usePaid { - t += o.Amount - } else { - t += o.RefundAmount - } - } - return t, nil -} - func (s *PaymentService) writeAuditLog(ctx context.Context, oid int64, action, op string, detail map[string]any) { dj, _ := json.Marshal(detail) _, err := s.entClient.PaymentAuditLog.Create().SetOrderID(strconv.FormatInt(oid, 10)).SetAction(action).SetDetail(string(dj)).SetOperator(op).Save(ctx)