支付模块

This commit is contained in:
Hiven
2023-07-26 11:06:05 +08:00
parent 03627b93e9
commit 95baad761a
21 changed files with 789 additions and 1 deletions

40
utils/pay/pay.go Normal file
View File

@@ -0,0 +1,40 @@
package pay
import "fusenapi/constants"
type Config struct {
// stripe支付
Stripe Stripe
}
// NewPayDriver 实例化方法
func NewPayDriver(PayMethod int64, config *Config) Pay {
switch PayMethod {
case int64(constants.PAYMETHOD_STRIPE):
return &Stripe{Key: config.Stripe.Key}
default:
return &Stripe{Key: config.Stripe.Key}
}
}
// Pay 支付集成接口
type Pay interface {
GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error)
}
type GeneratePrepaymentReq struct {
Amount int64 `json:"amount"` // 支付金额
Currency string `json:"currency"` // 支付货币
ProductName string `json:"product_name"` // 商品名称
ProductDescription string `json:"product_description"` // 商品描述
ProductImages []*string `json:"product_imageso"` // 商品照片
Quantity int64 `json:"quantity"` //数量
SuccessURL string `json:"success_url"` // 支付成功回调
CancelURL string `json:"cancel_url"` // 支付取消回调
}
type GeneratePrepaymentRes struct {
URL string `json:"url"` // 支付重定向地址
TradeNo string `json:"trade_no"` //交易ID
}

58
utils/pay/stripe.go Normal file
View File

@@ -0,0 +1,58 @@
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
}