Merge pull request #2449 from wucm667/fix/payment-product-name-affix
fix: 修复订阅支付商品名前后缀不生效
This commit is contained in:
commit
ac856633aa
@ -499,24 +499,41 @@ func selectedInstanceSupportedTypes(sel *payment.InstanceSelection) string {
|
|||||||
|
|
||||||
func (s *PaymentService) buildPaymentSubject(plan *dbent.SubscriptionPlan, limitAmount float64, cfg *PaymentConfig, sel *payment.InstanceSelection) string {
|
func (s *PaymentService) buildPaymentSubject(plan *dbent.SubscriptionPlan, limitAmount float64, cfg *PaymentConfig, sel *payment.InstanceSelection) string {
|
||||||
if plan != nil {
|
if plan != nil {
|
||||||
if plan.ProductName != "" {
|
productName := plan.ProductName
|
||||||
return plan.ProductName
|
if productName == "" {
|
||||||
|
productName = "Sub2API Subscription " + plan.Name
|
||||||
}
|
}
|
||||||
return "Sub2API Subscription " + plan.Name
|
return applyPaymentProductNameAffix(productName, cfg)
|
||||||
}
|
}
|
||||||
currency := payment.DefaultPaymentCurrency
|
currency := payment.DefaultPaymentCurrency
|
||||||
if sel != nil {
|
if sel != nil {
|
||||||
currency = paymentProviderConfigCurrency(sel.ProviderKey, sel.Config)
|
currency = paymentProviderConfigCurrency(sel.ProviderKey, sel.Config)
|
||||||
}
|
}
|
||||||
amountStr := payment.FormatAmountForCurrency(limitAmount, currency)
|
amountStr := payment.FormatAmountForCurrency(limitAmount, currency)
|
||||||
pf := strings.TrimSpace(cfg.ProductNamePrefix)
|
if hasPaymentProductNameAffix(cfg) {
|
||||||
sf := strings.TrimSpace(cfg.ProductNameSuffix)
|
return applyPaymentProductNameAffix(amountStr, cfg)
|
||||||
if pf != "" || sf != "" {
|
|
||||||
return strings.TrimSpace(pf + " " + amountStr + " " + sf)
|
|
||||||
}
|
}
|
||||||
return "Sub2API " + amountStr + " " + currency
|
return "Sub2API " + amountStr + " " + currency
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasPaymentProductNameAffix(cfg *PaymentConfig) bool {
|
||||||
|
if cfg == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
pf := strings.TrimSpace(cfg.ProductNamePrefix)
|
||||||
|
sf := strings.TrimSpace(cfg.ProductNameSuffix)
|
||||||
|
return pf != "" || sf != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyPaymentProductNameAffix(productName string, cfg *PaymentConfig) string {
|
||||||
|
if !hasPaymentProductNameAffix(cfg) {
|
||||||
|
return productName
|
||||||
|
}
|
||||||
|
pf := strings.TrimSpace(cfg.ProductNamePrefix)
|
||||||
|
sf := strings.TrimSpace(cfg.ProductNameSuffix)
|
||||||
|
return strings.TrimSpace(pf + " " + productName + " " + sf)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *PaymentService) maybeBuildWeChatOAuthRequiredResponse(ctx context.Context, req CreateOrderRequest, amount, payAmount, feeRate float64) (*CreateOrderResponse, error) {
|
func (s *PaymentService) maybeBuildWeChatOAuthRequiredResponse(ctx context.Context, req CreateOrderRequest, amount, payAmount, feeRate float64) (*CreateOrderResponse, error) {
|
||||||
return s.maybeBuildWeChatOAuthRequiredResponseForSelection(ctx, req, amount, payAmount, feeRate, nil)
|
return s.maybeBuildWeChatOAuthRequiredResponseForSelection(ctx, req, amount, payAmount, feeRate, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -138,6 +138,41 @@ func TestCalculateCreateOrderPayAmountRejectsFractionalZeroDecimal(t *testing.T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildPaymentSubjectAppliesAffixToSubscriptionPlanProductName(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
svc := &PaymentService{}
|
||||||
|
cfg := &PaymentConfig{
|
||||||
|
ProductNamePrefix: "PRE",
|
||||||
|
ProductNameSuffix: "SUF",
|
||||||
|
}
|
||||||
|
plan := &dbent.SubscriptionPlan{
|
||||||
|
Name: "Pro Monthly",
|
||||||
|
ProductName: "Claude Pro",
|
||||||
|
}
|
||||||
|
|
||||||
|
got := svc.buildPaymentSubject(plan, 0, cfg, nil)
|
||||||
|
if got != "PRE Claude Pro SUF" {
|
||||||
|
t.Fatalf("buildPaymentSubject() = %q, want %q", got, "PRE Claude Pro SUF")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildPaymentSubjectAppliesAffixToSubscriptionPlanDefaultName(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
svc := &PaymentService{}
|
||||||
|
cfg := &PaymentConfig{
|
||||||
|
ProductNamePrefix: "PRE",
|
||||||
|
ProductNameSuffix: "SUF",
|
||||||
|
}
|
||||||
|
plan := &dbent.SubscriptionPlan{Name: "Team Monthly"}
|
||||||
|
|
||||||
|
got := svc.buildPaymentSubject(plan, 0, cfg, nil)
|
||||||
|
if got != "PRE Sub2API Subscription Team Monthly SUF" {
|
||||||
|
t.Fatalf("buildPaymentSubject() = %q, want %q", got, "PRE Sub2API Subscription Team Monthly SUF")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaybeBuildWeChatOAuthRequiredResponse(t *testing.T) {
|
func TestMaybeBuildWeChatOAuthRequiredResponse(t *testing.T) {
|
||||||
t.Setenv("PAYMENT_RESUME_SIGNING_KEY", "0123456789abcdef0123456789abcdef")
|
t.Setenv("PAYMENT_RESUME_SIGNING_KEY", "0123456789abcdef0123456789abcdef")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user