|
|
@ -18,32 +18,26 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
func init() { |
|
|
|
//go loopUnifiedOrder()
|
|
|
|
go loopUnifiedOrder() |
|
|
|
} |
|
|
|
|
|
|
|
var orderDelayQueue = make(chan *OrderDelayQueueParam, math.MaxInt8) |
|
|
|
|
|
|
|
type OrderDelayQueueParam struct { |
|
|
|
Order *models.UserOrder |
|
|
|
First bool `json:"first"` |
|
|
|
Expires int64 `json:"expires"` |
|
|
|
Delay int `json:"delay"` |
|
|
|
Order *models.UserOrder |
|
|
|
First bool `json:"first"` |
|
|
|
Delay int `json:"delay"` |
|
|
|
} |
|
|
|
|
|
|
|
func PutOrderDelayQueue(order *models.UserOrder, expires int64, delay int) { |
|
|
|
if expires == 0 { |
|
|
|
expires = time.Now().Add(12 * time.Hour).Unix() // 2 个小时
|
|
|
|
} |
|
|
|
|
|
|
|
func PutOrderDelayQueue(order *models.UserOrder, delay int) { |
|
|
|
if delay == 0 { |
|
|
|
delay = 0 |
|
|
|
} |
|
|
|
|
|
|
|
orderDelayQueue <- &OrderDelayQueueParam{ |
|
|
|
First: true, |
|
|
|
Expires: expires, |
|
|
|
Delay: delay, |
|
|
|
Order: order, |
|
|
|
First: true, |
|
|
|
Delay: delay, |
|
|
|
Order: order, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -54,12 +48,7 @@ func loopUnifiedOrder() { |
|
|
|
} |
|
|
|
|
|
|
|
for _, order := range orders { |
|
|
|
expire, err := core2.ParseTime(order.TimeExpire) |
|
|
|
if err != nil { |
|
|
|
expire = time.Now().Add(12 * time.Hour) |
|
|
|
} |
|
|
|
|
|
|
|
PutOrderDelayQueue(order, expire.Add(12*time.Hour).Unix(), 0) |
|
|
|
PutOrderDelayQueue(order, 0) |
|
|
|
} |
|
|
|
|
|
|
|
defer func() { |
|
|
@ -76,7 +65,7 @@ func loopUnifiedOrder() { |
|
|
|
if !ok { |
|
|
|
panic("通道异常关闭") |
|
|
|
} |
|
|
|
if param.Expires <= time.Now().Unix() { |
|
|
|
if param.Order.ExpireAt <= time.Now().Unix() { |
|
|
|
if param.Order.Status == 0 { |
|
|
|
order := new(models.UserOrder) |
|
|
|
_, err = order.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 7) |
|
|
@ -130,29 +119,26 @@ func loopUnifiedOrder() { |
|
|
|
|
|
|
|
const CallbackOrderUrl = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackOrder" |
|
|
|
|
|
|
|
func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) (map[string]interface{}, error) { |
|
|
|
func UnifiedOrder(content, openId string, fee, goodType, userId, activityId, expireAt int64) (map[string]interface{}, error) { |
|
|
|
client, err := Client() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
now := time.Now() |
|
|
|
timeStart := core2.FormatTime(now) |
|
|
|
timeExpire := core2.FormatTime(now.Add(12 * time.Hour)) |
|
|
|
outTradeNo := utils.RandomStr(32) |
|
|
|
nonceStr := utils.RandomStr(32) |
|
|
|
resp, err := pay.UnifiedOrder2(client, &pay.UnifiedOrderRequest{ |
|
|
|
Body: body, |
|
|
|
OutTradeNo: outTradeNo, |
|
|
|
TotalFee: fee, |
|
|
|
NotifyURL: CallbackOrderUrl, |
|
|
|
TradeType: "JSAPI", |
|
|
|
DeviceInfo: "WEB", |
|
|
|
NonceStr: nonceStr, |
|
|
|
SignType: core2.SignType_MD5, |
|
|
|
TimeStart: now, |
|
|
|
OpenId: openid, |
|
|
|
}) |
|
|
|
body := make(map[string]string, 0) |
|
|
|
body["body"] = content |
|
|
|
body["out_trade_no"] = outTradeNo |
|
|
|
body["total_fee"] = fmt.Sprint(fee) |
|
|
|
body["notify_url"] = CallbackOrderUrl |
|
|
|
body["trade_type"] = "JSAPI" |
|
|
|
body["device_info"] = "WEB" |
|
|
|
body["nonce_str"] = nonceStr |
|
|
|
body["sign_type"] = core2.SignType_MD5 |
|
|
|
body["open_id"] = openId |
|
|
|
|
|
|
|
resp, err := pay.UnifiedOrder(client, body) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -160,32 +146,28 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) |
|
|
|
// 记录这次订单
|
|
|
|
userOrder := new(models.UserOrder) |
|
|
|
userOrder.DeviceInfo = "WEB" |
|
|
|
userOrder.Body = body |
|
|
|
userOrder.Body = content |
|
|
|
userOrder.UserId = userId |
|
|
|
userOrder.ActivityId = activityId |
|
|
|
userOrder.FeeType = "CNY" |
|
|
|
userOrder.GoodType = goodType |
|
|
|
userOrder.OpenId = openid |
|
|
|
userOrder.OpenId = openId |
|
|
|
userOrder.OutTradeNo = outTradeNo |
|
|
|
userOrder.TimeStart = timeStart |
|
|
|
userOrder.TimeExpire = timeExpire |
|
|
|
userOrder.ExpireAt = expireAt |
|
|
|
userOrder.TotalFee = fee |
|
|
|
userOrder.TradeType = "JSAPI" |
|
|
|
userOrder.PrepayId = resp.PrepayId |
|
|
|
userOrder.PrepayId = resp["prepay_id"] |
|
|
|
userOrder.Status = 0 |
|
|
|
userOrder.IsDelete = false |
|
|
|
userOrder.CreatedAt = time.Now() |
|
|
|
userOrder.UpdatedAt = time.Now() |
|
|
|
if _, err = models.Add(userOrder); err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
timestamp := strconv.FormatInt(time.Now().Unix(), 10) |
|
|
|
//获取H5支付需要的paySign
|
|
|
|
pac := "prepay_id=" + resp.PrepayId |
|
|
|
timestamp := strconv.FormatInt(time.Now().Unix(), 10) |
|
|
|
pac := "prepay_id=" + resp["prepay_id"] |
|
|
|
paySign := core2.JsapiSign(client.AppId(), timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) |
|
|
|
|
|
|
|
go PutOrderDelayQueue(userOrder, 0, 0) |
|
|
|
go PutOrderDelayQueue(userOrder, 0) |
|
|
|
return map[string]interface{}{ |
|
|
|
"appid": Appid, |
|
|
|
"timestamp": timestamp, |
|
|
@ -213,7 +195,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, 0, 0) |
|
|
|
go PutOrderDelayQueue(userOrder, 0) |
|
|
|
return map[string]interface{}{ |
|
|
|
"appid": Appid, |
|
|
|
"timestamp": timestamp, |
|
|
@ -283,7 +265,6 @@ func NotifyOrder(w io.Reader) (order *models.UserOrder, err error) { |
|
|
|
|
|
|
|
if res.ResultCode == CODE_SUCCESS && res.TradeType == CODE_TRADE_SUCCESS { |
|
|
|
userOrder := new(models.UserOrder) |
|
|
|
userOrder.TimeEnd = res.TimeEnd |
|
|
|
userOrder.TransactionId = res.TransactionId |
|
|
|
userOrder.Status = 1 |
|
|
|
_, err = userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1) |
|
|
@ -337,13 +318,10 @@ func OrderQuery(outTradeNo string) (*OrderQueryResult, error) { |
|
|
|
}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
userOrder.ErrMsg = err.Error() |
|
|
|
userOrder.UpdateErrByOutTradeNo(outTradeNo) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
userOrder.TransactionId = res.TransactionId |
|
|
|
userOrder.TimeEnd = core2.FormatTime(res.TimeEnd) |
|
|
|
switch res.TradeState { |
|
|
|
case CODE_TRADE_SUCCESS: |
|
|
|
userOrder.Status = 1 |
|
|
@ -413,8 +391,6 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { |
|
|
|
}) |
|
|
|
//
|
|
|
|
if err != nil { |
|
|
|
userOrder.ErrMsg = err.Error() |
|
|
|
userOrder.UpdateErrByOutTradeNo(outTradeNo) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
userOrder.Status = 3 |
|
|
@ -423,7 +399,7 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
PutOrderDelayQueue(userOrder, 0, 0) // 退款查询
|
|
|
|
PutOrderDelayQueue(userOrder, 0) // 退款查询
|
|
|
|
return res, nil |
|
|
|
} |
|
|
|
|
|
|
@ -450,14 +426,11 @@ func QueryRefund(outTradeNo string) (*QueryRefundResult, error) { |
|
|
|
}) |
|
|
|
//请求申请退款
|
|
|
|
if err != nil { |
|
|
|
userOrder.ErrMsg = err.Error() |
|
|
|
userOrder.UpdateErrByOutTradeNo(outTradeNo) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
userOrder.RefundAccount = res.RefundList[0].RefundAccount |
|
|
|
userOrder.RefundRecvAccount = res.RefundList[0].RefundRecvAccout |
|
|
|
userOrder.SuccessTime = res.RefundList[0].RefundSuccessTime |
|
|
|
userOrder.Status = 4 |
|
|
|
_, err = userOrder.UpdateRefundByOutTradeNo(outTradeNo) |
|
|
|
if err != nil { |
|
|
|