refactor(payment): code quality improvements per project conventions

- payment_service: split GetDashboardStats (131→35 lines) into 4 sub-functions,
  extract applyPagination helper, replace bubble sort with sort.Slice,
  use sync.Once for EnsureProviders, define magic number constants
- providers: merge duplicate PC/Mobile functions in alipay/wxpay,
  extract loadKeyPair/queryOrderTotalFen in wxpay, add centsToYuan in stripe,
  define constants for success codes, currencies, event types
- handlers: extract requireAuth and parseIDParam helpers to eliminate
  repeated auth/ID-parsing boilerplate
- registry: remove dead seen map in GetProviderByKey
- types: centralize order status constants in payment package
- load_balancer+config_service: unify duplicated containsType into
  exported InstanceSupportsType function
This commit is contained in:
erio committed 2026-04-06 01:13:30 +08:00
1 parent c69929bbb7
commit 96b1a96cf9
15 files changed
+359 -245

No files matched your search

+17 -11
View File
@@ -4,9 +4,9 @@ import (
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
@@ -88,9 +88,8 @@ type CreateOrderRequest struct {
// CreateOrder creates a new payment order.
// POST /api/v1/payment/orders
func (h *PaymentHandler) CreateOrder(c *gin.Context) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
subject, ok := requireAuth(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return
}
@@ -121,9 +120,8 @@ func (h *PaymentHandler) CreateOrder(c *gin.Context) {
// GetMyOrders returns the authenticated user's orders.
// GET /api/v1/payment/orders/my
func (h *PaymentHandler) GetMyOrders(c *gin.Context) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
subject, ok := requireAuth(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return
}
@@ -145,9 +143,8 @@ func (h *PaymentHandler) GetMyOrders(c *gin.Context) {
// GetOrder returns a single order for the authenticated user.
// GET /api/v1/payment/orders/:id
func (h *PaymentHandler) GetOrder(c *gin.Context) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
subject, ok := requireAuth(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return
}
@@ -168,9 +165,8 @@ func (h *PaymentHandler) GetOrder(c *gin.Context) {
// CancelOrder cancels a pending order for the authenticated user.
// POST /api/v1/payment/orders/:id/cancel
func (h *PaymentHandler) CancelOrder(c *gin.Context) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
subject, ok := requireAuth(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return
}
@@ -196,9 +192,8 @@ type RefundRequestBody struct {
// RequestRefund submits a refund request for a completed order.
// POST /api/v1/payment/orders/:id/refund-request
func (h *PaymentHandler) RequestRefund(c *gin.Context) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
subject, ok := requireAuth(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return
}
@@ -221,6 +216,17 @@ func (h *PaymentHandler) RequestRefund(c *gin.Context) {
response.Success(c, gin.H{"message": "refund requested"})
}
// requireAuth extracts the authenticated subject from the context.
// Returns the subject and true on success; on failure it writes an Unauthorized response and returns false.
func requireAuth(c *gin.Context) (middleware2.AuthSubject, bool) {
subject, ok := middleware2.GetAuthSubjectFromContext(c)
if !ok {
response.Unauthorized(c, "User not authenticated")
return middleware2.AuthSubject{}, false
}
return subject, true
}
// isMobile detects mobile user agents.
func isMobile(c *gin.Context) bool {
ua := strings.ToLower(c.GetHeader("User-Agent"))