From 94076def9c4c3efa24d943e358b0be0f901fe709 Mon Sep 17 00:00:00 2001 From: mehunk Date: Sun, 25 Jan 2026 18:23:51 +0900 Subject: [PATCH] feat: Support customizing the success and cancel url of Stripe. --- controller/topup_stripe.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/controller/topup_stripe.go b/controller/topup_stripe.go index 337ff8e73..11e9f81c1 100644 --- a/controller/topup_stripe.go +++ b/controller/topup_stripe.go @@ -31,6 +31,8 @@ var stripeAdaptor = &StripeAdaptor{} type StripePayRequest struct { Amount int64 `json:"amount"` PaymentMethod string `json:"payment_method"` + SuccessURL string `json:"success_url,omitempty"` + CancelURL string `json:"cancel_url,omitempty"` } type StripeAdaptor struct { @@ -76,7 +78,7 @@ func (*StripeAdaptor) RequestPay(c *gin.Context, req *StripePayRequest) { reference := fmt.Sprintf("new-api-ref-%d-%d-%s", user.Id, time.Now().UnixMilli(), randstr.String(4)) referenceId := "ref_" + common.Sha1([]byte(reference)) - payLink, err := genStripeLink(referenceId, user.StripeCustomer, user.Email, req.Amount) + payLink, err := genStripeLink(referenceId, user.StripeCustomer, user.Email, req.Amount, req.SuccessURL, req.CancelURL) if err != nil { log.Println("获取Stripe Checkout支付链接失败", err) c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"}) @@ -210,17 +212,25 @@ func sessionExpired(event stripe.Event) { log.Println("充值订单已过期", referenceId) } -func genStripeLink(referenceId string, customerId string, email string, amount int64) (string, error) { +func genStripeLink(referenceId string, customerId string, email string, amount int64, successURL string, cancelURL string) (string, error) { if !strings.HasPrefix(setting.StripeApiSecret, "sk_") && !strings.HasPrefix(setting.StripeApiSecret, "rk_") { return "", fmt.Errorf("无效的Stripe API密钥") } stripe.Key = setting.StripeApiSecret + // Use custom URLs if provided, otherwise use defaults + if successURL == "" { + successURL = system_setting.ServerAddress + "/console/log" + } + if cancelURL == "" { + cancelURL = system_setting.ServerAddress + "/console/topup" + } + params := &stripe.CheckoutSessionParams{ ClientReferenceID: stripe.String(referenceId), - SuccessURL: stripe.String(system_setting.ServerAddress + "/console/log"), - CancelURL: stripe.String(system_setting.ServerAddress + "/console/topup"), + SuccessURL: stripe.String(successURL), + CancelURL: stripe.String(cancelURL), LineItems: []*stripe.CheckoutSessionLineItemParams{ { Price: stripe.String(setting.StripePriceId),