feat: add PaymentMethod field to TopUp model and enhance payment method validation in topup controllers
This commit is contained in:
parent
b2a40d3381
commit
8aaec8b1cc
@ -340,6 +340,10 @@ func EpayNotify(c *gin.Context) {
|
|||||||
log.Printf("易支付回调未找到订单: %v", verifyInfo)
|
log.Printf("易支付回调未找到订单: %v", verifyInfo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if topUp.PaymentMethod == "stripe" || topUp.PaymentMethod == "creem" || topUp.PaymentMethod == "waffo" {
|
||||||
|
log.Printf("易支付回调订单支付方式不匹配: %s, 订单号: %s", topUp.PaymentMethod, verifyInfo.ServiceTradeNo)
|
||||||
|
return
|
||||||
|
}
|
||||||
if topUp.Status == "pending" {
|
if topUp.Status == "pending" {
|
||||||
topUp.Status = "success"
|
topUp.Status = "success"
|
||||||
err := topUp.Update()
|
err := topUp.Update()
|
||||||
|
|||||||
@ -108,12 +108,13 @@ func (*CreemAdaptor) RequestPay(c *gin.Context, req *CreemPayRequest) {
|
|||||||
|
|
||||||
// 先创建订单记录,使用产品配置的金额和充值额度
|
// 先创建订单记录,使用产品配置的金额和充值额度
|
||||||
topUp := &model.TopUp{
|
topUp := &model.TopUp{
|
||||||
UserId: id,
|
UserId: id,
|
||||||
Amount: selectedProduct.Quota, // 充值额度
|
Amount: selectedProduct.Quota, // 充值额度
|
||||||
Money: selectedProduct.Price, // 支付金额
|
Money: selectedProduct.Price, // 支付金额
|
||||||
TradeNo: referenceId,
|
TradeNo: referenceId,
|
||||||
CreateTime: time.Now().Unix(),
|
PaymentMethod: PaymentMethodCreem,
|
||||||
Status: common.TopUpStatusPending,
|
CreateTime: time.Now().Unix(),
|
||||||
|
Status: common.TopUpStatusPending,
|
||||||
}
|
}
|
||||||
err = topUp.Insert()
|
err = topUp.Insert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -234,6 +234,11 @@ func sessionAsyncPaymentFailed(event stripe.Event) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if topUp.PaymentMethod != PaymentMethodStripe {
|
||||||
|
log.Printf("异步支付失败,订单支付方式不匹配: %s, ref: %s", topUp.PaymentMethod, referenceId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if topUp.Status != common.TopUpStatusPending {
|
if topUp.Status != common.TopUpStatusPending {
|
||||||
log.Printf("异步支付失败,订单状态非pending: %s, ref: %s", topUp.Status, referenceId)
|
log.Printf("异步支付失败,订单状态非pending: %s, ref: %s", topUp.Status, referenceId)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -12,17 +12,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TopUp struct {
|
type TopUp struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"user_id" gorm:"index"`
|
UserId int `json:"user_id" gorm:"index"`
|
||||||
Amount int64 `json:"amount"`
|
Amount int64 `json:"amount"`
|
||||||
Money float64 `json:"money"`
|
Money float64 `json:"money"`
|
||||||
TradeNo string `json:"trade_no" gorm:"unique;type:varchar(255);index"`
|
TradeNo string `json:"trade_no" gorm:"unique;type:varchar(255);index"`
|
||||||
PaymentMethod string `json:"payment_method" gorm:"type:varchar(50)"`
|
PaymentMethod string `json:"payment_method" gorm:"type:varchar(50)"`
|
||||||
CreateTime int64 `json:"create_time"`
|
CreateTime int64 `json:"create_time"`
|
||||||
CompleteTime int64 `json:"complete_time"`
|
CompleteTime int64 `json:"complete_time"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrPaymentMethodMismatch = errors.New("payment method mismatch")
|
||||||
|
|
||||||
func (topUp *TopUp) Insert() error {
|
func (topUp *TopUp) Insert() error {
|
||||||
var err error
|
var err error
|
||||||
err = DB.Create(topUp).Error
|
err = DB.Create(topUp).Error
|
||||||
@ -74,6 +76,10 @@ func Recharge(referenceId string, customerId string) (err error) {
|
|||||||
return errors.New("充值订单不存在")
|
return errors.New("充值订单不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if topUp.PaymentMethod != "stripe" {
|
||||||
|
return ErrPaymentMethodMismatch
|
||||||
|
}
|
||||||
|
|
||||||
if topUp.Status != common.TopUpStatusPending {
|
if topUp.Status != common.TopUpStatusPending {
|
||||||
return errors.New("充值订单状态错误")
|
return errors.New("充值订单状态错误")
|
||||||
}
|
}
|
||||||
@ -325,6 +331,10 @@ func RechargeCreem(referenceId string, customerEmail string, customerName string
|
|||||||
return errors.New("充值订单不存在")
|
return errors.New("充值订单不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if topUp.PaymentMethod != "creem" {
|
||||||
|
return ErrPaymentMethodMismatch
|
||||||
|
}
|
||||||
|
|
||||||
if topUp.Status != common.TopUpStatusPending {
|
if topUp.Status != common.TopUpStatusPending {
|
||||||
return errors.New("充值订单状态错误")
|
return errors.New("充值订单状态错误")
|
||||||
}
|
}
|
||||||
@ -396,6 +406,10 @@ func RechargeWaffo(tradeNo string) (err error) {
|
|||||||
return errors.New("充值订单不存在")
|
return errors.New("充值订单不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if topUp.PaymentMethod != "waffo" {
|
||||||
|
return ErrPaymentMethodMismatch
|
||||||
|
}
|
||||||
|
|
||||||
if topUp.Status == common.TopUpStatusSuccess {
|
if topUp.Status == common.TopUpStatusSuccess {
|
||||||
return nil // 幂等:已成功直接返回
|
return nil // 幂等:已成功直接返回
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user