PaymentCenter/app/services/thirdpay/do/pay_check.go

112 lines
2.7 KiB
Go

package do
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/types"
"xorm.io/builder"
"PaymentCenter/app/models/merchantmodel"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/paychannelmodel"
"context"
)
type PayCheck struct {
ctx *context.Context
Reqs *types.Reqs
AppCheck *services.AppCheck
RequestLog *orderrequestlogmodel.OrderRequestLog
Channel *paychannelmodel.PayChannel
Merchant *merchantmodel.Merchant
OldOrder *ordersmodel.Orders
CheckCode int
}
func NewPayCheck(ctx *context.Context, reqs *types.Reqs, appCheck *services.AppCheck, ip string) *PayCheck {
if appCheck == nil {
appCheck = services.GetAppCheck(reqs.AppId, ip)
}
return &PayCheck{
ctx: ctx,
Reqs: reqs,
AppCheck: appCheck,
CheckCode: appCheck.Code,
}
}
func (w *PayCheck) CheckPayInfo() {
w.CheckMerchant()
if w.CheckCode != errorcode.Success {
return
}
w.CheckPayChannel()
if w.CheckCode != errorcode.Success {
return
}
}
func (w *PayCheck) CheckPayChannel() {
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": w.Reqs.PayChannelId})
w.Channel, w.CheckCode = services.GetAndCheckPayChannel(&paychannelmodel.PayChannel{}, conn)
}
func (w *PayCheck) CheckMerchant() {
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": w.AppCheck.App.MerchantId})
w.Merchant, w.CheckCode = services.GetAndCheckMerchant(&merchantmodel.Merchant{}, conn)
}
func (w *PayCheck) CheckOrderPay() {
w.GetOrder()
if w.OldOrder != nil {
switch w.OldOrder.Status {
case common.ORDER_STATUS_CLOSE:
w.CheckCode = errorcode.OrderClosed
case common.ORDER_STATUS_FAILED:
w.CheckCode = errorcode.OrderFailed
case common.ORDER_STATUS_PAYED:
w.CheckCode = errorcode.OrderPayed
default:
}
}
return
}
func (w *PayCheck) CheckOrderRefund() {
w.GetOrder()
if w.OldOrder == nil {
w.CheckCode = errorcode.OrdersNotFound
}
switch w.OldOrder.Status {
case common.ORDER_STATUS_CLOSE:
w.CheckCode = errorcode.OrderClosed
case common.ORDER_STATUS_FAILED:
w.CheckCode = errorcode.OrderFailed
case common.ORDER_STATUS_WAITPAY:
w.CheckCode = errorcode.OrderStatusErr
case common.ORDER_STATUS_PAYING:
w.CheckCode = errorcode.OrderStatusErr
default:
}
return
}
func (w *PayCheck) GetOrder() {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"out_tread_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId})
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
if code == errorcode.SystemError {
w.CheckCode = code
return
}
if code == errorcode.Success {
w.OldOrder = order
}
}