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
312 lines
9.4 KiB
Go
312 lines
9.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/payment"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/h5"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
|
)
|
|
|
|
type Wxpay struct {
|
|
instanceID string
|
|
config map[string]string
|
|
mu sync.Mutex
|
|
coreClient *core.Client
|
|
notifyHandler *notify.Handler
|
|
}
|
|
|
|
func NewWxpay(instanceID string, config map[string]string) (*Wxpay, error) {
|
|
required := []string{"appId", "mchId", "privateKey", "apiV3Key", "publicKey", "publicKeyId"}
|
|
for _, k := range required {
|
|
if config[k] == "" {
|
|
return nil, fmt.Errorf("wxpay config missing required key: %s", k)
|
|
}
|
|
}
|
|
if len(config["apiV3Key"]) != 32 {
|
|
return nil, fmt.Errorf("wxpay apiV3Key must be exactly 32 bytes, got %d", len(config["apiV3Key"]))
|
|
}
|
|
return &Wxpay{instanceID: instanceID, config: config}, nil
|
|
}
|
|
|
|
func (w *Wxpay) Name() string { return "Wxpay" }
|
|
func (w *Wxpay) ProviderKey() string { return "wxpay" }
|
|
func (w *Wxpay) SupportedTypes() []payment.PaymentType {
|
|
return []payment.PaymentType{payment.TypeWxpayDirect}
|
|
}
|
|
|
|
func formatPEM(key, keyType string) string {
|
|
key = strings.TrimSpace(key)
|
|
if strings.HasPrefix(key, "-----BEGIN") {
|
|
return key
|
|
}
|
|
return fmt.Sprintf("-----BEGIN %s-----\n%s\n-----END %s-----", keyType, key, keyType)
|
|
}
|
|
|
|
func (w *Wxpay) ensureClient() (*core.Client, error) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if w.coreClient != nil {
|
|
return w.coreClient, nil
|
|
}
|
|
privateKey, err := utils.LoadPrivateKey(formatPEM(w.config["privateKey"], "PRIVATE KEY"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay load private key: %w", err)
|
|
}
|
|
publicKey, err := utils.LoadPublicKey(formatPEM(w.config["publicKey"], "PUBLIC KEY"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay load public key: %w", err)
|
|
}
|
|
certSerial := w.config["certSerial"]
|
|
if certSerial == "" {
|
|
certSerial = w.config["publicKeyId"]
|
|
}
|
|
verifier := verifiers.NewSHA256WithRSAPubkeyVerifier(w.config["publicKeyId"], *publicKey)
|
|
client, err := core.NewClient(context.Background(),
|
|
option.WithMerchantCredential(w.config["mchId"], certSerial, privateKey),
|
|
option.WithVerifier(verifier))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay init client: %w", err)
|
|
}
|
|
handler, err := notify.NewRSANotifyHandler(w.config["apiV3Key"], verifier)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay init notify handler: %w", err)
|
|
}
|
|
w.notifyHandler = handler
|
|
w.coreClient = client
|
|
return w.coreClient, nil
|
|
}
|
|
|
|
func yuanToFen(s string) (int64, error) {
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid amount: %s", s)
|
|
}
|
|
return int64(math.Round(f * 100)), nil
|
|
}
|
|
|
|
func (w *Wxpay) CreatePayment(ctx context.Context, req payment.CreatePaymentRequest) (*payment.CreatePaymentResponse, error) {
|
|
client, err := w.ensureClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
notifyURL := w.config["notifyUrl"]
|
|
if notifyURL == "" {
|
|
notifyURL = req.NotifyURL
|
|
}
|
|
if notifyURL == "" {
|
|
return nil, fmt.Errorf("wxpay notifyUrl is required")
|
|
}
|
|
totalFen, err := yuanToFen(req.Amount)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay create payment: %w", err)
|
|
}
|
|
if req.IsMobile && req.ClientIP != "" {
|
|
resp, err := w.createH5Order(ctx, client, req, notifyURL, totalFen)
|
|
if err == nil {
|
|
return resp, nil
|
|
}
|
|
if !strings.Contains(err.Error(), "NO_AUTH") {
|
|
return nil, err
|
|
}
|
|
}
|
|
return w.createNativeOrder(ctx, client, req, notifyURL, totalFen)
|
|
}
|
|
|
|
func (w *Wxpay) createNativeOrder(ctx context.Context, c *core.Client, req payment.CreatePaymentRequest, notifyURL string, totalFen int64) (*payment.CreatePaymentResponse, error) {
|
|
svc := native.NativeApiService{Client: c}
|
|
cur := "CNY"
|
|
resp, _, err := svc.Prepay(ctx, native.PrepayRequest{
|
|
Appid: core.String(w.config["appId"]), Mchid: core.String(w.config["mchId"]),
|
|
Description: core.String(req.Subject), OutTradeNo: core.String(req.OrderID),
|
|
NotifyUrl: core.String(notifyURL),
|
|
Amount: &native.Amount{Total: core.Int64(totalFen), Currency: &cur},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay native prepay: %w", err)
|
|
}
|
|
codeURL := ""
|
|
if resp.CodeUrl != nil {
|
|
codeURL = *resp.CodeUrl
|
|
}
|
|
return &payment.CreatePaymentResponse{TradeNo: req.OrderID, QRCode: codeURL}, nil
|
|
}
|
|
|
|
func (w *Wxpay) createH5Order(ctx context.Context, c *core.Client, req payment.CreatePaymentRequest, notifyURL string, totalFen int64) (*payment.CreatePaymentResponse, error) {
|
|
svc := h5.H5ApiService{Client: c}
|
|
cur := "CNY"
|
|
tp := "Wap"
|
|
resp, _, err := svc.Prepay(ctx, h5.PrepayRequest{
|
|
Appid: core.String(w.config["appId"]), Mchid: core.String(w.config["mchId"]),
|
|
Description: core.String(req.Subject), OutTradeNo: core.String(req.OrderID),
|
|
NotifyUrl: core.String(notifyURL),
|
|
Amount: &h5.Amount{Total: core.Int64(totalFen), Currency: &cur},
|
|
SceneInfo: &h5.SceneInfo{PayerClientIp: core.String(req.ClientIP), H5Info: &h5.H5Info{Type: &tp}},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay h5 prepay: %w", err)
|
|
}
|
|
h5URL := ""
|
|
if resp.H5Url != nil {
|
|
h5URL = *resp.H5Url
|
|
}
|
|
return &payment.CreatePaymentResponse{TradeNo: req.OrderID, PayURL: h5URL}, nil
|
|
}
|
|
|
|
func wxSV(s *string) string {
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
return *s
|
|
}
|
|
|
|
func mapWxState(s string) string {
|
|
switch s {
|
|
case "SUCCESS":
|
|
return "paid"
|
|
case "REFUND":
|
|
return "refunded"
|
|
case "CLOSED", "PAYERROR":
|
|
return "failed"
|
|
default:
|
|
return "pending"
|
|
}
|
|
}
|
|
|
|
func (w *Wxpay) QueryOrder(ctx context.Context, tradeNo string) (*payment.QueryOrderResponse, error) {
|
|
c, err := w.ensureClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
svc := native.NativeApiService{Client: c}
|
|
tx, _, err := svc.QueryOrderByOutTradeNo(ctx, native.QueryOrderByOutTradeNoRequest{
|
|
OutTradeNo: core.String(tradeNo), Mchid: core.String(w.config["mchId"]),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay query order: %w", err)
|
|
}
|
|
var amt float64
|
|
if tx.Amount != nil && tx.Amount.Total != nil {
|
|
amt = float64(*tx.Amount.Total) / 100
|
|
}
|
|
id := tradeNo
|
|
if tx.TransactionId != nil {
|
|
id = *tx.TransactionId
|
|
}
|
|
pa := ""
|
|
if tx.SuccessTime != nil {
|
|
pa = *tx.SuccessTime
|
|
}
|
|
return &payment.QueryOrderResponse{TradeNo: id, Status: mapWxState(wxSV(tx.TradeState)), Amount: amt, PaidAt: pa}, nil
|
|
}
|
|
|
|
func (w *Wxpay) VerifyNotification(ctx context.Context, rawBody string, headers map[string]string) (*payment.PaymentNotification, error) {
|
|
if _, err := w.ensureClient(); err != nil {
|
|
return nil, err
|
|
}
|
|
r, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", io.NopCloser(bytes.NewBufferString(rawBody)))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay construct request: %w", err)
|
|
}
|
|
for k, v := range headers {
|
|
r.Header.Set(k, v)
|
|
}
|
|
var tx payments.Transaction
|
|
nr, err := w.notifyHandler.ParseNotifyRequest(ctx, r, &tx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay verify notification: %w", err)
|
|
}
|
|
if nr.EventType != "TRANSACTION.SUCCESS" {
|
|
return nil, nil
|
|
}
|
|
var amt float64
|
|
if tx.Amount != nil && tx.Amount.Total != nil {
|
|
amt = float64(*tx.Amount.Total) / 100
|
|
}
|
|
st := "failed"
|
|
if wxSV(tx.TradeState) == "SUCCESS" {
|
|
st = "success"
|
|
}
|
|
return &payment.PaymentNotification{
|
|
TradeNo: wxSV(tx.TransactionId), OrderID: wxSV(tx.OutTradeNo),
|
|
Amount: amt, Status: st, RawData: rawBody,
|
|
}, nil
|
|
}
|
|
|
|
func (w *Wxpay) Refund(ctx context.Context, req payment.RefundRequest) (*payment.RefundResponse, error) {
|
|
c, err := w.ensureClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rf, err := yuanToFen(req.Amount)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay refund amount: %w", err)
|
|
}
|
|
svc := native.NativeApiService{Client: c}
|
|
tx, _, err := svc.QueryOrderByOutTradeNo(ctx, native.QueryOrderByOutTradeNoRequest{
|
|
OutTradeNo: core.String(req.OrderID), Mchid: core.String(w.config["mchId"]),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay refund query order: %w", err)
|
|
}
|
|
var tf int64
|
|
if tx.Amount != nil && tx.Amount.Total != nil {
|
|
tf = *tx.Amount.Total
|
|
}
|
|
rs := refunddomestic.RefundsApiService{Client: c}
|
|
cur := "CNY"
|
|
res, _, err := rs.Create(ctx, refunddomestic.CreateRequest{
|
|
OutTradeNo: core.String(req.OrderID),
|
|
OutRefundNo: core.String(fmt.Sprintf("refund-%s", req.OrderID)),
|
|
Reason: core.String(req.Reason),
|
|
Amount: &refunddomestic.AmountReq{Refund: core.Int64(rf), Total: core.Int64(tf), Currency: &cur},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wxpay refund: %w", err)
|
|
}
|
|
rid := wxSV(res.RefundId)
|
|
if rid == "" {
|
|
rid = fmt.Sprintf("%s-refund", req.OrderID)
|
|
}
|
|
st := "pending"
|
|
if res.Status != nil && *res.Status == refunddomestic.STATUS_SUCCESS {
|
|
st = "success"
|
|
}
|
|
return &payment.RefundResponse{RefundID: rid, Status: st}, nil
|
|
}
|
|
|
|
func (w *Wxpay) CancelPayment(ctx context.Context, tradeNo string) error {
|
|
c, err := w.ensureClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
svc := native.NativeApiService{Client: c}
|
|
_, err = svc.CloseOrder(ctx, native.CloseOrderRequest{
|
|
OutTradeNo: core.String(tradeNo), Mchid: core.String(w.config["mchId"]),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("wxpay cancel payment: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
_ payment.Provider = (*Wxpay)(nil)
|
|
_ payment.CancelableProvider = (*Wxpay)(nil)
|
|
)
|