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 +}