From 514104a3917ddd26cd9db4fa5712501efa1a3220 Mon Sep 17 00:00:00 2001 From: wolter Date: Wed, 7 Aug 2024 10:49:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E6=9F=A5=E8=AF=A2=E9=80=80?= =?UTF-8?q?=E6=AC=BE=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 144 +++++++++++++++++++++---------- app/console/kernel.go | 1 + app/constants/common/common.go | 3 +- app/models/ordersmodel/orders.go | 29 ++++--- 4 files changed, 114 insertions(+), 63 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index de6eed2..47025ab 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -6,14 +6,13 @@ import ( "PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities/backend" "PaymentCenter/app/models/ordersmodel" - "PaymentCenter/app/models/orderthirdpaylogmodel" + "PaymentCenter/app/services/thirdpay/notify" "PaymentCenter/app/third/paymentService" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/app/utils" "PaymentCenter/config" "context" "encoding/json" - "fmt" "github.com/qit-team/snow-core/command" "strconv" "sync" @@ -83,7 +82,7 @@ func closeOrder() { if response.Code == payCommon.PAY_SUCCESS_CODE { orderIds = append(orderIds, orderInfo.Id) } else { - utils.Log(nil, "关闭订单,上游支付失败", response) + utils.Log(nil, "关闭订单,上游失败", response) } } // 修改订单状态为关闭 @@ -172,64 +171,115 @@ func queryOrder() { // 成功 status = common.ORDER_STATUS_PAYED case "REFUND": - // 退款 - status = common.ORDER_STATUS_REFUND - + // 退款 订单支付完成才能退款,所以支付单的状态是支付完成 + status = common.ORDER_STATUS_PAYED case "NOTPAY": // 未支付 return case "CLOSED": // 关闭 status = common.ORDER_STATUS_CLOSE - } // 回调通知下游 todo + notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.PayerTotal), "") + utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult) + } + }(orderInfo) + } + wg.Wait() + } +} - // 更新订单状态 todo - orderUpdate := ordersmodel.Orders{ - Id: orderInfo.Id, - Status: status, - PayerTotal: int(result.Result.PayerTotal), +// 主动查询退款订单状态 +func queryRefundOrder() { + 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{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))}) + cond = cond.And(builder.Eq{"order_type": common.ORDER_TYPE_REFUND}) + 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.OrderRefundQueryRequest{ + RefundOrderId: orderInfo.Id, + OrderId: orderInfo.RefundOrderId, + } + switch utils.PayType(orderInfo.ChannelType) { + case common.PAY_CHANNLE_TYPE_WECHAT: + wx := backend.WechatPayChannel{} + _ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx) + query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT + query.Wx = paymentService.WxPay{ + AppId: orderInfo.AppId, + MchId: wx.MchId, + SerialNo: wx.SerialNo, + ApiV3Key: wx.ApiV3Key, + PrivateKey: wx.PrivateKey, } + case common.PAY_CHANNLE_TYPE_ZFB: + ali := backend.AliPayChannel{} + query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB + _ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali) + query.Ali = paymentService.AliPay{ + AppId: orderInfo.AppId, + PrivateKey: ali.PrivateKey, + AppPublicCert: ali.AppPublicCert, + AlipayRootCert: ali.AlipayRootCert, + AlipayPublicCert: ali.AlipayPublicCert, + } + default: + utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType) + return + } - session := ordersmodel.GetInstance().GetDb().NewSession() - if err = session.Begin(); err != nil { - utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err) + // 发起查询 + result := paymentService.OrderRefundQuery(ctx, query) + utils.Log(nil, "主动查询退款订单状态,上游返回数据", result) + // 查询成功,校验状态 + var status int + if result.Code == payCommon.PAY_SUCCESS_CODE { + // 退款状态 0:未申请,1:退款中,2:退款成功,3:退款失败 + switch result.Result.RefundStatus { + case 0: + // 未申请 + utils.Log(nil, "主动查询退款订单状态,未申请", status) return - } - defer func() { - if err != nil { - session.Rollback() - } else { - err = session.Commit() - } - }() - orderLogRepo := data.NewOrderThirdPayLogRepo(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) + case 1: + // 退款中 + status = common.ORDER_STATUS_PAYING return + case 2: + // 退款成功 + status = common.ORDER_STATUS_PAYED + case 3: + // 退款失败 + status = common.ORDER_STATUS_FAILED } - - // 写入日志 - body, _ := json.Marshal(result) - log := orderthirdpaylogmodel.OrderThirdPayLog{ - OrderId: orderInfo.Id, - PayCallback: string(body), - Status: 0, - PayParam: fmt.Sprintf(`{"pay_channel_id":%d}`, orderInfo.PayChannelId), - Type: common.THIRD_ORDER_TYPE_ORDER_QUERY, - MerchantCallback: "{}", - } - _, err = orderLogRepo.OrderThirdPayLogInsertOne(&log) - if err != nil { - utils.Log(nil, "主动查询订单支付状态,写入支付日志失败", err) - } - + // 回调通知下游 todo + notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.RefundFee), "") + utils.Log(nil, "主动查询退款订单状态,回调下游", notifyResult) } }(orderInfo) } diff --git a/app/console/kernel.go b/app/console/kernel.go index 88316d4..578f286 100644 --- a/app/console/kernel.go +++ b/app/console/kernel.go @@ -17,4 +17,5 @@ func RegisterSchedule(c *cron.Cron) { //c.AddFunc("@every 10s", test) c.AddFunc("@every 60s", closeOrder) c.AddFunc("@every 10s", queryOrder) + c.AddFunc("@every 60s", queryRefundOrder) } diff --git a/app/constants/common/common.go b/app/constants/common/common.go index f560214..0549e43 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -22,13 +22,12 @@ const ( ADMIN_USER_NAME = "User-Name" ADMIN_USER_INCLUDEUSERS = "Include-Users" - // '订单状态: 1待支付、2支付中、3支付成功、4支付失败、5订单关闭 6支付完成后退款, + // '订单状态: 1待支付、2支付中、3支付成功、4支付失败、5订单关闭 ORDER_STATUS_WAITPAY = 1 ORDER_STATUS_PAYING = 2 ORDER_STATUS_PAYED = 3 ORDER_STATUS_FAILED = 4 ORDER_STATUS_CLOSE = 5 - ORDER_STATUS_REFUND = 6 // 订单类型,1支付,2退款 ORDER_TYPE_PAY = 1 diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go index ed8219a..e75a1a7 100644 --- a/app/models/ordersmodel/orders.go +++ b/app/models/ordersmodel/orders.go @@ -13,20 +13,21 @@ var ( // 实体 type Orders struct { - Id int64 - MerchantId int64 `xorm:"'merchant_id' bigint(20)"` - PayChannelId int64 `xorm:"'pay_channel_id' bigint(20)"` - AppId int64 `xorm:"'app_id' bigint(20)"` - OutTreadNo string `xorm:"'out_tread_no' varchar(50)"` - OrderType int `xorm:"'order_type' TINYINT"` - Amount int `xorm:"'amount' int(11)"` - PayerTotal int `xorm:"'payer_total' int(11)"` - ExtJson string `xorm:"'ext_json' varchar(1024)"` - Desc string `xorm:"'desc' varchar(100)"` - CreateTime time.Time `xorm:"'create_time' datetime created"` - UpdateTime time.Time `xorm:"'update_time' timestamp updated"` - Status int `xorm:"'status' TINYINT"` - DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` + Id int64 + MerchantId int64 `xorm:"'merchant_id' bigint(20)"` + PayChannelId int64 `xorm:"'pay_channel_id' bigint(20)"` + AppId int64 `xorm:"'app_id' bigint(20)"` + OutTreadNo string `xorm:"'out_tread_no' varchar(50)"` + OrderType int `xorm:"'order_type' TINYINT"` + RefundOrderId int64 `xorm:"'refund_order_id' bigint(20)"` + Amount int `xorm:"'amount' int(11)"` + PayerTotal int `xorm:"'payer_total' int(11)"` + ExtJson string `xorm:"'ext_json' varchar(1024)"` + Desc string `xorm:"'desc' varchar(100)"` + CreateTime time.Time `xorm:"'create_time' datetime created"` + UpdateTime time.Time `xorm:"'update_time' timestamp updated"` + Status int `xorm:"'status' TINYINT"` + DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` } type OrdersBackendList struct {