feat(payment): 虎皮椒聚合支付 - 官方插件与跨平台可执行文件
新增 xunhupay 官方包,支付 RPC 与宿主只保留订单和验签边界;官方支付包同时带上各系统 provider,避免把 Linux ELF 当成本机入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e5a5f0ccc8
commit
a23df0cb32
63 files changed
+2070
-128
No files matched your search
@@ -187,7 +187,7 @@ func (s *Service) AdminPaymentProviders(actor *model.User) ([]AdminPaymentProvid
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
manifest, _ := paymentManifestForProvider(descriptor.ID)
|
||||
manifest, _ := s.paymentManifestForProvider(descriptor.ID)
|
||||
view := AdminPaymentProviderView{PaymentProviderView: base, Values: map[string]string{}, SecretConfigured: map[string]bool{}, ConfigFields: manifest.Configuration.Fields}
|
||||
if config != nil {
|
||||
values, err := s.decryptPaymentConfig(config)
|
||||
@@ -220,11 +220,14 @@ func (s *Service) UpdatePaymentProviderConfig(actor *model.User, providerID stri
|
||||
return nil, BadAuthRequest("未知支付渠道")
|
||||
}
|
||||
descriptor := provider.Descriptor()
|
||||
manifest, ok := paymentManifestForProvider(descriptor.ID)
|
||||
manifest, ok := s.paymentManifestForProvider(descriptor.ID)
|
||||
if !ok {
|
||||
return nil, BadAuthRequest("支付插件清单不存在")
|
||||
}
|
||||
policy, ok := paymentExpiryPolicy(manifest, descriptor.ID)
|
||||
if !ok {
|
||||
return nil, BadAuthRequest("支付插件清单不存在")
|
||||
}
|
||||
policy := manifest.Contributes.PaymentProviders[0].ExpiryPolicy
|
||||
if request.CloseAfterMinutes < policy.MinMinutes || request.CloseAfterMinutes > policy.MaxMinutes {
|
||||
return nil, BadAuthRequest(fmt.Sprintf("未支付关闭时间必须为 %d-%d 分钟", policy.MinMinutes, policy.MaxMinutes))
|
||||
}
|
||||
@@ -318,8 +321,10 @@ func (s *Service) UpdatePaymentProviderConfig(actor *model.User, providerID stri
|
||||
|
||||
func (s *Service) paymentProviderView(descriptor payment.Descriptor) (PaymentProviderView, *model.PaymentProviderConfig, error) {
|
||||
view := PaymentProviderView{ID: descriptor.ID, PluginID: descriptor.PluginID, Name: descriptor.Name, Icon: descriptor.Icon, CheckoutMode: descriptor.CheckoutMode}
|
||||
if manifest, ok := paymentManifestForProvider(descriptor.ID); ok && len(manifest.Contributes.PaymentProviders) > 0 {
|
||||
view.CloseAfterMinutes = manifest.Contributes.PaymentProviders[0].ExpiryPolicy.DefaultMinutes
|
||||
if manifest, ok := s.paymentManifestForProvider(descriptor.ID); ok {
|
||||
if policy, found := paymentExpiryPolicy(manifest, descriptor.ID); found {
|
||||
view.CloseAfterMinutes = policy.DefaultMinutes
|
||||
}
|
||||
}
|
||||
state, err := s.pluginStateForUser(nil, descriptor.PluginID, s.Plugins())
|
||||
if err == nil {
|
||||
@@ -339,7 +344,24 @@ func (s *Service) paymentProviderView(descriptor payment.Descriptor) (PaymentPro
|
||||
return view, config, nil
|
||||
}
|
||||
|
||||
func paymentManifestForProvider(providerID string) (protocol.Manifest, bool) {
|
||||
func (s *Service) paymentManifestForProvider(providerID string) (protocol.Manifest, bool) {
|
||||
providerID = strings.TrimSpace(providerID)
|
||||
if providerID == "" {
|
||||
return protocol.Manifest{}, false
|
||||
}
|
||||
if s != nil {
|
||||
for _, plugin := range s.Plugins() {
|
||||
for _, contribution := range plugin.Manifest.Contributes.PaymentProviders {
|
||||
if contribution.ID == providerID {
|
||||
return protocolManifestFromPluginView(plugin), true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bundledPaymentManifestForProvider(providerID)
|
||||
}
|
||||
|
||||
func bundledPaymentManifestForProvider(providerID string) (protocol.Manifest, bool) {
|
||||
for _, manifest := range bundledPaymentPluginManifests() {
|
||||
for _, contribution := range manifest.Contributes.PaymentProviders {
|
||||
if contribution.ID == providerID {
|
||||
@@ -350,6 +372,34 @@ func paymentManifestForProvider(providerID string) (protocol.Manifest, bool) {
|
||||
return protocol.Manifest{}, false
|
||||
}
|
||||
|
||||
func protocolManifestFromPluginView(plugin PluginView) protocol.Manifest {
|
||||
return protocol.Manifest{
|
||||
APIVersion: plugin.Manifest.APIVersion,
|
||||
Metadata: protocol.Metadata{
|
||||
ID: plugin.Manifest.ID, Version: plugin.Manifest.Version, Name: plugin.Manifest.Name,
|
||||
Vendor: plugin.Manifest.Author, Description: plugin.Manifest.Description,
|
||||
Documentation: plugin.Manifest.Documentation,
|
||||
},
|
||||
Surfaces: plugin.Manifest.Surfaces,
|
||||
Runtime: plugin.Manifest.Runtime,
|
||||
Permissions: plugin.Manifest.Permissions,
|
||||
Configuration: plugin.Manifest.Configuration,
|
||||
Contributes: plugin.Manifest.Contributes,
|
||||
}
|
||||
}
|
||||
|
||||
func paymentExpiryPolicy(manifest protocol.Manifest, providerID string) (protocol.ManifestPaymentExpiryPolicy, bool) {
|
||||
for _, contribution := range manifest.Contributes.PaymentProviders {
|
||||
if contribution.ID == providerID {
|
||||
return contribution.ExpiryPolicy, true
|
||||
}
|
||||
}
|
||||
if len(manifest.Contributes.PaymentProviders) == 1 {
|
||||
return manifest.Contributes.PaymentProviders[0].ExpiryPolicy, true
|
||||
}
|
||||
return protocol.ManifestPaymentExpiryPolicy{}, false
|
||||
}
|
||||
|
||||
func (s *Service) decryptPaymentConfig(config *model.PaymentProviderConfig) (payment.Config, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("支付渠道配置不存在")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/payment"
|
||||
@@ -59,7 +60,7 @@ func TestSystemPaymentPluginsRegisterRPCProviders(t *testing.T) {
|
||||
if registry == nil {
|
||||
t.Fatal("payment registry is nil")
|
||||
}
|
||||
for _, providerID := range []string{PaymentProviderAlipay, PaymentProviderWeChat} {
|
||||
for _, providerID := range []string{PaymentProviderAlipay, PaymentProviderWeChat, "xunhupay-aggregate"} {
|
||||
provider, ok := registry.Get(providerID)
|
||||
if !ok {
|
||||
t.Fatalf("payment provider %q is missing", providerID)
|
||||
@@ -70,6 +71,14 @@ func TestSystemPaymentPluginsRegisterRPCProviders(t *testing.T) {
|
||||
if provider.Descriptor().PluginID == "" || provider.Descriptor().PluginVersion == "" {
|
||||
t.Fatalf("payment provider %q is missing plugin identity: %#v", providerID, provider.Descriptor())
|
||||
}
|
||||
err := provider.ValidateConfig(nil)
|
||||
var providerErr *payment.ProviderError
|
||||
if !errors.As(err, &providerErr) {
|
||||
t.Fatalf("provider %q ValidateConfig() = %T %v, want ProviderError", providerID, err, err)
|
||||
}
|
||||
if providerErr.Code == "plugin_executable_missing" || providerErr.Code == "plugin_exec_format_error" || providerErr.Code == "plugin_start_failed" {
|
||||
t.Fatalf("provider %q failed to start: %#v cause=%v", providerID, providerErr, providerErr.Cause)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +92,7 @@ func TestPaymentRegistryFailsClosedWhenOfficialPackagesAreMissing(t *testing.T)
|
||||
if registry == nil {
|
||||
t.Fatal("payment registry is nil")
|
||||
}
|
||||
for _, providerID := range []string{PaymentProviderAlipay, PaymentProviderWeChat} {
|
||||
for _, providerID := range []string{PaymentProviderAlipay, PaymentProviderWeChat, "xunhupay-aggregate"} {
|
||||
if _, ok := registry.Get(providerID); ok {
|
||||
t.Fatalf("payment provider %q unexpectedly came from host fallback", providerID)
|
||||
}
|
||||
@@ -107,3 +116,38 @@ func TestTopupProductCreditAmountStaysWithinSafeLimit(t *testing.T) {
|
||||
t.Fatalf("maximum safe top-up credits: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXunHuPayOfficialPackageIsPaymentPlugin(t *testing.T) {
|
||||
center, err := newPluginRuntime(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var plugin PluginView
|
||||
for _, item := range center.list() {
|
||||
if item.Manifest.ID == "official-payment-xunhupay" {
|
||||
plugin = item
|
||||
plugin.Management = pluginManagementFromView(item)
|
||||
break
|
||||
}
|
||||
}
|
||||
if plugin.Manifest.ID == "" {
|
||||
t.Fatal("official-payment-xunhupay is missing")
|
||||
}
|
||||
if plugin.Management.Kind != PluginKindPayment || plugin.Source != PluginOriginOfficial {
|
||||
t.Fatalf("xunhupay plugin = %#v", plugin)
|
||||
}
|
||||
if len(plugin.Manifest.Contributes.PaymentProviders) != 1 || plugin.Manifest.Contributes.PaymentProviders[0].ID != "xunhupay-aggregate" {
|
||||
t.Fatalf("xunhupay contributions = %#v", plugin.Manifest.Contributes.PaymentProviders)
|
||||
}
|
||||
registry := center.paymentRegistrySnapshot()
|
||||
if registry == nil {
|
||||
t.Fatal("payment registry is nil")
|
||||
}
|
||||
provider, ok := registry.Get("xunhupay-aggregate")
|
||||
if !ok {
|
||||
t.Fatal("xunhupay-aggregate provider is missing")
|
||||
}
|
||||
if _, ok := provider.(*payment.RPCProvider); !ok {
|
||||
t.Fatalf("xunhupay provider type = %T", provider)
|
||||
}
|
||||
}
|
||||
@@ -139,12 +139,47 @@ func TestPaymentRuntimeRejectsInvalidDigest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaymentRuntimeReadyAcceptsTaggedBackend(t *testing.T) {
|
||||
packageData := paymentRuntimePackageFiles(t, map[string][]byte{"backend/provider-darwin-arm64": []byte("darwin-provider")})
|
||||
pkg, err := protocol.ParsePluginPackage(packageData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
packageDir := t.TempDir()
|
||||
runtimeDir, err := materializePaymentBackend(packageDir, pluginHash(packageData), pkg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(runtimeDir, "backend", "provider")); !os.IsNotExist(err) {
|
||||
t.Fatalf("canonical provider should be absent: %v", err)
|
||||
}
|
||||
actual, err := os.ReadFile(filepath.Join(runtimeDir, "backend", "provider-darwin-arm64"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(actual, []byte("darwin-provider")) {
|
||||
t.Fatalf("tagged runtime provider = %q", actual)
|
||||
}
|
||||
if !paymentRuntimeReady(runtimeDir, pluginHash(packageData), "backend/provider") {
|
||||
t.Fatal("tagged payment runtime was not marked ready")
|
||||
}
|
||||
}
|
||||
|
||||
func paymentRuntimePackage(t *testing.T, provider []byte) []byte {
|
||||
t.Helper()
|
||||
return paymentRuntimePackageFiles(t, map[string][]byte{"backend/provider": provider})
|
||||
}
|
||||
|
||||
func paymentRuntimePackageFiles(t *testing.T, files map[string][]byte) []byte {
|
||||
t.Helper()
|
||||
manifest := []byte(`{"apiVersion":"yingce.plugin/v1","id":"test-payment-runtime","version":"1.0.0","name":"Test Payment Runtime","author":"Test","enabled":true,"runtime":{"backend":"rpc","backendEntry":"backend/provider"},"contributes":{"paymentProviders":[{"id":"test-payment","label":"Test Payment","icon":"brand:test","checkoutMode":"redirect","expiryPolicy":{"defaultMinutes":30,"minMinutes":5,"maxMinutes":1440}}]}}`)
|
||||
payload := map[string][]byte{"manifest.json": manifest}
|
||||
for name, content := range files {
|
||||
payload[name] = content
|
||||
}
|
||||
var buffer bytes.Buffer
|
||||
writer := zip.NewWriter(&buffer)
|
||||
for name, content := range map[string][]byte{"manifest.json": manifest, "backend/provider": provider} {
|
||||
for name, content := range payload {
|
||||
file, err := writer.Create(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -118,6 +118,21 @@ func pluginManagement(pluginID string, source string) PluginManagementView {
|
||||
}
|
||||
}
|
||||
|
||||
func pluginManagementFromView(plugin PluginView) PluginManagementView {
|
||||
policy := pluginManagement(plugin.Manifest.ID, plugin.Source)
|
||||
if policy.Kind != PluginKindProtocol || len(plugin.Manifest.Contributes.PaymentProviders) == 0 {
|
||||
return policy
|
||||
}
|
||||
origin := strings.TrimSpace(plugin.Source)
|
||||
if origin == "" {
|
||||
origin = PluginOriginOfficial
|
||||
}
|
||||
return PluginManagementView{
|
||||
Origin: origin, Kind: PluginKindPayment,
|
||||
ActivationScope: PluginScopeSystem, ConfigurationScope: PluginConfigurationSystem,
|
||||
}
|
||||
}
|
||||
|
||||
func knownPluginIDs(items []PluginView) []string {
|
||||
seen := make(map[string]struct{}, len(items)+len(officialApplicationPolicies)+len(systemPaymentPolicies))
|
||||
ids := make([]string, 0, len(items)+len(officialApplicationPolicies)+len(systemPaymentPolicies))
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/protocol"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
@@ -70,6 +71,22 @@ func TestUploadedManifestCannotClaimUserActivationScope(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaymentContributionInfersPaymentManagementKind(t *testing.T) {
|
||||
plugin := PluginView{
|
||||
Source: PluginOriginOfficial,
|
||||
Manifest: PluginManifestView{
|
||||
ID: "official-payment-xunhupay",
|
||||
Contributes: protocol.ManifestContributions{PaymentProviders: []protocol.ManifestPaymentProvider{{
|
||||
ID: "xunhupay-aggregate", Label: "虎皮椒聚合支付", Icon: "assets/icon.svg", CheckoutMode: "qr_code",
|
||||
}}},
|
||||
},
|
||||
}
|
||||
policy := pluginManagementFromView(plugin)
|
||||
if policy.Kind != PluginKindPayment || policy.Origin != PluginOriginOfficial || policy.ActivationScope != PluginScopeSystem {
|
||||
t.Fatalf("inferred payment policy = %#v", policy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtCritiqueIsUserToggleableApplication(t *testing.T) {
|
||||
policy := pluginManagement(PluginAIArtCritique, "bundled")
|
||||
if policy.Origin != PluginOriginOfficial || policy.Kind != PluginKindApplication || policy.ActivationScope != PluginScopeUser || policy.ConfigurationScope != PluginConfigurationNone {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -91,6 +92,15 @@ type pluginRegistryRecord struct {
|
||||
}
|
||||
|
||||
func newPluginRuntime(dataDir string) (*pluginRuntime, error) {
|
||||
dataDir = strings.TrimSpace(dataDir)
|
||||
if dataDir == "" {
|
||||
return nil, errors.New("plugin data directory is empty")
|
||||
}
|
||||
var err error
|
||||
dataDir, err = filepath.Abs(dataDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin data directory is invalid: %w", err)
|
||||
}
|
||||
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("create plugin registry directory: %w", err)
|
||||
}
|
||||
@@ -519,8 +529,24 @@ func paymentRuntimeReady(root, digest, backendEntry string) bool {
|
||||
if err != nil || strings.TrimSpace(string(ready)) != digest {
|
||||
return false
|
||||
}
|
||||
entry, err := os.Stat(filepath.Join(root, filepath.FromSlash(backendEntry)))
|
||||
return err == nil && !entry.IsDir()
|
||||
return protocol.HasAnyPaymentRPCBackend(backendEntry, paymentRuntimeBackendFiles(root))
|
||||
}
|
||||
|
||||
func paymentRuntimeBackendFiles(root string) map[string][]byte {
|
||||
files := map[string][]byte{}
|
||||
backendRoot := filepath.Join(root, "backend")
|
||||
_ = filepath.WalkDir(backendRoot, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil || entry.IsDir() {
|
||||
return err
|
||||
}
|
||||
rel, relErr := filepath.Rel(root, path)
|
||||
if relErr != nil {
|
||||
return nil
|
||||
}
|
||||
files[filepath.ToSlash(rel)] = []byte{1}
|
||||
return nil
|
||||
})
|
||||
return files
|
||||
}
|
||||
|
||||
func (c *pluginRuntime) list() []PluginView {
|
||||
|
||||
@@ -137,7 +137,7 @@ func (s *Service) Plugins() []PluginView {
|
||||
}
|
||||
items := s.pluginRuntime.list()
|
||||
for index := range items {
|
||||
items[index].Management = pluginManagement(items[index].Manifest.ID, items[index].Source)
|
||||
items[index].Management = pluginManagementFromView(items[index])
|
||||
}
|
||||
return items
|
||||
}
|
||||
@@ -202,7 +202,7 @@ func (s *Service) InstallPluginForAdmin(actor *model.User, data []byte, fileName
|
||||
if err != nil {
|
||||
return PluginView{}, err
|
||||
}
|
||||
plugin.Management = pluginManagement(plugin.Manifest.ID, plugin.Source)
|
||||
plugin.Management = pluginManagementFromView(plugin)
|
||||
now := time.Now()
|
||||
state := &model.PluginPlatformState{PluginID: plugin.Manifest.ID, Available: plugin.Status == "enabled", UpdatedBy: actor.ID, CreatedAt: now, UpdatedAt: now}
|
||||
if err := s.repo.SavePluginPlatformState(state); err != nil {
|
||||
|
||||
Reference in new issue
Block a user