PaymentCenter/app/console/command.go

298 lines
9.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package console
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/services/thirdpay/notify"
"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)
c.AddFunc("queryOrder", queryOrder)
}
// 关闭长时间支付中的订单
func closeOrder() {
var now = time.Now().Format(time.DateTime)
utils.Log(nil, "关闭订单", now)
var ctx = context.Background()
orderIds := make([]int64, 0)
// 查询未支付的订单
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
// 拼接条件
cond := builder.NewCond()
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Lt{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.CloseOrderTime))})
// 查询订单
order := make([]ordersmodel.OrdersLeftPayChannelList, 0)
err := repo.OrdersLeftPayChannelList(cond, entities.PageRequest{}, &order)
if err != nil {
utils.Log(nil, "关闭订单,查询未支付订单失败", err)
} else if len(order) > 0 {
for i := range order {
orderInfo := order[i]
// 往上游发送关闭订单请求
req := paymentService.OrderCloseRequest{OrderId: orderInfo.Id}
switch utils.PayType(orderInfo.ChannelType) {
case common.PAY_CHANNLE_TYPE_WECHAT:
wx := backend.WechatPayChannel{}
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx)
req.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
req.Wx = paymentService.WxPay{
AppId: orderInfo.AppId,
MchId: wx.MchId,
SerialNo: wx.SerialNo,
ApiV3Key: wx.ApiV3Key,
PrivateKey: wx.PrivateKey,
}
case common.PAY_CHANNLE_TYPE_ZFB:
var ali backend.AliPayChannel
req.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
req.Ali = paymentService.AliPay{
AppId: orderInfo.AppId,
PrivateKey: ali.PrivateKey,
AppPublicCert: ali.AppPublicCert,
AlipayRootCert: ali.AlipayRootCert,
AlipayPublicCert: ali.AlipayPublicCert,
}
default:
utils.Log(nil, "关闭订单,支付渠道不支持", orderInfo.ChannelType)
continue
}
// 发起关闭订单请求
response := paymentService.OrderClose(ctx, req)
// 成功
if response.Code == payCommon.PAY_SUCCESS_CODE {
orderIds = append(orderIds, orderInfo.Id)
} else {
utils.Log(nil, "关闭订单,上游失败", response)
}
}
// 修改订单状态为关闭
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, "关闭订单,修改订单状态成功", "总数量="+strconv.Itoa(len(order)), "成功数量="+strconv.Itoa(len(orderIds)))
}
// 主动查询订单支付状态
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{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))})
cond = cond.And(builder.Eq{"order_type": common.ORDER_TYPE_PAY})
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 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
}
// 发起查询
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":
// 退款 订单支付完成才能退款,所以支付单的状态是支付完成
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()
}
}
// 主动查询退款订单状态
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
}
// 发起查询
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
case 1:
// 退款中
status = common.ORDER_STATUS_PAYING
return
case 2:
// 退款成功
status = common.ORDER_STATUS_PAYED
case 3:
// 退款失败
status = common.ORDER_STATUS_FAILED
}
// 回调通知下游 todo
notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.RefundFee), "")
utils.Log(nil, "主动查询退款订单状态,回调下游", notifyResult)
}
}(orderInfo)
}
wg.Wait()
}
}
// 回调下游
func callbackOrder() {
var now = time.Now().Format(time.DateTime)
utils.Log(nil, "回调下游", now)
// 查询回调失败的订单
// 发起回调
}