mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-21 15:09:21 +00:00
Replace iframe-based external payment with native implementation: Backend (Go): - 5 new Ent schemas: PaymentOrder, PaymentAuditLog, PaymentChannel, SubscriptionPlan, PaymentProviderInstance - 5 SQL migrations (090-094) - Payment provider interface + registry + load balancer - 4 provider implementations (EasyPay, Alipay, WxPay, Stripe) - PaymentService (order lifecycle, fulfillment, refund 3-phase) - PaymentConfigService (settings, instance/channel/plan CRUD) - 31 API endpoints (9 user + 4 webhook + 18 admin) - Wire DI integration Frontend (Vue3): - Payment types, API modules, store, router changes - Sidebar: payment_enabled controls visibility, new menu items - User pages: PaymentView (replace iframe), QR payment, Stripe, result page, order list - Admin pages: Order management (4 tabs), provider instance mgmt - Settings: new payment tab, removed old purchase iframe config - i18n: full payment namespace (en + zh) Docs: - PAYMENT_FRONTEND_MIGRATION_PLAN.md - PAYMENT_BACKEND_MIGRATION_PLAN.md - PAYMENT_IMPLEMENTATION_ROADMAP.md
261 lines
6.6 KiB
Go
261 lines
6.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/payment"
|
|
"github.com/smartwalle/alipay/v3"
|
|
)
|
|
|
|
// Alipay implements payment.Provider and payment.CancelableProvider using the smartwalle/alipay SDK.
|
|
type Alipay struct {
|
|
instanceID string
|
|
config map[string]string // appId, privateKey, publicKey (or alipayPublicKey), notifyUrl, returnUrl
|
|
|
|
mu sync.Mutex
|
|
client *alipay.Client
|
|
}
|
|
|
|
// NewAlipay creates a new Alipay provider instance.
|
|
func NewAlipay(instanceID string, config map[string]string) (*Alipay, error) {
|
|
required := []string{"appId", "privateKey"}
|
|
for _, k := range required {
|
|
if config[k] == "" {
|
|
return nil, fmt.Errorf("alipay config missing required key: %s", k)
|
|
}
|
|
}
|
|
return &Alipay{
|
|
instanceID: instanceID,
|
|
config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (a *Alipay) getClient() (*alipay.Client, error) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if a.client != nil {
|
|
return a.client, nil
|
|
}
|
|
client, err := alipay.New(a.config["appId"], a.config["privateKey"], true)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay init client: %w", err)
|
|
}
|
|
pubKey := a.config["publicKey"]
|
|
if pubKey == "" {
|
|
pubKey = a.config["alipayPublicKey"]
|
|
}
|
|
if pubKey != "" {
|
|
if err := client.LoadAliPayPublicKey(pubKey); err != nil {
|
|
return nil, fmt.Errorf("alipay load public key: %w", err)
|
|
}
|
|
}
|
|
a.client = client
|
|
return a.client, nil
|
|
}
|
|
|
|
func (a *Alipay) Name() string { return "Alipay" }
|
|
func (a *Alipay) ProviderKey() string { return "alipay" }
|
|
func (a *Alipay) SupportedTypes() []payment.PaymentType {
|
|
return []payment.PaymentType{payment.TypeAlipayDirect}
|
|
}
|
|
|
|
// CreatePayment creates an Alipay payment page URL.
|
|
func (a *Alipay) CreatePayment(_ context.Context, req payment.CreatePaymentRequest) (*payment.CreatePaymentResponse, error) {
|
|
client, err := a.getClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
notifyURL := a.config["notifyUrl"]
|
|
if req.NotifyURL != "" {
|
|
notifyURL = req.NotifyURL
|
|
}
|
|
returnURL := a.config["returnUrl"]
|
|
if req.ReturnURL != "" {
|
|
returnURL = req.ReturnURL
|
|
}
|
|
|
|
if req.IsMobile {
|
|
return a.createWapPayment(client, req, notifyURL, returnURL)
|
|
}
|
|
return a.createPagePayment(client, req, notifyURL, returnURL)
|
|
}
|
|
|
|
func (a *Alipay) createPagePayment(client *alipay.Client, req payment.CreatePaymentRequest, notifyURL, returnURL string) (*payment.CreatePaymentResponse, error) {
|
|
param := alipay.TradePagePay{}
|
|
param.OutTradeNo = req.OrderID
|
|
param.TotalAmount = req.Amount
|
|
param.Subject = req.Subject
|
|
param.ProductCode = "FAST_INSTANT_TRADE_PAY"
|
|
param.NotifyURL = notifyURL
|
|
param.ReturnURL = returnURL
|
|
|
|
payURL, err := client.TradePagePay(param)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay TradePagePay: %w", err)
|
|
}
|
|
|
|
return &payment.CreatePaymentResponse{
|
|
TradeNo: req.OrderID,
|
|
PayURL: payURL.String(),
|
|
QRCode: payURL.String(),
|
|
}, nil
|
|
}
|
|
|
|
func (a *Alipay) createWapPayment(client *alipay.Client, req payment.CreatePaymentRequest, notifyURL, returnURL string) (*payment.CreatePaymentResponse, error) {
|
|
param := alipay.TradeWapPay{}
|
|
param.OutTradeNo = req.OrderID
|
|
param.TotalAmount = req.Amount
|
|
param.Subject = req.Subject
|
|
param.ProductCode = "QUICK_WAP_WAY"
|
|
param.NotifyURL = notifyURL
|
|
param.ReturnURL = returnURL
|
|
|
|
payURL, err := client.TradeWapPay(param)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay TradeWapPay: %w", err)
|
|
}
|
|
|
|
return &payment.CreatePaymentResponse{
|
|
TradeNo: req.OrderID,
|
|
PayURL: payURL.String(),
|
|
}, nil
|
|
}
|
|
|
|
// QueryOrder queries the trade status via Alipay.
|
|
func (a *Alipay) QueryOrder(ctx context.Context, tradeNo string) (*payment.QueryOrderResponse, error) {
|
|
client, err := a.getClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result, err := client.TradeQuery(ctx, alipay.TradeQuery{OutTradeNo: tradeNo})
|
|
if err != nil {
|
|
if isTradeNotExist(err) {
|
|
return &payment.QueryOrderResponse{
|
|
TradeNo: tradeNo,
|
|
Status: "pending",
|
|
}, nil
|
|
}
|
|
return nil, fmt.Errorf("alipay TradeQuery: %w", err)
|
|
}
|
|
|
|
status := "pending"
|
|
switch result.TradeStatus {
|
|
case alipay.TradeStatusSuccess, alipay.TradeStatusFinished:
|
|
status = "paid"
|
|
case alipay.TradeStatusClosed:
|
|
status = "failed"
|
|
}
|
|
|
|
amount, _ := strconv.ParseFloat(result.TotalAmount, 64)
|
|
|
|
return &payment.QueryOrderResponse{
|
|
TradeNo: result.TradeNo,
|
|
Status: status,
|
|
Amount: amount,
|
|
PaidAt: result.SendPayDate,
|
|
}, nil
|
|
}
|
|
|
|
// VerifyNotification decodes and verifies an Alipay async notification.
|
|
func (a *Alipay) VerifyNotification(ctx context.Context, rawBody string, _ map[string]string) (*payment.PaymentNotification, error) {
|
|
client, err := a.getClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
values, err := url.ParseQuery(rawBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay parse notification: %w", err)
|
|
}
|
|
|
|
notification, err := client.DecodeNotification(ctx, values)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay verify notification: %w", err)
|
|
}
|
|
|
|
status := "failed"
|
|
if notification.TradeStatus == alipay.TradeStatusSuccess || notification.TradeStatus == alipay.TradeStatusFinished {
|
|
status = "success"
|
|
}
|
|
|
|
amount, _ := strconv.ParseFloat(notification.TotalAmount, 64)
|
|
|
|
return &payment.PaymentNotification{
|
|
TradeNo: notification.TradeNo,
|
|
OrderID: notification.OutTradeNo,
|
|
Amount: amount,
|
|
Status: status,
|
|
RawData: rawBody,
|
|
}, nil
|
|
}
|
|
|
|
// Refund requests a refund through Alipay.
|
|
func (a *Alipay) Refund(ctx context.Context, req payment.RefundRequest) (*payment.RefundResponse, error) {
|
|
client, err := a.getClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result, err := client.TradeRefund(ctx, alipay.TradeRefund{
|
|
OutTradeNo: req.OrderID,
|
|
RefundAmount: req.Amount,
|
|
RefundReason: req.Reason,
|
|
OutRequestNo: req.OrderID + "-refund",
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alipay TradeRefund: %w", err)
|
|
}
|
|
|
|
refundStatus := "pending"
|
|
if result.FundChange == "Y" {
|
|
refundStatus = "success"
|
|
}
|
|
|
|
refundID := result.TradeNo
|
|
if refundID == "" {
|
|
refundID = req.OrderID + "-refund"
|
|
}
|
|
|
|
return &payment.RefundResponse{
|
|
RefundID: refundID,
|
|
Status: refundStatus,
|
|
}, nil
|
|
}
|
|
|
|
// CancelPayment closes a pending trade on Alipay.
|
|
func (a *Alipay) CancelPayment(ctx context.Context, tradeNo string) error {
|
|
client, err := a.getClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = client.TradeClose(ctx, alipay.TradeClose{OutTradeNo: tradeNo})
|
|
if err != nil {
|
|
if isTradeNotExist(err) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("alipay TradeClose: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isTradeNotExist(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
return strings.Contains(err.Error(), "ACQ.TRADE_NOT_EXIST")
|
|
}
|
|
|
|
// Ensure interface compliance.
|
|
var (
|
|
_ payment.Provider = (*Alipay)(nil)
|
|
_ payment.CancelableProvider = (*Alipay)(nil)
|
|
)
|