fusenapi/utils/pay/stripe.go

115 lines
3.3 KiB
Go
Raw Normal View History

2023-07-26 03:06:05 +00:00
package pay
import (
2023-09-25 09:31:42 +00:00
"github.com/stripe/stripe-go/v75"
"github.com/stripe/stripe-go/v75/checkout/session"
"github.com/stripe/stripe-go/v75/paymentintent"
"github.com/stripe/stripe-go/v75/refund"
2023-07-28 11:03:36 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-07-26 03:06:05 +00:00
)
type Stripe struct {
2023-09-11 05:59:16 +00:00
Key string `json:"key"`
PayType string `json:"pay_type"`
2023-07-26 03:06:05 +00:00
}
2023-07-28 11:03:36 +00:00
// 生成退款
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
}
2023-07-26 03:06:05 +00:00
// 生成预付款
func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) {
var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams
2023-07-28 03:15:42 +00:00
// productData.Metadata = map[string]string{"order_id": "33333333333333"}
2023-07-26 03:06:05 +00:00
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
2023-09-11 05:59:16 +00:00
var resultPayment resultPayment
switch stripePay.PayType {
case "session":
// session 方式
params := &stripe.CheckoutSessionParams{
2023-09-22 08:27:17 +00:00
PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{Metadata: req.Metadata},
2023-09-11 05:59:16 +00:00
// 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),
2023-07-26 03:06:05 +00:00
},
},
2023-09-11 05:59:16 +00:00
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)),
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
// "ideal",
}),
}
2023-09-22 08:27:17 +00:00
for key, item := range req.Metadata {
params.AddMetadata(key, item)
}
2023-09-11 05:59:16 +00:00
resPaymentintent, err := paymentintent.New(params)
if err != nil {
return nil, err
}
resultPayment.TradeNo = resPaymentintent.ID
resultPayment.ClientSecret = resPaymentintent.ClientSecret
default:
2023-07-26 03:06:05 +00:00
}
return &GeneratePrepaymentRes{
2023-09-11 05:59:16 +00:00
URL: resultPayment.Url,
SessionId: resultPayment.SessionId,
TradeNo: resultPayment.TradeNo,
ClientSecret: resultPayment.ClientSecret,
2023-07-26 03:06:05 +00:00
}, err
}
2023-09-11 05:59:16 +00:00
type resultPayment struct {
Url string
SessionId string
TradeNo string
ClientSecret string
}