From 490e2fa15f734cff31ca89f2cd77526e2fac5e90 Mon Sep 17 00:00:00 2001 From: wolter Date: Mon, 5 Aug 2024 14:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 123 ++++++++++++++++++++--- app/console/kernel.go | 3 + app/constants/common/common.go | 2 +- app/data/orders.go | 10 ++ app/http/entities/backend/pay_channel.go | 2 +- app/models/ordersmodel/orders.go | 6 ++ config/config.go | 7 ++ 7 files changed, 137 insertions(+), 16 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index c77f187..676015d 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -4,10 +4,17 @@ 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" ) @@ -25,7 +32,7 @@ func closeOrder() { 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.Hour)}) + 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) @@ -48,31 +55,119 @@ func closeOrder() { 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() - cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"create_time": time.Now().Add(-time.Second)}) - order := make([]ordersmodel.Orders, 0) - total, err := repo.OrderList(cond, entities.PageRequest{}, &order) + 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 total > 0 { - // 发起查询上游支付 - for _, v := range order { - go func(order ordersmodel.Orders) { - // 发起查询 - utils.Log(nil, "主动查询订单支付状态,发起查询", order.Id) - // 解析上游结果 + } else if len(order) > 0 { + ch := make(chan struct{}, config.GetConf().CronConfig.ConcurrentNumber) + wg := sync.WaitGroup{} - // 修改订单状态 - }(v) + 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() } } diff --git a/app/console/kernel.go b/app/console/kernel.go index 7ef72d9..88316d4 100644 --- a/app/console/kernel.go +++ b/app/console/kernel.go @@ -1,6 +1,8 @@ package console import ( + "PaymentCenter/config" + "fmt" "github.com/robfig/cron" ) @@ -9,6 +11,7 @@ import ( * @wiki https://godoc.org/github.com/robfig/cron */ func RegisterSchedule(c *cron.Cron) { + fmt.Println(config.GetConf().CronConfig) //c.AddFunc("0 30 * * * *", test) //c.AddFunc("@hourly", test) //c.AddFunc("@every 10s", test) diff --git a/app/constants/common/common.go b/app/constants/common/common.go index 7051a80..eff49bc 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -22,7 +22,7 @@ const ( ADMIN_USER_NAME = "User-Name" ADMIN_USER_INCLUDEUSERS = "Include-Users" - // '订单状态,待支付、支付中、支付成功、支付失败、订单关闭', + // '订单状态,1待支付、2支付中、3支付成功、4支付失败、5订单关闭', ORDER_STATUS_WAITPAY = 1 ORDER_STATUS_PAYING = 2 ORDER_STATUS_PAYED = 3 diff --git a/app/data/orders.go b/app/data/orders.go index c9bd442..9bc6c7e 100644 --- a/app/data/orders.go +++ b/app/data/orders.go @@ -49,3 +49,13 @@ func (m *OrderRepo) OrdersBackendList(conn builder.Cond, pageFilter entities.Pag Join("left", "pay_channel", "pay_channel.id = orders.pay_id") return repo.Desc("create_time").FindAndCount(orderList) } + +func (m *OrderRepo) OrdersLeftPayChannelList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.OrdersLeftPayChannelList) error { + repo := m.repo.Select(`orders.*,pay_channel.channel_type,pay_channel.app_id ,pay_channel.ext_json`). + Where(conn) + if pageFilter.Page > 0 { + repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1)) + } + repo = repo.Join("left", "pay_channel", "pay_channel.id = orders.pay_id") + return repo.Find(orderList) +} diff --git a/app/http/entities/backend/pay_channel.go b/app/http/entities/backend/pay_channel.go index e6b3af2..2fe10aa 100644 --- a/app/http/entities/backend/pay_channel.go +++ b/app/http/entities/backend/pay_channel.go @@ -30,7 +30,7 @@ func (p *PayChannelResponse) ResponseFromDb(db paychannelmodel.PayChannel) { p.ExpireTime = db.ExpireTime.Format("2006-01-02 15:04:05") p.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") - switch p.ChannelType { + switch db.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(db.ExtJson), &p.WechatPayChannel) case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI: diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go index 2d8b1c2..c5e96c8 100644 --- a/app/models/ordersmodel/orders.go +++ b/app/models/ordersmodel/orders.go @@ -36,6 +36,12 @@ type OrdersBackendList struct { PayName string `xorm:"'pay_name' varchar(128)"` AppName string `xorm:"'app_name' varchar(128)"` } +type OrdersLeftPayChannelList struct { + Orders `xorm:"extends"` + ChannelType int `xorm:"'channel_type' int(11)"` + AppId string `xorm:"'app_id' varchar(255)"` + ExtJson string `xorm:"'ext_json' JSON"` +} // 表名 func (m *Orders) TableName() string { diff --git a/config/config.go b/config/config.go index 4db996c..09ced4e 100644 --- a/config/config.go +++ b/config/config.go @@ -37,6 +37,7 @@ type Config struct { Jwt Jwt `toml:"Jwt"` AliOss AliOss `toml:"AliOss"` AdminGate []string `toml:"AdminGate"` + CronConfig CronConfig `toml:"CronConfig"` } type AliOss struct { @@ -83,6 +84,12 @@ type Nacos struct { Port int64 } +type CronConfig struct { + CloseOrderTime int `toml:"CloseOrderTime"` + QueryOrderTime int `toml:"QueryOrderTime"` + ConcurrentNumber int `toml:"ConcurrentNumber"` +} + func newConfig() *Config { return new(Config) }