Merge branch 'refs/heads/feature/zxp' into dev/dev1.0

This commit is contained in:
wolter 2024-08-08 09:18:49 +08:00
commit 3061987dae
4 changed files with 114 additions and 63 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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

View File

@ -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 {