fusenapi/utils/pay/stripe.go
2023-07-26 11:06:05 +08:00

59 lines
1.5 KiB
Go

package pay
import (
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/checkout/session"
)
type Stripe struct {
Key string `json:"key"`
}
// 生成预付款
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
params := &stripe.CheckoutSessionParams{
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)
if err != nil {
return nil, err
}
return &GeneratePrepaymentRes{
URL: result.URL,
TradeNo: result.ID,
}, err
}