From 3d4d3844fd3483afec6cedb53035233db7598e3c Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Fri, 8 May 2020 11:35:21 +0800 Subject: [PATCH] pay --- controllers/common/wechat_oauth.go | 20 ++++++++++++++++++-- services/pay/handle.go | 4 ++-- services/pay/order.go | 29 ++++++++++++++++++++--------- services/pay/transfer.go | 9 +++++++-- test/config_test.go | 2 +- test/pay_test.go | 3 ++- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/controllers/common/wechat_oauth.go b/controllers/common/wechat_oauth.go index 63ecbf5..12b1aec 100644 --- a/controllers/common/wechat_oauth.go +++ b/controllers/common/wechat_oauth.go @@ -7,6 +7,8 @@ import ( "go.uber.org/atomic" "hudongzhuanjia/controllers" "hudongzhuanjia/libs/filter" + "hudongzhuanjia/logger" + pay_service "hudongzhuanjia/services/pay" "os" "time" ) @@ -70,8 +72,21 @@ var counter = new(atomic.Int64) func (t *WeChatOauthCtl) CallbackOrder() { counter.Add(1) - // 搜索支付的order表, 查找到某条记录 - //t.CheckErr(pay_service.NotifyOrder(t.Request.OriginRequest)) + //搜索支付的order表, 查找到某条记录 + order, err := pay_service.NotifyOrder(t.Request.OriginRequest.Body) + if err != nil { + logger.Error(fmt.Sprintf("出现错误: %v", err)) + return + } + err = pay_service.HandleSuccess(&pay_service.OrderDelayQueueParam{ + Order: order, + }) + + if err != nil { + logger.Error(fmt.Sprintf("出现错误: %v", err)) + return + } + param := new(CallbackParam) param.ReturnCode = CDATA{Text: "SUCCESS"} param.ReturnMsg = CDATA{Text: "OK"} @@ -82,6 +97,7 @@ func (t *WeChatOauthCtl) CallbackOrder() { "time": time.Now(), }) } + logger.Error(fmt.Sprintf("微信响应次数: %v", counter.Load())) t.XML(xmlRes) } diff --git a/services/pay/handle.go b/services/pay/handle.go index 059fee1..49efa5a 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -14,7 +14,7 @@ import ( //处理支付成功之后的回调问题 // 直播红包 -func HandleSuccess(param *orderDelayQueueParam) (err error) { +func HandleSuccess(param *OrderDelayQueueParam) (err error) { var exist bool if param.Order.GoodType == 4 { // 直播商品 customerOrder := new(models.CustomerOrder) @@ -142,7 +142,7 @@ func HandleSuccess(param *orderDelayQueueParam) (err error) { return } -func HandleFailed(param *orderDelayQueueParam) (err error) { +func HandleFailed(param *OrderDelayQueueParam) (err error) { var exist bool // 退还库存 if param.Order.GoodType == 4 { diff --git a/services/pay/order.go b/services/pay/order.go index a07d82c..437954c 100644 --- a/services/pay/order.go +++ b/services/pay/order.go @@ -20,9 +20,9 @@ func init() { go loopUnifiedOrder() } -var orderDelayQueue = make(chan *orderDelayQueueParam, math.MaxInt8) +var orderDelayQueue = make(chan *OrderDelayQueueParam, math.MaxInt8) -type orderDelayQueueParam struct { +type OrderDelayQueueParam struct { Order *models.UserOrder First bool `json:"first"` Expires int64 `json:"expires"` @@ -35,10 +35,10 @@ func PutOrderDelayQueue(order *models.UserOrder, expires int64, delay int) { } if delay == 0 { - delay = 1000 + delay = 0 } - orderDelayQueue <- &orderDelayQueueParam{ + orderDelayQueue <- &OrderDelayQueueParam{ First: true, Expires: expires, Delay: delay, @@ -262,10 +262,13 @@ type NotifyRequest struct { TimeEnd string `xml:"time_end,omitempty" json:"time_end,omitempty"` } -func NotifyOrder(w io.Reader) (*models.UserOrder, error) { +func NotifyOrder(w io.Reader) (order *models.UserOrder, err error) { + var exist bool res := new(NotifyRequest) - if err := xml.NewDecoder(w).Decode(res); err != nil { - return nil, fmt.Errorf("xml.NewDecoder.Decode:%w", err) + err = xml.NewDecoder(w).Decode(res) + if err != nil { + err = fmt.Errorf("xml.NewDecoder.Decode:%w", err) + return } if res.ReturnCode != CODE_SUCCESS { @@ -277,12 +280,20 @@ func NotifyOrder(w io.Reader) (*models.UserOrder, error) { userOrder.TimeEnd = res.TimeEnd userOrder.TransactionId = res.TransactionId userOrder.Status = 1 - if _, err := userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1); err != nil { + _, err = userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1) + if err != nil { return nil, err } + exist, err = userOrder.GetByOutTradeNo(res.OutTradeNo) + if err == nil || !exist { + err = fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg) + return + } + return userOrder, nil } else { - return nil, fmt.Errorf("trade error, err_code: %v and err_code_des: %v", res.ErrCode, res.ErrCodeDes) + err = fmt.Errorf("trade error, err_code: %v and err_code_des: %v", res.ErrCode, res.ErrCodeDes) + return } } diff --git a/services/pay/transfer.go b/services/pay/transfer.go index a3631fe..3f400dc 100644 --- a/services/pay/transfer.go +++ b/services/pay/transfer.go @@ -20,6 +20,7 @@ func init() { var transferDelayQueue = make(chan *transferDelayQueueParam, math.MaxInt8) type transferDelayQueueParam struct { + first bool // 首次跳过 Retries int `json:"retries"` // 尝试次数 Delay int `json:"delay"` // 延迟时间, 单位second Amount int `json:"amount"` // 转账金额 @@ -38,6 +39,7 @@ func loopTransfer() { transferDelayQueue = make(chan *transferDelayQueueParam, math.MaxInt8) for _, transfer := range transfers { transferDelayQueue <- &transferDelayQueueParam{ + first: true, Retries: 5, Delay: 2 * 60, Amount: transfer.PaymentAmount, @@ -75,8 +77,10 @@ func loopTransfer() { param.Retries-- } - time.Sleep(time.Duration(param.Delay) * time.Second) - + if !param.first { + time.Sleep(time.Duration(param.Delay) * time.Second) + } + param.first = false res, err := TransferInfo(param.PartnerTradeNo) if err != nil { logger.Error("微信转账查询出现的错误", zap.String("错误原因", err.Error()), @@ -121,6 +125,7 @@ func PutTransferDelayQueue(desc, openId, partnerTradeNo string, amount, retries, delay = 30 } transferDelayQueue <- &transferDelayQueueParam{ + first: true, Retries: retries, Delay: delay, Amount: amount, diff --git a/test/config_test.go b/test/config_test.go index f8d22b6..72b3229 100644 --- a/test/config_test.go +++ b/test/config_test.go @@ -196,7 +196,7 @@ func TestDuration(t *testing.T) { //} func TestRefundRedPack(t *testing.T) { - res, err := pay_service.Refund("退款", "RpY6pmvbZy9yPP8kPEzF5H7s27E3kW3E") + res, err := pay_service.Refund("退款", "f8nYoKC5Gk0O5t68AzU3boo2eJfETKRf") fmt.Printf("%+v, %v", res, err) //res, err := pay_service.QueryRedPack("tDYW8edlzegSlVNaJMXsteZEeuVL") //fmt.Printf("%+v, %+v", res, err) diff --git a/test/pay_test.go b/test/pay_test.go index e112948..7e4b67d 100644 --- a/test/pay_test.go +++ b/test/pay_test.go @@ -7,6 +7,7 @@ import ( "hudongzhuanjia/logger" im_service "hudongzhuanjia/services/im" pay_service "hudongzhuanjia/services/pay" + "hudongzhuanjia/utils" "math" "testing" "time" @@ -15,7 +16,7 @@ import ( var openId = "o9XM41s_NN8Y0QK6_MbM-aYMV3TE" func TestTransfer(t *testing.T) { - res, err := pay_service.Transfer("欧轩互动-转账测试", "127.0.0.1", openId, 0.30*10) + res, err := pay_service.Transfer("欧轩互动-转账测试", openId, utils.RandomStr(32), 0.30*100) fmt.Println(err) fmt.Printf("%+v\n", res) }