Feat: vas alipay
This commit is contained in:
@@ -4,6 +4,9 @@ import (
|
||||
"fmt"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -23,6 +26,12 @@ var (
|
||||
ErrGroupInvalid = serializer.NewError(serializer.CodeNoPermissionErr, "用户组不可用", nil)
|
||||
// ErrUpgradeGroup 用户组冲突
|
||||
ErrUpgradeGroup = serializer.NewError(serializer.CodeDBError, "无法升级用户组", nil)
|
||||
// ErrUInitPayment 无法初始化支付实例
|
||||
ErrUInitPayment = serializer.NewError(serializer.CodeInternalSetting, "无法初始化支付实例", nil)
|
||||
// ErrIssueOrder 订单接口请求失败
|
||||
ErrIssueOrder = serializer.NewError(serializer.CodeInternalSetting, "无法创建订单", nil)
|
||||
// ErrOrderNotFound 订单不存在
|
||||
ErrOrderNotFound = serializer.NewError(serializer.CodeNotFound, "订单不存在", nil)
|
||||
)
|
||||
|
||||
// Pay 支付处理接口
|
||||
@@ -32,16 +41,39 @@ type Pay interface {
|
||||
|
||||
// OrderCreateRes 订单创建结果
|
||||
type OrderCreateRes struct {
|
||||
Payment bool `json:"payment"` // 是否需要支付
|
||||
Payment bool `json:"payment"` // 是否需要支付
|
||||
ID string `json:"id,omitempty"` // 订单号
|
||||
QRCode string `json:"qr_code,omitempty"` // 支付二维码指向的地址
|
||||
Redirect string `json:"redirect,omitempty"` // 支付跳转连接,和二维码二选一,不需要的留空
|
||||
}
|
||||
|
||||
// NewPaymentInstance 获取新的支付实例
|
||||
func NewPaymentInstance(method string) (Pay, error) {
|
||||
if method == "score" {
|
||||
switch method {
|
||||
case "score":
|
||||
return &ScorePayment{}, nil
|
||||
}
|
||||
case "alipay":
|
||||
options := model.GetSettingByNames("alipay_enabled", "appid", "appkey", "shopid")
|
||||
if options["alipay_enabled"] != "1" {
|
||||
return nil, ErrUnknownPaymentMethod
|
||||
}
|
||||
|
||||
return nil, ErrUnknownPaymentMethod
|
||||
// 初始化支付宝客户端
|
||||
var client, err = alipay.New(options["appid"], options["appkey"], true)
|
||||
if err != nil {
|
||||
return nil, ErrUInitPayment.WithError(err)
|
||||
}
|
||||
|
||||
// 加载支付宝公钥
|
||||
err = client.LoadAliPayPublicKey(options["shopid"])
|
||||
if err != nil {
|
||||
return nil, ErrUInitPayment.WithError(err)
|
||||
}
|
||||
|
||||
return &Alipay{Client: client}, nil
|
||||
default:
|
||||
return nil, ErrUnknownPaymentMethod
|
||||
}
|
||||
}
|
||||
|
||||
// NewOrder 创建新订单
|
||||
@@ -78,6 +110,7 @@ func NewOrder(pack *serializer.PackProduct, group *serializer.GroupProducts, num
|
||||
// 创建订单记录
|
||||
order := &model.Order{
|
||||
UserID: user.ID,
|
||||
OrderNo: orderID(),
|
||||
Type: orderType,
|
||||
Method: method,
|
||||
ProductID: productID,
|
||||
@@ -89,3 +122,10 @@ func NewOrder(pack *serializer.PackProduct, group *serializer.GroupProducts, num
|
||||
|
||||
return pay.Create(order, pack, group, user)
|
||||
}
|
||||
|
||||
func orderID() string {
|
||||
return fmt.Sprintf("%s%d%d",
|
||||
time.Now().Format("20060102150405"),
|
||||
10000+rand.Intn(90000),
|
||||
time.Now().UnixNano())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user