Files
sub2api/backend/internal/payment/provider/stripe_test.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

71 lines
2.1 KiB
Go

//go:build unit
package provider
import (
"bytes"
"context"
"testing"
"github.com/Wei-Shaw/sub2api/internal/payment"
"github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v85"
)
type stripeRefundBackend struct {
params []*stripe.RefundCreateParams
}
func (b *stripeRefundBackend) Call(_ string, _ string, _ string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {
b.params = append(b.params, params.(*stripe.RefundCreateParams))
refund := v.(*stripe.Refund)
refund.ID = "re_123"
refund.Status = stripe.RefundStatusSucceeded
return nil
}
func (*stripeRefundBackend) CallStreaming(string, string, string, stripe.ParamsContainer, stripe.StreamingLastResponseSetter) error {
return nil
}
func (*stripeRefundBackend) CallRaw(string, string, string, []byte, *stripe.Params, stripe.LastResponseSetter) error {
return nil
}
func (*stripeRefundBackend) CallMultipart(string, string, string, string, *bytes.Buffer, *stripe.Params, stripe.LastResponseSetter) error {
return nil
}
func (*stripeRefundBackend) SetMaxNetworkRetries(int64) {}
func TestStripeRefundUsesStableAmountSpecificIdempotencyKey(t *testing.T) {
backend := &stripeRefundBackend{}
client := stripe.NewClient("sk_test", stripe.WithBackends(&stripe.Backends{API: backend}))
provider := &Stripe{
config: map[string]string{"currency": "CNY"},
initialized: true,
sc: client,
}
refund := func(amount string) {
_, err := provider.Refund(context.Background(), payment.RefundRequest{
TradeNo: "pi_123",
OrderID: "sub2_order_456",
Amount: amount,
})
require.NoError(t, err)
}
refund("12.34")
refund("12.34")
refund("12.35")
require.Len(t, backend.params, 3)
require.Equal(t, int64(1234), *backend.params[0].Amount)
require.Equal(t, "re-sub2_order_456-1234", *backend.params[0].IdempotencyKey)
require.Equal(t, backend.params[0].IdempotencyKey, backend.params[1].IdempotencyKey)
require.Equal(t, int64(1235), *backend.params[2].Amount)
require.Equal(t, "re-sub2_order_456-1235", *backend.params[2].IdempotencyKey)
require.NotEqual(t, *backend.params[0].IdempotencyKey, *backend.params[2].IdempotencyKey)
}