fusenapi/utils/pay/stripe.go
2023-09-22 10:39:02 +08:00

113 lines
3.3 KiB
Go

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"`
PayType string `json:"pay_type"`
}
// 生成退款
func (stripePay *Stripe) PayRefund(req *PayRefundReq) (res *PayRefundRes, err error) {
stripe.Key = stripePay.Key
params := &stripe.RefundParams{PaymentIntent: stripe.String(req.TradeNo)}
_, err = refund.New(params)
if err != nil {
logx.Error(err)
}
return res, err
}
// 生成预付款
func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) {
var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams
// productData.Metadata = map[string]string{"order_id": "33333333333333"}
if req.ProductName != "" {
productData.Name = stripe.String(req.ProductName)
}
if req.ProductDescription != "" {
productData.Description = stripe.String(req.ProductDescription)
}
if len(req.ProductImages) > 0 {
productData.Images = req.ProductImages
}
// var images = make([]*string, 1)
// var image string = "https://img2.woyaogexing.com/2023/07/25/133132a32b9f79cfad84965a4bea57e0.jpg"
// images[0] = stripe.String(image)
// productData.Images = images
stripe.Key = stripePay.Key
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),
},
},
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{
Params: stripe.Params{Metadata: map[string]string{"order_sn": req.OrderSn}},
Amount: stripe.Int64(req.Amount),
Currency: stripe.String(string(req.Currency)),
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: resultPayment.Url,
SessionId: resultPayment.SessionId,
TradeNo: resultPayment.TradeNo,
ClientSecret: resultPayment.ClientSecret,
}, err
}
type resultPayment struct {
Url string
SessionId string
TradeNo string
ClientSecret string
}