|
|
@ -23,39 +23,31 @@ func init() { |
|
|
|
var orderDelayQueue = make(chan *orderDelayQueueParam, math.MaxInt8) |
|
|
|
|
|
|
|
type orderDelayQueueParam struct { |
|
|
|
First bool `json:"first"` |
|
|
|
Expires int64 `json:"expires"` |
|
|
|
Delay int `json:"delay"` |
|
|
|
OutTradeNo string `json:"out_trade_no"` |
|
|
|
Body string `json:"body"` |
|
|
|
Amount int `json:"amount"` // 金额
|
|
|
|
Status int `json:"status"` // 0 订单 3 退款
|
|
|
|
OpenId string `json:"open_id"` // 被操作人
|
|
|
|
Order *models.UserOrder |
|
|
|
First bool `json:"first"` |
|
|
|
Expires int64 `json:"expires"` |
|
|
|
Delay int `json:"delay"` |
|
|
|
} |
|
|
|
|
|
|
|
func PutOrderDelayQueue(body, outTradeNo, openId string, amount, status int, expires int64, delay int) { |
|
|
|
func PutOrderDelayQueue(order *models.UserOrder, expires int64, delay int) { |
|
|
|
if expires == 0 { |
|
|
|
expires = time.Now().Add(12 * time.Hour).Unix() // 2 个小时
|
|
|
|
} |
|
|
|
|
|
|
|
if delay == 0 { |
|
|
|
delay = 100 |
|
|
|
delay = 1000 |
|
|
|
} |
|
|
|
|
|
|
|
orderDelayQueue <- &orderDelayQueueParam{ |
|
|
|
First: true, |
|
|
|
Expires: expires, |
|
|
|
Delay: delay, |
|
|
|
OutTradeNo: outTradeNo, |
|
|
|
Body: body, |
|
|
|
Amount: amount, |
|
|
|
Status: status, |
|
|
|
OpenId: openId, |
|
|
|
First: true, |
|
|
|
Expires: expires, |
|
|
|
Delay: delay, |
|
|
|
Order: order, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func loopUnifiedOrder() { |
|
|
|
orders, err := models.GetUserOrdersByStatus(core2.FormatTime(time.Now()), 0, 3) |
|
|
|
orders, err := models.GetUserOrdersByStatus(core2.FormatTime(time.Now().Add(12*time.Hour)), 0, 3) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
@ -65,7 +57,8 @@ func loopUnifiedOrder() { |
|
|
|
if err != nil { |
|
|
|
expire = time.Now().Add(12 * time.Hour) |
|
|
|
} |
|
|
|
PutOrderDelayQueue(order.Body, order.OutTradeNo, order.OpenId, int(order.TotalFee), order.Status, expire.Unix(), 0) |
|
|
|
|
|
|
|
PutOrderDelayQueue(order, expire.Add(12*time.Hour).Unix(), 0) |
|
|
|
} |
|
|
|
|
|
|
|
defer func() { |
|
|
@ -83,11 +76,12 @@ func loopUnifiedOrder() { |
|
|
|
panic("通道异常关闭") |
|
|
|
} |
|
|
|
if param.Expires <= time.Now().Unix() { |
|
|
|
//if param.Status == 0 {
|
|
|
|
//order := new(models.UserOrder)
|
|
|
|
//_, err = order.UpdateStatusByOutTradeNo(param.OutTradeNo, 7)
|
|
|
|
//Close(param.OutTradeNo) // 超时关闭订单
|
|
|
|
//}
|
|
|
|
if param.Order.Status == 0 { |
|
|
|
order := new(models.UserOrder) |
|
|
|
_, err = order.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 7) |
|
|
|
Close(param.Order.OutTradeNo) // 超时关闭订单
|
|
|
|
go HandleFailed(param) |
|
|
|
} |
|
|
|
continue // 超时
|
|
|
|
} |
|
|
|
|
|
|
@ -97,32 +91,33 @@ func loopUnifiedOrder() { |
|
|
|
} |
|
|
|
param.First = false |
|
|
|
|
|
|
|
if param.Status == 0 { |
|
|
|
res, err := OrderQuery(param.OutTradeNo) |
|
|
|
if param.Order.Status == 0 { |
|
|
|
res, err := OrderQuery(param.Order.OutTradeNo) |
|
|
|
|
|
|
|
// 出现错误
|
|
|
|
if err != nil { |
|
|
|
logger.Error("查询订单出现错误", zap.String("错误原因", err.Error()), |
|
|
|
zap.String("交易订单号", param.OutTradeNo)) |
|
|
|
zap.String("交易订单号", param.Order.OutTradeNo)) |
|
|
|
orderDelayQueue <- param // 重新进入队列
|
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if res.Query.TradeState == CODE_TRADE_SUCCESS { |
|
|
|
go HandleSuccess(res.Order) |
|
|
|
param.Order = res.Order |
|
|
|
go HandleSuccess(param) |
|
|
|
} else if res.Query.TradeState == CODE_TRADE_REFUND { |
|
|
|
param.Status = 3 |
|
|
|
param.Order.Status = 3 |
|
|
|
orderDelayQueue <- param |
|
|
|
continue |
|
|
|
} else if res.Query.TradeState == CODE_TRADE_NOTPAY || res.Query.TradeState == CODE_TRADE_USERPAYING { |
|
|
|
orderDelayQueue <- param |
|
|
|
continue |
|
|
|
} |
|
|
|
} else if param.Status == 3 { |
|
|
|
_, err = QueryRefund(param.OutTradeNo) |
|
|
|
} else if param.Order.Status == 3 { |
|
|
|
_, err = QueryRefund(param.Order.OutTradeNo) |
|
|
|
if err != nil { |
|
|
|
logger.Error("退款订单查询错误", zap.String("错误原因", err.Error()), |
|
|
|
zap.String("交易订单号", param.OutTradeNo)) |
|
|
|
zap.String("交易订单号", param.Order.OutTradeNo)) |
|
|
|
continue |
|
|
|
} |
|
|
|
} else { |
|
|
@ -180,7 +175,7 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) |
|
|
|
userOrder.IsDelete = false |
|
|
|
userOrder.CreatedAt = time.Now() |
|
|
|
userOrder.UpdatedAt = time.Now() |
|
|
|
if _, err = userOrder.AddUserOrder(); err != nil { |
|
|
|
if _, err = models.Add(userOrder); err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
@ -189,7 +184,7 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) |
|
|
|
pac := "prepay_id=" + resp.PrepayId |
|
|
|
paySign := core2.JsapiSign(client.AppId(), timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) |
|
|
|
|
|
|
|
go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) |
|
|
|
go PutOrderDelayQueue(userOrder, 0, 0) |
|
|
|
return map[string]interface{}{ |
|
|
|
"appid": Appid, |
|
|
|
"timestamp": timestamp, |
|
|
@ -217,7 +212,7 @@ func ReOrder(outTradeNo string) (map[string]interface{}, error) { |
|
|
|
//获取H5支付需要的paySign
|
|
|
|
pac := "prepay_id=" + userOrder.PrepayId |
|
|
|
paySign := core2.JsapiSign(Appid, timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) |
|
|
|
go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) |
|
|
|
go PutOrderDelayQueue(userOrder, 0, 0) |
|
|
|
return map[string]interface{}{ |
|
|
|
"appid": Appid, |
|
|
|
"timestamp": timestamp, |
|
|
@ -404,7 +399,7 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) // 退款查询
|
|
|
|
PutOrderDelayQueue(userOrder, 0, 0) // 退款查询
|
|
|
|
return res, nil |
|
|
|
} |
|
|
|
|
|
|
|