添加优惠券功能,实现价格计算
This commit is contained in:
@@ -205,7 +205,25 @@ func (data *CreateResourceData) GetName() string {
|
||||
|
||||
func (data *CreateResourceData) GetPrice() float64 {
|
||||
if data.price == 0 {
|
||||
data.price = 0.01
|
||||
var count int
|
||||
switch data.Type {
|
||||
case 1:
|
||||
count = int(data.DailyLimit)
|
||||
case 2:
|
||||
count = int(data.Quota)
|
||||
}
|
||||
|
||||
seconds := int(data.Live)
|
||||
if seconds == 180 {
|
||||
seconds = 150
|
||||
}
|
||||
|
||||
times := int(data.Expire)
|
||||
if data.Type == 2 {
|
||||
times = 1
|
||||
}
|
||||
|
||||
data.price = float64(count*seconds*times) / 30000
|
||||
}
|
||||
return data.price
|
||||
}
|
||||
@@ -229,7 +247,7 @@ func createResource(data *CreateResourceData, uid int32) (*m.Resource, error) {
|
||||
Type: data.Type,
|
||||
Live: data.Live,
|
||||
Quota: data.Quota,
|
||||
Expire: common.LocalDateTime(time.Now().Add(time.Duration(data.Expire) * time.Second)),
|
||||
Expire: common.LocalDateTime(time.Now().Add(time.Duration(data.Expire) * 24 * time.Hour)),
|
||||
DailyLimit: data.DailyLimit,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var Transaction = &transactionService{}
|
||||
@@ -26,10 +27,60 @@ type transactionService struct {
|
||||
|
||||
func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query, uid int32, data *TransactionPrepareData) (*TransactionPrepareResult, error) {
|
||||
var subject = data.Subject
|
||||
var amount = data.Amount
|
||||
var expire = data.ExpireAt
|
||||
var tType = data.Type
|
||||
var method = data.Method
|
||||
var amount = data.Amount
|
||||
if data.CouponCode != "" {
|
||||
coupon, err := q.Coupon.WithContext(ctx).
|
||||
Where(
|
||||
q.Coupon.Code.Eq(data.CouponCode),
|
||||
q.Coupon.Status.Eq(0),
|
||||
).
|
||||
Take()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("优惠券不存在或已失效")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !coupon.ExpireAt.IsZero() && coupon.ExpireAt.Before(time.Now()) {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("优惠券已过期")
|
||||
}
|
||||
|
||||
if data.Amount < coupon.MinAmount {
|
||||
return nil, errors.New("订单金额未达到使用优惠券的条件")
|
||||
}
|
||||
|
||||
switch {
|
||||
// 该优惠券不属于当前用户
|
||||
default:
|
||||
return nil, errors.New("优惠券不属于当前用户")
|
||||
|
||||
// 公开优惠券
|
||||
case coupon.UserID == 0:
|
||||
amount = amount - coupon.Amount
|
||||
|
||||
// 指定用户的优惠券
|
||||
case coupon.UserID == uid:
|
||||
amount = amount - coupon.Amount
|
||||
if coupon.ExpireAt.IsZero() {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成订单号
|
||||
tradeNo, err := ID.GenSerial(ctx)
|
||||
@@ -78,7 +129,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
|
||||
// 不支持的支付方式
|
||||
default:
|
||||
return nil, errors.New("不支持的支付方式")
|
||||
return nil, ErrTransactionNotSupported
|
||||
}
|
||||
|
||||
// 保存交易订单
|
||||
@@ -140,11 +191,11 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
return nil, err
|
||||
}
|
||||
if resp.Code != alipay.CodeSuccess {
|
||||
slog.Warn("支付宝交易取消失败", "code", resp.Code, "sub_code", resp.SubCode, "msg", resp.Msg)
|
||||
slog.Warn("支付宝交易查询失败", "code", resp.Code, "sub_code", resp.SubCode, "msg", resp.Msg)
|
||||
return nil, errors.New("交易查询失败")
|
||||
}
|
||||
if resp.TradeStatus != alipay.TradeStatusSuccess {
|
||||
return nil, errors.New("交易未完成")
|
||||
return nil, ErrTransactionNotPaid
|
||||
}
|
||||
|
||||
transId = resp.TradeNo
|
||||
@@ -167,7 +218,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
return nil, err
|
||||
}
|
||||
if *resp.TradeState != "SUCCESS" {
|
||||
return nil, errors.New("交易未完成")
|
||||
return nil, ErrTransactionNotPaid
|
||||
}
|
||||
|
||||
transId = *resp.TransactionId
|
||||
@@ -179,7 +230,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
|
||||
// 不支持的支付方式
|
||||
default:
|
||||
return nil, errors.New("不支持的支付方式")
|
||||
return nil, ErrTransactionNotSupported
|
||||
}
|
||||
|
||||
return &TransactionVerifyResult{
|
||||
@@ -289,11 +340,12 @@ const (
|
||||
)
|
||||
|
||||
type TransactionPrepareData struct {
|
||||
Subject string
|
||||
Amount float64
|
||||
ExpireAt time.Time
|
||||
Type TransactionType
|
||||
Method TransactionMethod
|
||||
Subject string
|
||||
Amount float64
|
||||
ExpireAt time.Time
|
||||
Type TransactionType
|
||||
Method TransactionMethod
|
||||
CouponCode string
|
||||
}
|
||||
|
||||
type TransactionPrepareResult struct {
|
||||
@@ -322,3 +374,8 @@ type TransactionCompleteData struct {
|
||||
type TransactionCompleteResult struct {
|
||||
Trade *m.Trade
|
||||
}
|
||||
|
||||
var (
|
||||
ErrTransactionNotPaid = common.NewErr("transaction", "交易未完成")
|
||||
ErrTransactionNotSupported = common.NewErr("transaction", "不支持的支付方式")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user