fusenapi/utils/pay/stripe.go
2023-07-28 19:03:36 +08:00

92 lines
2.6 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/refund"
"github.com/zeromicro/go-zero/core/logx"
)
type Stripe struct {
Key string `json:"key"`
}
// 生成退款
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
// 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),
}
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
}
return &GeneratePrepaymentRes{
URL: result.URL,
//TradeNo: result.ID,
SessionId: result.ID,
// ClientSecret: result.ClientSecret,
}, err
}