Compare commits

...

2 Commits

Author SHA1 Message Date
wolter 50382f2c0a 后台,定时任务查询订单状态和关闭订单 2024-08-05 17:51:57 +08:00
wolter 0ac4feb07a 后台,订单字段调整fix 2024-08-05 17:01:58 +08:00
8 changed files with 149 additions and 78 deletions

View File

@ -4,6 +4,7 @@ import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/orderthirdpaylogmodel"
"PaymentCenter/app/third/paymentService"
@ -28,20 +29,59 @@ func RegisterCommand(c *command.Command) {
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{"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)
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 total > 0 {
orderIds := make([]int64, 0)
for _, v := range order {
orderIds = append(orderIds, v.Id)
} 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:
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: []byte(ali.AppPublicCert),
AlipayRootCert: []byte(ali.AlipayRootCert),
AlipayPublicCert: []byte(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()
@ -52,7 +92,7 @@ func closeOrder() {
return
}
}
utils.Log(nil, "关闭订单,修改订单状态成功", "count="+strconv.Itoa(len(order)))
utils.Log(nil, "关闭订单,修改订单状态成功", "总数量="+strconv.Itoa(len(order)), "成功数量="+strconv.Itoa(len(orderIds)))
}
// 主动查询订单支付状态
@ -64,7 +104,6 @@ func queryOrder() {
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)
@ -90,13 +129,32 @@ func queryOrder() {
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)
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
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
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), &query.Ali)
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
query.Ali = paymentService.AliPay{
AppId: orderInfo.AppId,
PrivateKey: ali.PrivateKey,
AppPublicCert: []byte(ali.AppPublicCert),
AlipayRootCert: []byte(ali.AlipayRootCert),
AlipayPublicCert: []byte(ali.AlipayPublicCert),
}
default:
utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType)
return
}
// 发起查询
@ -152,9 +210,10 @@ func queryOrder() {
}
// 写入日志
body, _ := json.Marshal(result)
log := orderthirdpaylogmodel.OrderThirdPayLog{
OrderId: orderInfo.Id,
PayCallback: "",
PayCallback: string(body),
Status: 0,
MerchantParam: "",
MerchantCallback: "",

View File

@ -28,4 +28,7 @@ const (
ORDER_STATUS_PAYED = 3
ORDER_STATUS_FAILED = 4
ORDER_STATUS_CLOSE = 5
PAY_CHANNLE_TYPE_WECHAT = 1 // 支付类型: 微信
PAY_CHANNLE_TYPE_ZFB = 2 // 支付类型:支付宝
)

View File

@ -47,16 +47,17 @@ var MsgEN = map[int]string{
}
var MsgZH = map[int]string{
Success: "请求成功",
ParamError: "参数错误",
NotFound: "数据不存在",
NotAuth: "未经授权",
NotLogin: "未登录",
RequestTimeOut: "请求超时",
MerchantNotFound: "商户不存在",
AppNotFound: "app_id未找到",
AppDisabled: "app通道关闭",
AppIpNotAllow: "ip不在白名单内",
Success: "请求成功",
ParamError: "参数错误",
NotFound: "数据不存在",
NotAuth: "未经授权",
NotLogin: "未登录",
RequestTimeOut: "请求超时",
MerchantNotFound: "商户不存在",
AppNotFound: "app_id未找到",
AppDisabled: "app通道关闭",
AppIpNotAllow: "ip不在白名单内",
SystemError: "系统错误",
PayChannelNotFound: "支付方式不存在",
}
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}

View File

