182 lines
5.4 KiB
Go
182 lines
5.4 KiB
Go
package console
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/common"
|
|
"PaymentCenter/app/data"
|
|
"PaymentCenter/app/http/entities"
|
|
"PaymentCenter/app/models/orderlogmodel"
|
|
"PaymentCenter/app/models/ordersmodel"
|
|
"PaymentCenter/app/third/paymentService"
|
|
"PaymentCenter/app/third/paymentService/payCommon"
|
|
"PaymentCenter/app/utils"
|
|
"PaymentCenter/config"
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/qit-team/snow-core/command"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
func RegisterCommand(c *command.Command) {
|
|
c.AddFunc("test", test)
|
|
c.AddFunc("closeOrder", closeOrder)
|
|
}
|
|
|
|
// 关闭长时间支付中的订单
|
|
func closeOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "关闭订单", now)
|
|
// 查询未支付的订单
|
|
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
|
|
// 拼接条件
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Lt{"create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.CloseOrderTime))})
|
|
|
|
order := make([]ordersmodel.Orders, 0)
|
|
total, err := repo.OrderList(cond, entities.PageRequest{}, &order)
|
|
if err != nil {
|
|
utils.Log(nil, "关闭订单,查询未支付订单失败", err)
|
|
} else if total > 0 {
|
|
orderIds := make([]int64, 0)
|
|
for _, v := range order {
|
|
orderIds = append(orderIds, v.Id)
|
|
}
|
|
// 修改订单状态为关闭
|
|
cond = builder.NewCond()
|
|
cond = cond.And(builder.In("id", orderIds))
|
|
_, err = repo.OrderUpdate(&ordersmodel.Orders{Status: common.ORDER_STATUS_CLOSE}, cond, "status")
|
|
if err != nil {
|
|
utils.Log(nil, "关闭订单,修改订单状态失败", err)
|
|
return
|
|
}
|
|
}
|
|
utils.Log(nil, "关闭订单,修改订单状态成功", "count="+strconv.Itoa(len(order)))
|
|
}
|
|
|
|
// 主动查询订单支付状态
|
|
func queryOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "主动查询订单支付状态", now)
|
|
ctx := context.Background()
|
|
// 查询未支付的订单
|
|
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
|
|
// 拼接条件
|
|
cond := builder.NewCond()
|
|
config.GetConf()
|
|
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))})
|
|
order := make([]ordersmodel.OrdersLeftPayChannelList, 0)
|
|
err := repo.OrdersLeftPayChannelList(cond, entities.PageRequest{}, &order)
|
|
if err != nil {
|
|
utils.Log(nil, "主动查询订单支付状态,查询未付中订单失败", err)
|
|
return
|
|
} else if len(order) > 0 {
|
|
ch := make(chan struct{}, config.GetConf().CronConfig.ConcurrentNumber)
|
|
wg := sync.WaitGroup{}
|
|
|
|
for index := range order {
|
|
ch <- struct{}{}
|
|
wg.Add(1)
|
|
orderInfo := order[index]
|
|
|
|
// 发起查询上游支付
|
|
go func(orderInfo ordersmodel.OrdersLeftPayChannelList) {
|
|
defer func() {
|
|
<-ch
|
|
wg.Done()
|
|
}()
|
|
|
|
query := paymentService.PayOrderQueryRequest{
|
|
OrderId: orderInfo.Id,
|
|
}
|
|
switch orderInfo.ChannelType {
|
|
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
|
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &query.Wx)
|
|
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
|
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
|
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
|
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &query.Ali)
|
|
}
|
|
|
|
// 发起查询
|
|
result := paymentService.PayOrderQuery(ctx, query)
|
|
utils.Log(nil, "主动查询订单支付状态,上游返回数据", result)
|
|
// 查询成功,校验状态
|
|
var status int
|
|
if result.Code == payCommon.PAY_SUCCESS_CODE {
|
|
switch result.Result.TradeState {
|
|
case "SUCCESS":
|
|
// 成功
|
|
status = common.ORDER_STATUS_PAYED
|
|
case "REFUND":
|
|
// 退款
|
|
|
|
case "NOTPAY":
|
|
// 未支付
|
|
return
|
|
case "CLOSED":
|
|
// 关闭
|
|
status = common.ORDER_STATUS_CLOSE
|
|
|
|
}
|
|
// 回调通知下游 todo
|
|
|
|
// 更新订单状态 todo
|
|
orderUpdate := ordersmodel.Orders{
|
|
Id: orderInfo.Id,
|
|
Status: status,
|
|
}
|
|
|
|
session := ordersmodel.GetInstance().GetDb().NewSession()
|
|
if err = session.Begin(); err != nil {
|
|
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
|
return
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
session.Rollback()
|
|
} else {
|
|
err = session.Commit()
|
|
}
|
|
}()
|
|
orderLogRepo := data.NewOrderLogRepo(session)
|
|
orderRepo := data.NewOrderRepo(session)
|
|
|
|
conn := builder.NewCond()
|
|
conn = conn.And(builder.Eq{"id": orderInfo.Id})
|
|
_, err = orderRepo.OrderUpdate(&orderUpdate, conn)
|
|
if err != nil {
|
|
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
|
return
|
|
}
|
|
|
|
// 写入支付日志
|
|
log := orderlogmodel.OrderLog{
|
|
OrderId: orderInfo.Id,
|
|
PayCallback: "",
|
|
Status: 0,
|
|
MerchantParam: "",
|
|
MerchantCallback: "",
|
|
}
|
|
_, err = orderLogRepo.OrderLogInsertOne(&log)
|
|
if err != nil {
|
|
utils.Log(nil, "主动查询订单支付状态,写入支付日志失败", err)
|
|
}
|
|
|
|
}
|
|
}(orderInfo)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
}
|
|
|
|
// 回调下游
|
|
func callbackOrder() {
|
|
var now = time.Now().Format(time.DateTime)
|
|
utils.Log(nil, "回调下游", now)
|
|
// 查询回调失败的订单
|
|
|
|
// 发起回调
|
|
}
|