重构增加模型枚举值定义
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/u"
|
||||
"platform/web/core"
|
||||
bill2 "platform/web/domains/bill"
|
||||
coupon2 "platform/web/domains/coupon"
|
||||
trade2 "platform/web/domains/trade"
|
||||
g "platform/web/globals"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
@@ -43,7 +46,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
coupon, err := q.Coupon.WithContext(ctx).
|
||||
Where(
|
||||
q.Coupon.Code.Eq(data.CouponCode),
|
||||
q.Coupon.Status.Eq(0),
|
||||
q.Coupon.Status.Eq(int32(coupon2.StatusUnused)),
|
||||
).
|
||||
Take()
|
||||
if err != nil {
|
||||
@@ -57,7 +60,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
if !expireAt.IsZero() && expireAt.Before(time.Now()) {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 2)
|
||||
Update(q.Coupon.Status, coupon2.StatusExpired)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -83,7 +86,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
if time.Time(coupon.ExpireAt).IsZero() {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 1)
|
||||
Update(q.Coupon.Status, int32(coupon2.StatusUsed))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,7 +105,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
switch method {
|
||||
|
||||
// 调用支付宝支付接口
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradePagePay(alipay.TradePagePay{
|
||||
QRPayMode: "4",
|
||||
Trade: alipay.Trade{
|
||||
@@ -119,7 +122,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
payUrl = resp.String()
|
||||
|
||||
// 调用微信支付接口
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, _, err := g.WechatPay.Native.Prepay(ctx, native.PrepayRequest{
|
||||
Appid: &env.WechatPayAppId,
|
||||
Mchid: &env.WechatPayMchId,
|
||||
@@ -142,15 +145,12 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
}
|
||||
|
||||
// 保存交易订单
|
||||
var tradeType int
|
||||
var billType int
|
||||
var billType bill2.Type
|
||||
switch tType {
|
||||
case TransactionTypeRecharge:
|
||||
tradeType = 2
|
||||
billType = 3
|
||||
case TransactionTypePurchase:
|
||||
tradeType = 1
|
||||
billType = 1
|
||||
case trade2.TypeRecharge:
|
||||
billType = bill2.TypeRecharge
|
||||
case trade2.TypePurchase:
|
||||
billType = bill2.TypeConsume
|
||||
}
|
||||
|
||||
var trade = m.Trade{
|
||||
@@ -158,7 +158,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
InnerNo: tradeNo,
|
||||
Subject: subject,
|
||||
Method: int32(method),
|
||||
Type: int32(tradeType),
|
||||
Type: int32(tType),
|
||||
Amount: amount,
|
||||
PayURL: payUrl,
|
||||
}
|
||||
@@ -202,7 +202,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
switch method {
|
||||
|
||||
// 检查支付宝交易
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradeQuery(ctx, alipay.TradeQuery{
|
||||
OutTradeNo: tradeNo,
|
||||
})
|
||||
@@ -228,7 +228,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
}
|
||||
|
||||
// 检查微信交易
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, _, err := g.WechatPay.Native.QueryOrderByOutTradeNo(ctx, native.QueryOrderByOutTradeNoRequest{
|
||||
OutTradeNo: &tradeNo,
|
||||
Mchid: &env.WechatPayMchId,
|
||||
@@ -274,12 +274,12 @@ func (s *transactionService) CompleteTransaction(ctx context.Context, q *q.Query
|
||||
}
|
||||
|
||||
// 检查交易状态
|
||||
if trade.Status != 0 {
|
||||
if trade.Status != int32(trade2.StatusPending) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 更新交易状态
|
||||
trade.Status = 1
|
||||
trade.Status = int32(trade2.StatusSuccess)
|
||||
trade.OuterNo = transId
|
||||
trade.Payment = payment
|
||||
trade.PaidAt = core.LocalDateTime(paidAt)
|
||||
@@ -294,11 +294,11 @@ func (s *transactionService) CompleteTransaction(ctx context.Context, q *q.Query
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo string, method TransactionMethod) error {
|
||||
func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo string, method trade2.Method) error {
|
||||
|
||||
switch method {
|
||||
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradeCancel(ctx, alipay.TradeCancel{
|
||||
OutTradeNo: tradeNo,
|
||||
})
|
||||
@@ -310,7 +310,7 @@ func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo stri
|
||||
return errors.New("交易取消失败")
|
||||
}
|
||||
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, err := g.WechatPay.Native.CloseOrder(ctx, native.CloseOrderRequest{
|
||||
Mchid: &env.WechatPayMchId,
|
||||
OutTradeNo: &tradeNo,
|
||||
@@ -333,7 +333,7 @@ func (s *transactionService) FinishTransaction(ctx context.Context, q *q.Query,
|
||||
Where(q.Trade.InnerNo.Eq(tradeNo)).
|
||||
Select(q.Trade.Status, q.Trade.CancelAt, q.Trade.PayURL).
|
||||
Updates(m.Trade{
|
||||
Status: 2,
|
||||
Status: int32(trade2.StatusCanceled),
|
||||
CancelAt: core.LocalDateTime(time),
|
||||
PayURL: "",
|
||||
})
|
||||
@@ -344,26 +344,12 @@ func (s *transactionService) FinishTransaction(ctx context.Context, q *q.Query,
|
||||
return nil
|
||||
}
|
||||
|
||||
type TransactionType int32
|
||||
|
||||
const (
|
||||
TransactionTypePurchase TransactionType = iota + 1
|
||||
TransactionTypeRecharge
|
||||
)
|
||||
|
||||
type TransactionMethod int32
|
||||
|
||||
const (
|
||||
TransactionMethodAlipay TransactionMethod = iota
|
||||
TransactionMethodWeChat
|
||||
)
|
||||
|
||||
type TransactionPrepareData struct {
|
||||
Subject string
|
||||
Amount float64
|
||||
ExpireAt time.Time
|
||||
Type TransactionType
|
||||
Method TransactionMethod
|
||||
Type trade2.Type
|
||||
Method trade2.Method
|
||||
CouponCode string
|
||||
}
|
||||
|
||||
@@ -376,7 +362,7 @@ type TransactionPrepareResult struct {
|
||||
|
||||
type TransactionVerifyData struct {
|
||||
TradeNo string
|
||||
Method TransactionMethod
|
||||
Method trade2.Method
|
||||
}
|
||||
|
||||
type TransactionVerifyResult struct {
|
||||
|
||||
Reference in New Issue
Block a user