You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/payment/score.go

46 lines
1023 B

package payment
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
)
// ScorePayment 积分支付处理
type ScorePayment struct {
}
// Create 创建新订单
func (pay *ScorePayment) Create(order *model.Order, pack *serializer.PackProduct, group *serializer.GroupProducts, user *model.User) (*OrderCreateRes, error) {
if pack != nil {
order.Price = pack.Score
} else {
order.Price = group.Score
}
// 检查此订单是否可用积分支付
if order.Price == 0 {
return nil, ErrUnsupportedPaymentMethod
}
// 扣除用户积分
if !user.PayScore(order.Price * order.Num) {
return nil, ErrScoreNotEnough
}
// 商品“发货”
if err := GiveProduct(user, pack, group, order.Num); err != nil {
user.AddScore(order.Price * order.Num)
return nil, err
}
// 创建订单记录
order.Status = model.OrderPaid
if _, err := order.Create(); err != nil {
return nil, ErrInsertOrder.WithError(err)
}
return &OrderCreateRes{
Payment: false,
}, nil
}