From 1a6822da645b58e960dfea2bd583d7203f712b47 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Mon, 11 Sep 2023 13:59:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=B0=83=E6=95=B4=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/pay/stripe.go | 103 +++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 40 deletions(-) diff --git a/utils/pay/stripe.go b/utils/pay/stripe.go index e2e13799..2b9ea9cc 100644 --- a/utils/pay/stripe.go +++ b/utils/pay/stripe.go @@ -3,12 +3,14 @@ package pay import ( "github.com/stripe/stripe-go/v74" "github.com/stripe/stripe-go/v74/checkout/session" + "github.com/stripe/stripe-go/v74/paymentintent" "github.com/stripe/stripe-go/v74/refund" "github.com/zeromicro/go-zero/core/logx" ) type Stripe struct { - Key string `json:"key"` + Key string `json:"key"` + PayType string `json:"pay_type"` } // 生成退款 @@ -43,49 +45,70 @@ func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *Ge // images[0] = stripe.String(image) // productData.Images = images stripe.Key = stripePay.Key - - // session 方式 - params := &stripe.CheckoutSessionParams{ - PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{Metadata: map[string]string{"order_sn": req.OrderSn}}, - // Params: stripe.Params{Metadata: map[string]string{"order_id": "1111111111111"}}, - PaymentMethodTypes: stripe.StringSlice([]string{ - "card", - // "ideal", - }), - LineItems: []*stripe.CheckoutSessionLineItemParams{ - { - PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{ - Currency: stripe.String(req.Currency), - ProductData: &productData, - UnitAmount: stripe.Int64(req.Amount), + var resultPayment resultPayment + switch stripePay.PayType { + case "session": + // session 方式 + params := &stripe.CheckoutSessionParams{ + PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{Metadata: map[string]string{"order_sn": req.OrderSn}}, + // Params: stripe.Params{Metadata: map[string]string{"order_id": "1111111111111"}}, + PaymentMethodTypes: stripe.StringSlice([]string{ + "card", + // "ideal", + }), + LineItems: []*stripe.CheckoutSessionLineItemParams{ + { + PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{ + Currency: stripe.String(req.Currency), + ProductData: &productData, + UnitAmount: stripe.Int64(req.Amount), + }, + Quantity: stripe.Int64(req.Quantity), }, - Quantity: stripe.Int64(req.Quantity), }, - }, - Mode: stripe.String(string(stripe.CheckoutSessionModePayment)), - SuccessURL: stripe.String(req.SuccessURL), - CancelURL: stripe.String(req.CancelURL), - } - result, err := session.New(params) - - // 密钥方式 - // params := &stripe.PaymentIntentParams{ - // Amount: stripe.Int64(req.Amount), - // Currency: stripe.String(string(req.Currency)), - // AutomaticPaymentMethods: &stripe.PaymentIntentAutomaticPaymentMethodsParams{ - // Enabled: stripe.Bool(true), - // }, - // } - // result, err := paymentintent.New(params) - - if err != nil { - return nil, err + Mode: stripe.String(string(stripe.CheckoutSessionModePayment)), + SuccessURL: stripe.String(req.SuccessURL), + CancelURL: stripe.String(req.CancelURL), + } + resCheckoutSession, err := session.New(params) + if err != nil { + return nil, err + } + resultPayment.Url = resCheckoutSession.URL + resultPayment.SessionId = resCheckoutSession.ID + case "intent": + // 密钥方式 + params := &stripe.PaymentIntentParams{ + Amount: stripe.Int64(req.Amount), + Currency: stripe.String(string(req.Currency)), + AutomaticPaymentMethods: &stripe.PaymentIntentAutomaticPaymentMethodsParams{ + Enabled: stripe.Bool(true), + }, + PaymentMethodTypes: stripe.StringSlice([]string{ + "card", + // "ideal", + }), + } + resPaymentintent, err := paymentintent.New(params) + if err != nil { + return nil, err + } + resultPayment.TradeNo = resPaymentintent.ID + resultPayment.ClientSecret = resPaymentintent.ClientSecret + default: } return &GeneratePrepaymentRes{ - URL: result.URL, - //TradeNo: result.ID, - SessionId: result.ID, - // ClientSecret: result.ClientSecret, + URL: resultPayment.Url, + SessionId: resultPayment.SessionId, + TradeNo: resultPayment.TradeNo, + ClientSecret: resultPayment.ClientSecret, }, err } + +type resultPayment struct { + Url string + SessionId string + TradeNo string + ClientSecret string +}