Motivation: platform-certificate mode is being phased out by WeChat (2024-10+,
newly-provisioned merchants already cannot download platform certificates at
all), and wxpay config errors currently surface only when an order is being
created — admins have no feedback at save time. Also, errors were returned as
natural-language strings, leaving the frontend no way to localize them.
Changes:
- backend/internal/payment/provider/wxpay.go
- Replace fmt.Errorf with structured infraerrors.BadRequest errors:
- WXPAY_CONFIG_MISSING_KEY (metadata: key)
- WXPAY_CONFIG_INVALID_KEY_LENGTH (metadata: key, expected, actual)
- WXPAY_CONFIG_INVALID_KEY (metadata: key) for malformed PEMs
- Parse privateKey and publicKey PEMs in NewWxpay so malformed keys fail
at save time instead of at order creation.
- Keep the pubkey verifier (NewSHA256WithRSAPubkeyVerifier) as the single
supported verifier; no more loadKeyPair helper.
- backend/internal/service/payment_order.go invokeProvider
- If CreateProvider or CreatePayment returns a structured ApplicationError,
pass it through (optionally enriching metadata with provider/instance_id)
instead of wrapping it as generic PAYMENT_GATEWAY_ERROR — so clients see
the actual reason code (e.g. WXPAY_CONFIG_MISSING_KEY) and can localize.
- Simplify a few messages (TOO_MANY_PENDING, DAILY_LIMIT_EXCEEDED,
PAYMENT_GATEWAY_ERROR, NO_AVAILABLE_INSTANCE) to keyword form with
metadata for template variables.
- backend/internal/service/payment_config_providers.go
- New helper validateProviderConfig calls provider.CreateProvider at save
time. Enabled instances are validated on both Create and Update so admins
see config errors immediately in the dialog, not later at order creation.
- Disabled instances are not validated (half-filled drafts are allowed).
- backend/internal/payment/provider/wxpay_test.go
- Add generateTestKeyPair helper that produces valid RSA-2048 PKCS8/PKIX
PEMs per test, used by the valid-config baseline (prior fake strings no
longer pass the eager PEM check).
- Cover each structured-error branch (missing/invalid-length/malformed PEM).
345 lines
13 KiB
Go
345 lines
13 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
dbent "github.com/Wei-Shaw/sub2api/ent"
|
|
"github.com/Wei-Shaw/sub2api/ent/paymentorder"
|
|
"github.com/Wei-Shaw/sub2api/internal/payment"
|
|
"github.com/Wei-Shaw/sub2api/internal/payment/provider"
|
|
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
|
)
|
|
|
|
// --- Order Creation ---
|
|
|
|
func (s *PaymentService) CreateOrder(ctx context.Context, req CreateOrderRequest) (*CreateOrderResponse, error) {
|
|
if req.OrderType == "" {
|
|
req.OrderType = payment.OrderTypeBalance
|
|
}
|
|
cfg, err := s.configService.GetPaymentConfig(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get payment config: %w", err)
|
|
}
|
|
if !cfg.Enabled {
|
|
return nil, infraerrors.Forbidden("PAYMENT_DISABLED", "payment system is disabled")
|
|
}
|
|
plan, err := s.validateOrderInput(ctx, req, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.checkCancelRateLimit(ctx, req.UserID, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
user, err := s.userRepo.GetByID(ctx, req.UserID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get user: %w", err)
|
|
}
|
|
if user.Status != payment.EntityStatusActive {
|
|
return nil, infraerrors.Forbidden("USER_INACTIVE", "user account is disabled")
|
|
}
|
|
orderAmount := req.Amount
|
|
limitAmount := req.Amount
|
|
if plan != nil {
|
|
orderAmount = plan.Price
|
|
limitAmount = plan.Price
|
|
} else if req.OrderType == payment.OrderTypeBalance {
|
|
orderAmount = calculateCreditedBalance(req.Amount, cfg.BalanceRechargeMultiplier)
|
|
}
|
|
feeRate := cfg.RechargeFeeRate
|
|
payAmountStr := payment.CalculatePayAmount(limitAmount, feeRate)
|
|
payAmount, _ := strconv.ParseFloat(payAmountStr, 64)
|
|
order, err := s.createOrderInTx(ctx, req, user, plan, cfg, orderAmount, limitAmount, feeRate, payAmount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := s.invokeProvider(ctx, order, req, cfg, limitAmount, payAmountStr, payAmount, plan)
|
|
if err != nil {
|
|
_, _ = s.entClient.PaymentOrder.UpdateOneID(order.ID).
|
|
SetStatus(OrderStatusFailed).
|
|
Save(ctx)
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *PaymentService) validateOrderInput(ctx context.Context, req CreateOrderRequest, cfg *PaymentConfig) (*dbent.SubscriptionPlan, error) {
|
|
if req.OrderType == payment.OrderTypeBalance && cfg.BalanceDisabled {
|
|
return nil, infraerrors.Forbidden("BALANCE_PAYMENT_DISABLED", "balance recharge has been disabled")
|
|
}
|
|
if req.OrderType == payment.OrderTypeSubscription {
|
|
return s.validateSubOrder(ctx, req)
|
|
}
|
|
if math.IsNaN(req.Amount) || math.IsInf(req.Amount, 0) || req.Amount <= 0 {
|
|
return nil, infraerrors.BadRequest("INVALID_AMOUNT", "amount must be a positive number")
|
|
}
|
|
if (cfg.MinAmount > 0 && req.Amount < cfg.MinAmount) || (cfg.MaxAmount > 0 && req.Amount > cfg.MaxAmount) {
|
|
return nil, infraerrors.BadRequest("INVALID_AMOUNT", "amount out of range").
|
|
WithMetadata(map[string]string{"min": fmt.Sprintf("%.2f", cfg.MinAmount), "max": fmt.Sprintf("%.2f", cfg.MaxAmount)})
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *PaymentService) validateSubOrder(ctx context.Context, req CreateOrderRequest) (*dbent.SubscriptionPlan, error) {
|
|
if req.PlanID == 0 {
|
|
return nil, infraerrors.BadRequest("INVALID_INPUT", "subscription order requires a plan")
|
|
}
|
|
plan, err := s.configService.GetPlan(ctx, req.PlanID)
|
|
if err != nil || !plan.ForSale {
|
|
return nil, infraerrors.NotFound("PLAN_NOT_AVAILABLE", "plan not found or not for sale")
|
|
}
|
|
group, err := s.groupRepo.GetByID(ctx, plan.GroupID)
|
|
if err != nil || group.Status != payment.EntityStatusActive {
|
|
return nil, infraerrors.NotFound("GROUP_NOT_FOUND", "subscription group is no longer available")
|
|
}
|
|
if !group.IsSubscriptionType() {
|
|
return nil, infraerrors.BadRequest("GROUP_TYPE_MISMATCH", "group is not a subscription type")
|
|
}
|
|
return plan, nil
|
|
}
|
|
|
|
func (s *PaymentService) createOrderInTx(ctx context.Context, req CreateOrderRequest, user *User, plan *dbent.SubscriptionPlan, cfg *PaymentConfig, orderAmount, limitAmount, feeRate, payAmount float64) (*dbent.PaymentOrder, error) {
|
|
tx, err := s.entClient.Tx(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("begin transaction: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
if err := s.checkPendingLimit(ctx, tx, req.UserID, cfg.MaxPendingOrders); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.checkDailyLimit(ctx, tx, req.UserID, limitAmount, cfg.DailyLimit); err != nil {
|
|
return nil, err
|
|
}
|
|
tm := cfg.OrderTimeoutMin
|
|
if tm <= 0 {
|
|
tm = defaultOrderTimeoutMin
|
|
}
|
|
exp := time.Now().Add(time.Duration(tm) * time.Minute)
|
|
b := tx.PaymentOrder.Create().
|
|
SetUserID(req.UserID).
|
|
SetUserEmail(user.Email).
|
|
SetUserName(user.Username).
|
|
SetNillableUserNotes(psNilIfEmpty(user.Notes)).
|
|
SetAmount(orderAmount).
|
|
SetPayAmount(payAmount).
|
|
SetFeeRate(feeRate).
|
|
SetRechargeCode("").
|
|
SetOutTradeNo(generateOutTradeNo()).
|
|
SetPaymentType(req.PaymentType).
|
|
SetPaymentTradeNo("").
|
|
SetOrderType(req.OrderType).
|
|
SetStatus(OrderStatusPending).
|
|
SetExpiresAt(exp).
|
|
SetClientIP(req.ClientIP).
|
|
SetSrcHost(req.SrcHost)
|
|
if req.SrcURL != "" {
|
|
b.SetSrcURL(req.SrcURL)
|
|
}
|
|
if plan != nil {
|
|
b.SetPlanID(plan.ID).SetSubscriptionGroupID(plan.GroupID).SetSubscriptionDays(psComputeValidityDays(plan.ValidityDays, plan.ValidityUnit))
|
|
}
|
|
order, err := b.Save(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create order: %w", err)
|
|
}
|
|
code := fmt.Sprintf("PAY-%d-%d", order.ID, time.Now().UnixNano()%100000)
|
|
order, err = tx.PaymentOrder.UpdateOneID(order.ID).SetRechargeCode(code).Save(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("set recharge code: %w", err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, fmt.Errorf("commit order transaction: %w", err)
|
|
}
|
|
return order, nil
|
|
}
|
|
|
|
func (s *PaymentService) checkPendingLimit(ctx context.Context, tx *dbent.Tx, userID int64, max int) error {
|
|
if max <= 0 {
|
|
max = defaultMaxPendingOrders
|
|
}
|
|
c, err := tx.PaymentOrder.Query().Where(paymentorder.UserIDEQ(userID), paymentorder.StatusEQ(OrderStatusPending)).Count(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("count pending orders: %w", err)
|
|
}
|
|
if c >= max {
|
|
return infraerrors.TooManyRequests("TOO_MANY_PENDING", "too_many_pending").
|
|
WithMetadata(map[string]string{"max": strconv.Itoa(max)})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PaymentService) checkDailyLimit(ctx context.Context, tx *dbent.Tx, userID int64, amount, limit float64) error {
|
|
if limit <= 0 {
|
|
return nil
|
|
}
|
|
ts := psStartOfDayUTC(time.Now())
|
|
orders, err := tx.PaymentOrder.Query().Where(paymentorder.UserIDEQ(userID), paymentorder.StatusIn(OrderStatusPaid, OrderStatusRecharging, OrderStatusCompleted), paymentorder.PaidAtGTE(ts)).All(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("query daily usage: %w", err)
|
|
}
|
|
var used float64
|
|
for _, o := range orders {
|
|
if o.OrderType == payment.OrderTypeBalance {
|
|
used += o.PayAmount
|
|
continue
|
|
}
|
|
used += o.Amount
|
|
}
|
|
if used+amount > limit {
|
|
return infraerrors.TooManyRequests("DAILY_LIMIT_EXCEEDED", "daily_limit_exceeded").
|
|
WithMetadata(map[string]string{"remaining": fmt.Sprintf("%.2f", math.Max(0, limit-used))})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PaymentService) invokeProvider(ctx context.Context, order *dbent.PaymentOrder, req CreateOrderRequest, cfg *PaymentConfig, limitAmount float64, payAmountStr string, payAmount float64, plan *dbent.SubscriptionPlan) (*CreateOrderResponse, error) {
|
|
// Select an instance across all providers that support the requested payment type.
|
|
// This enables cross-provider load balancing (e.g. EasyPay + Alipay direct for "alipay").
|
|
sel, err := s.loadBalancer.SelectInstance(ctx, "", req.PaymentType, payment.Strategy(cfg.LoadBalanceStrategy), payAmount)
|
|
if err != nil {
|
|
return nil, infraerrors.ServiceUnavailable("PAYMENT_GATEWAY_ERROR", "method_not_configured").
|
|
WithMetadata(map[string]string{"payment_type": req.PaymentType})
|
|
}
|
|
if sel == nil {
|
|
return nil, infraerrors.TooManyRequests("NO_AVAILABLE_INSTANCE", "no_available_instance")
|
|
}
|
|
prov, err := provider.CreateProvider(sel.ProviderKey, sel.InstanceID, sel.Config)
|
|
if err != nil {
|
|
slog.Error("[PaymentService] CreateProvider failed", "provider", sel.ProviderKey, "instance", sel.InstanceID, "error", err)
|
|
// If the provider returned a structured ApplicationError (e.g. WXPAY_CONFIG_MISSING_KEY),
|
|
// pass it through with provider context added to metadata. Otherwise wrap as PAYMENT_PROVIDER_MISCONFIGURED.
|
|
if appErr := new(infraerrors.ApplicationError); errors.As(err, &appErr) {
|
|
md := map[string]string{"provider": sel.ProviderKey, "instance_id": sel.InstanceID}
|
|
for k, v := range appErr.Metadata {
|
|
md[k] = v
|
|
}
|
|
return nil, appErr.WithMetadata(md)
|
|
}
|
|
return nil, infraerrors.ServiceUnavailable("PAYMENT_PROVIDER_MISCONFIGURED", "provider_misconfigured").
|
|
WithMetadata(map[string]string{"provider": sel.ProviderKey, "instance_id": sel.InstanceID})
|
|
}
|
|
subject := s.buildPaymentSubject(plan, limitAmount, cfg)
|
|
outTradeNo := order.OutTradeNo
|
|
pr, err := prov.CreatePayment(ctx, payment.CreatePaymentRequest{OrderID: outTradeNo, Amount: payAmountStr, PaymentType: req.PaymentType, Subject: subject, ClientIP: req.ClientIP, IsMobile: req.IsMobile, InstanceSubMethods: sel.SupportedTypes})
|
|
if err != nil {
|
|
slog.Error("[PaymentService] CreatePayment failed", "provider", sel.ProviderKey, "instance", sel.InstanceID, "error", err)
|
|
if appErr := new(infraerrors.ApplicationError); errors.As(err, &appErr) {
|
|
return nil, appErr
|
|
}
|
|
return nil, infraerrors.ServiceUnavailable("PAYMENT_GATEWAY_ERROR", "payment_gateway_error").
|
|
WithMetadata(map[string]string{"provider": sel.ProviderKey, "instance_id": sel.InstanceID})
|
|
}
|
|
_, err = s.entClient.PaymentOrder.UpdateOneID(order.ID).SetNillablePaymentTradeNo(psNilIfEmpty(pr.TradeNo)).SetNillablePayURL(psNilIfEmpty(pr.PayURL)).SetNillableQrCode(psNilIfEmpty(pr.QRCode)).SetNillableProviderInstanceID(psNilIfEmpty(sel.InstanceID)).Save(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("update order with payment details: %w", err)
|
|
}
|
|
s.writeAuditLog(ctx, order.ID, "ORDER_CREATED", fmt.Sprintf("user:%d", req.UserID), map[string]any{
|
|
"paymentAmount": req.Amount,
|
|
"creditedAmount": order.Amount,
|
|
"payAmount": order.PayAmount,
|
|
"paymentType": req.PaymentType,
|
|
"orderType": req.OrderType,
|
|
})
|
|
return &CreateOrderResponse{OrderID: order.ID, Amount: order.Amount, PayAmount: payAmount, FeeRate: order.FeeRate, Status: OrderStatusPending, PaymentType: req.PaymentType, PayURL: pr.PayURL, QRCode: pr.QRCode, ClientSecret: pr.ClientSecret, ExpiresAt: order.ExpiresAt, PaymentMode: sel.PaymentMode}, nil
|
|
}
|
|
|
|
func (s *PaymentService) buildPaymentSubject(plan *dbent.SubscriptionPlan, limitAmount float64, cfg *PaymentConfig) string {
|
|
if plan != nil {
|
|
if plan.ProductName != "" {
|
|
return plan.ProductName
|
|
}
|
|
return "Sub2API Subscription " + plan.Name
|
|
}
|
|
amountStr := strconv.FormatFloat(limitAmount, 'f', 2, 64)
|
|
pf := strings.TrimSpace(cfg.ProductNamePrefix)
|
|
sf := strings.TrimSpace(cfg.ProductNameSuffix)
|
|
if pf != "" || sf != "" {
|
|
return strings.TrimSpace(pf + " " + amountStr + " " + sf)
|
|
}
|
|
return "Sub2API " + amountStr + " CNY"
|
|
}
|
|
|
|
// --- Order Queries ---
|
|
|
|
func (s *PaymentService) GetOrder(ctx context.Context, orderID, userID int64) (*dbent.PaymentOrder, error) {
|
|
o, err := s.entClient.PaymentOrder.Get(ctx, orderID)
|
|
if err != nil {
|
|
return nil, infraerrors.NotFound("NOT_FOUND", "order not found")
|
|
}
|
|
if o.UserID != userID {
|
|
return nil, infraerrors.Forbidden("FORBIDDEN", "no permission for this order")
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func (s *PaymentService) GetOrderByID(ctx context.Context, orderID int64) (*dbent.PaymentOrder, error) {
|
|
o, err := s.entClient.PaymentOrder.Get(ctx, orderID)
|
|
if err != nil {
|
|
return nil, infraerrors.NotFound("NOT_FOUND", "order not found")
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func (s *PaymentService) GetUserOrders(ctx context.Context, userID int64, p OrderListParams) ([]*dbent.PaymentOrder, int, error) {
|
|
q := s.entClient.PaymentOrder.Query().Where(paymentorder.UserIDEQ(userID))
|
|
if p.Status != "" {
|
|
q = q.Where(paymentorder.StatusEQ(p.Status))
|
|
}
|
|
if p.OrderType != "" {
|
|
q = q.Where(paymentorder.OrderTypeEQ(p.OrderType))
|
|
}
|
|
if p.PaymentType != "" {
|
|
q = q.Where(paymentorder.PaymentTypeEQ(p.PaymentType))
|
|
}
|
|
total, err := q.Clone().Count(ctx)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("count user orders: %w", err)
|
|
}
|
|
ps, pg := applyPagination(p.PageSize, p.Page)
|
|
orders, err := q.Order(dbent.Desc(paymentorder.FieldCreatedAt)).Limit(ps).Offset((pg - 1) * ps).All(ctx)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("query user orders: %w", err)
|
|
}
|
|
return orders, total, nil
|
|
}
|
|
|
|
// AdminListOrders returns a paginated list of orders. If userID > 0, filters by user.
|
|
func (s *PaymentService) AdminListOrders(ctx context.Context, userID int64, p OrderListParams) ([]*dbent.PaymentOrder, int, error) {
|
|
q := s.entClient.PaymentOrder.Query()
|
|
if userID > 0 {
|
|
q = q.Where(paymentorder.UserIDEQ(userID))
|
|
}
|
|
if p.Status != "" {
|
|
q = q.Where(paymentorder.StatusEQ(p.Status))
|
|
}
|
|
if p.OrderType != "" {
|
|
q = q.Where(paymentorder.OrderTypeEQ(p.OrderType))
|
|
}
|
|
if p.PaymentType != "" {
|
|
q = q.Where(paymentorder.PaymentTypeEQ(p.PaymentType))
|
|
}
|
|
if p.Keyword != "" {
|
|
q = q.Where(paymentorder.Or(
|
|
paymentorder.OutTradeNoContainsFold(p.Keyword),
|
|
paymentorder.UserEmailContainsFold(p.Keyword),
|
|
paymentorder.UserNameContainsFold(p.Keyword),
|
|
))
|
|
}
|
|
total, err := q.Clone().Count(ctx)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("count admin orders: %w", err)
|
|
}
|
|
ps, pg := applyPagination(p.PageSize, p.Page)
|
|
orders, err := q.Order(dbent.Desc(paymentorder.FieldCreatedAt)).Limit(ps).Offset((pg - 1) * ps).All(ctx)
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("query admin orders: %w", err)
|
|
}
|
|
return orders, total, nil
|
|
}
|