fusenapi/utils/pay/stripe.go

118 lines
3.4 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
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-11-23 08:22:12 +00:00
// PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{Metadata: req.Metadata},
2023-09-11 05:59:16 +00:00
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
// "ideal",
}),
2023-11-23 08:22:12 +00:00
Metadata: req.Metadata,
2023-09-11 05:59:16 +00:00
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),
}
2023-11-23 08:22:12 +00:00
// for key, item := range req.Metadata {
// params.AddMetadata(key, item)
// }
2023-09-11 05:59:16 +00:00
resCheckoutSession, err := session.New(params)
if err != nil {
return nil, err
}
resultPayment.Url = resCheckoutSession.URL
resultPayment.SessionId = resCheckoutSession.ID
case "intent":
// 密钥方式
params := &stripe.PaymentIntentParams{
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
// "ideal",
}),
2023-11-23 08:22:12 +00:00
Metadata: req.Metadata,
Amount: stripe.Int64(req.Amount),
Currency: stripe.String(string(req.Currency)),
2023-09-11 05:59:16 +00:00
}
2023-11-23 08:22:12 +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 {
2023-11-02 07:58:13 +00:00
Url string `json:"url"` // 生成预付款链接
SessionId string `json:"session_id"`
TradeNo string `json:"trade_no"`
ClientSecret string `json:"client_secret"`
2023-09-11 05:59:16 +00:00
}