@ -46,8 +46,8 @@ func (m *OrderRepo) OrdersBackendList(conn builder.Cond, pageFilter entities.Pag
}
repo = repo.Join("left", "app", "app.id = orders.app_id").
Join("left", "merchant", "merchant.id = orders.merchant_id").
Join("left", "pay_channel", "pay_channel.id = orders.pay_id")
return repo.Desc("create_time").FindAndCount(orderList)
Join("left", "pay_channel", "pay_channel.id = orders.pay_channel_id")
return repo.Desc("orders.create_time").FindAndCount(orderList)
}
func (m *OrderRepo) OrdersLeftPayChannelList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.OrdersLeftPayChannelList) error {
@ -56,6 +56,6 @@ func (m *OrderRepo) OrdersLeftPayChannelList(conn builder.Cond, pageFilter entit
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")
repo = repo.Join("left", "pay_channel", "pay_channel.id = orders.pay_channel_id")
return repo.Find(orderList)
}

View File

@ -12,27 +12,27 @@ import (
)
type OrderListRequest struct {
Id int64 `json:"id" form:"id"`
MerchantId int64 `json:"merchant_id" form:"merchant_id"`
PayId int64 `json:"pay_id" form:"pay_id"`
AppId int64 `json:"app_id" form:"app_id"`
MerchantOrderId string `json:"merchant_order_id" form:"merchant_order_id"`
Status int `json:"status" form:"status"`
OrderType int `json:"order_type" form:"order_type"`
StartTime string `json:"start_time" form:"start_time"`
EndTime string `json:"end_time" form:"end_time"`
Id int64 `json:"id" form:"id"`
MerchantId int64 `json:"merchant_id" form:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id" form:"pay_channel_id"`
AppId int64 `json:"app_id" form:"app_id"`
OutTreadNo string `json:"out_tread_no" form:"out_tread_no"`
Status int `json:"status" form:"status"`
OrderType int `json:"order_type" form:"order_type"`
StartTime string `json:"start_time" form:"start_time"`
EndTime string `json:"end_time" form:"end_time"`
entities.PageRequest
}
type OrderList struct {
Id int64 `json:"id"`
MerchantId int64 `json:"merchant_id"`
PayId int64 `json:"pay_id"`
AppId int64 `json:"app_id"`
MerchantOrderId string `json:"merchant_order_id"`
Status int `json:"status"`
OrderType int `json:"order_type"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Id int64 `json:"id"`
MerchantId int64 `json:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id"`
AppId int64 `json:"app_id"`
OutTreadNo string `json:"out_tread_no"`
Status int `json:"status"`
OrderType int `json:"order_type"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
entities.PageRequest
}
@ -54,9 +54,9 @@ func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) {
r.Id = o.Id
r.MerchantId = o.MerchantId
r.PayId = o.PayId
r.PayChannelId = o.PayChannelId
r.AppId = o.AppId
r.MerchantOrderId = o.MerchantOrderId
r.OutTreadNo = o.OutTreadNo
r.Status = o.Status
r.OrderType = o.OrderType
r.PageRequest = o.PageRequest
@ -65,24 +65,20 @@ func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) {
}
type OrdersResponse struct {
Id int64 `json:"id"`
MerchantId int64 `json:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id"`
AppId int64 `json:"app_id"`
OutTreadNo string `json:"out_tread_no"`
Status int `json:"status"`
OrderType int `json:"order_type"`
Amount int `json:"amount"`
IpAddress string `json:"ip_address"`
MerchantRequest string `json:"merchant_request"`
MerchantResponse string `json:"merchant_response"`
OrderResponse string `json:"order_response"`
ExtJson string `json:"ext_json"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
MerchantName string `json:"merchant_name"`
PayName string `json:"pay_name"`
AppName string `json:"app_name"`
Id int64 `json:"id"`
MerchantId int64 `json:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id"`
AppId int64 `json:"app_id"`
OutTreadNo string `json:"out_tread_no"`
Status int `json:"status"`
OrderType int `json:"order_type"`
Amount int `json:"amount"`
ExtJson string `json:"ext_json"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
MerchantName string `json:"merchant_name"`
PayName string `json:"pay_name"`
AppName string `json:"app_name"`
}
func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) {

View File

@ -17,7 +17,7 @@ type PayChannelResponse struct {
AppId string `json:"app_id"`
ExpireTime string `json:"expire_time"`
CreateTime string `json:"create_time"`
AliPayPayChannel *AliPayPayChannel `json:"ali_pay_pay_channel,omitempty"`
AliPayChannel *AliPayChannel `json:"ali_pay_channel,omitempty"`
WechatPayChannel *WechatPayChannel `json:"wechat_pay_channel,omitempty"`
}
@ -34,7 +34,7 @@ func (p *PayChannelResponse) ResponseFromDb(db paychannelmodel.PayChannel) {
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:
_ = json.Unmarshal([]byte(db.ExtJson), &p.AliPayPayChannel)
_ = json.Unmarshal([]byte(db.ExtJson), &p.AliPayChannel)
}
}
@ -44,7 +44,7 @@ type PayChannelCreateRequest struct {
ChannelType int `json:"channel_type" validate:"required" label:"支付渠道"` //支付渠道枚举,1微信JSAPI2微信H53微信app4微信Native5微信小程序6支付宝网页&移动应用7支付宝小程序8支付宝JSAPI
AppId string `json:"app_id" validate:"required" label:"应用appId"`
ExpireTime string `json:"expire_time"`
AliPayPayChannel AliPayPayChannel `json:"ali_pay_pay_channel,omitempty"`
AliPayChannel AliPayChannel `json:"ali_pay_channel,omitempty"`
WechatPayChannel WechatPayChannel `json:"wechat_pay_channel,omitempty"`
}
@ -55,7 +55,7 @@ type WechatPayChannel struct {
PrivateKey string `json:"private_key"` // 私钥 apiclient_key.pem 读取后的内容
}
type AliPayPayChannel struct {
type AliPayChannel struct {
PrivateKey string `json:"private_key"` // 应用私钥
AppPublicCert string `json:"app_public_cert"` // 应用公钥
AlipayRootCert string `json:"alipay_root_cert"` // 支付宝根证书
@ -78,7 +78,7 @@ func (p *PayChannelCreateRequest) RequestToDb() (db paychannelmodel.PayChannel,
b, _ := json.Marshal(p.WechatPayChannel)
db.ExtJson = string(b)
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
b, _ := json.Marshal(p.AliPayPayChannel)
b, _ := json.Marshal(p.AliPayChannel)
db.ExtJson = string(b)
default:
err = errors.New("支付渠道类型错误")
@ -98,7 +98,7 @@ type PayChannelUpdateRequest struct {
ChannelType int `json:"channel_type" validate:"required" label:"支付渠道"` //支付渠道枚举,1微信JSAPI2微信H53微信app4微信Native5微信小程序6支付宝网页&移动应用7支付宝小程序8支付宝JSAPI
AppId string `json:"app_id" validate:"required" label:"应用appId"`
ExpireTime string `json:"expire_time"`
AliPayPayChannel AliPayPayChannel `json:"ali_pay_pay_channel,omitempty"`
AliPayChannel AliPayChannel `json:"ali_pay_channel,omitempty"`
WechatPayChannel WechatPayChannel `json:"wechat_pay_channel,omitempty"`
}
@ -118,7 +118,7 @@ func (p PayChannelUpdateRequest) RequestToDb() (db paychannelmodel.PayChannel, e
b, _ := json.Marshal(p.WechatPayChannel)
db.ExtJson = string(b)
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
b, _ := json.Marshal(p.AliPayPayChannel)
b, _ := json.Marshal(p.AliPayChannel)
db.ExtJson = string(b)
default:
err = errors.New("支付渠道类型错误")

View File

@ -21,14 +21,14 @@ func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, t
if req.MerchantId > 0 {
conn = conn.And(builder.Eq{"merchant_id": req.MerchantId})
}
if req.PayId > 0 {
conn = conn.And(builder.Eq{"pay_id": req.PayId})
if req.PayChannelId > 0 {
conn = conn.And(builder.Eq{"pay_channel_id": req.PayChannelId})
}
if req.AppId > 0 {
conn = conn.And(builder.Eq{"app_id": req.AppId})
}
if req.MerchantOrderId != "" {
conn = conn.And(builder.Like{"merchant_order_id", req.MerchantOrderId})
if req.OutTreadNo != "" {
conn = conn.And(builder.Like{"out_tread_no", req.OutTreadNo})
}
if req.Status > 0 {
conn = conn.And(builder.Eq{"status": req.Status})

View File

@ -450,3 +450,15 @@ func SliceInStr(s string, slice []string) bool {
}
return false
}
// 判断支付方式支付宝或者微信
func PayType(payChannel int) int {
switch payChannel {
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:
return common.PAY_CHANNLE_TYPE_WECHAT
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
return common.PAY_CHANNLE_TYPE_ZFB
default:
return 0
}
}