互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package common
  2. import (
  3. "crypto/sha1"
  4. "encoding/xml"
  5. "fmt"
  6. "go.uber.org/atomic"
  7. "hudongzhuanjia/controllers"
  8. "hudongzhuanjia/libs/filter"
  9. "hudongzhuanjia/logger"
  10. pay_service "hudongzhuanjia/services/pay"
  11. "hudongzhuanjia/utils/define"
  12. "os"
  13. "time"
  14. )
  15. var Now = time.Now()
  16. type WeChatOauthCtl struct {
  17. controllers.BaseCtl
  18. }
  19. const token = "hd_oauth"
  20. // 验证服务器资源配置
  21. func (t *WeChatOauthCtl) Oauth() {
  22. signature := t.MustGet("signature")
  23. timestamp := t.MustGet("timestamp")
  24. nonce := t.MustGet("nonce")
  25. echoStr := t.MustGet("echostr")
  26. tokenStr := fmt.Sprintf("%s%s%s", token, timestamp, nonce)
  27. s := fmt.Sprintf("%s", sha1.New().Sum([]byte(tokenStr)))
  28. if s == signature {
  29. t.OriginResponseWriter.Write([]byte(echoStr))
  30. }
  31. t.OriginResponseWriter.Write([]byte("failed"))
  32. }
  33. func (t *WeChatOauthCtl) Checkin() {
  34. content, _ := t.Get("content")
  35. path, _ := os.Getwd()
  36. t.JSON(map[string]interface{}{
  37. "success": "你好, tommy黄梓健",
  38. "version": Now,
  39. "wd": path,
  40. "content": filter.Replace(content),
  41. })
  42. }
  43. //
  44. //func init() {
  45. // ticker := time.NewTicker(5 * time.Second)
  46. // go func() {
  47. // fmt.Println("循环查询订单数据")
  48. // for range ticker.C {
  49. // pay_service.BatchQuery()
  50. // }
  51. // }()
  52. //
  53. //}
  54. type CDATA struct {
  55. Text string `xml:",cdata"`
  56. }
  57. type CallbackParam struct {
  58. XMLName xml.Name `xml:"xml"`
  59. ReturnCode CDATA `xml:"return_code"`
  60. ReturnMsg CDATA `xml:"return_msg"`
  61. }
  62. var orderSuccessCounter = new(atomic.Int64)
  63. var orderFailedCounter = new(atomic.Int64)
  64. var refundSuccessCounter = new(atomic.Int64)
  65. var refundFailedCounter = new(atomic.Int64)
  66. func (t *WeChatOauthCtl) CallbackOrder() {
  67. //搜索支付的order表, 查找到某条记录
  68. _, err := pay_service.NotifyOrder(t.Request.BODY)
  69. param := new(CallbackParam)
  70. if err != nil {
  71. logger.Error(err)
  72. orderFailedCounter.Add(1)
  73. logger.Error(fmt.Sprintf("call order失败微信响应次数: %v", orderFailedCounter.Load()))
  74. param.ReturnCode = CDATA{Text: define.CODE_FAIL}
  75. param.ReturnMsg = CDATA{Text: err.Error()}
  76. } else {
  77. orderSuccessCounter.Add(1)
  78. logger.Error(fmt.Sprintf("call order成功微信响应次数: %v", orderSuccessCounter.Load()))
  79. param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
  80. param.ReturnMsg = CDATA{Text: "OK"}
  81. }
  82. xmlRes, _ := xml.Marshal(param)
  83. if t.Request.OriginRequest.Method == "GET" {
  84. t.JSON(map[string]interface{}{
  85. "time": time.Now(),
  86. "xml": param,
  87. })
  88. return
  89. }
  90. t.XML(xmlRes)
  91. }
  92. func (t *WeChatOauthCtl) CallbackRefund() {
  93. _, err := pay_service.NotifyRefund(t.Request.BODY)
  94. param := new(CallbackParam)
  95. if err != nil {
  96. logger.Error(err)
  97. refundFailedCounter.Add(1)
  98. logger.Error(fmt.Sprintf("call refund失败微信响应次数: %v", refundFailedCounter.Load()))
  99. param.ReturnCode = CDATA{Text: define.CODE_FAIL}
  100. param.ReturnMsg = CDATA{Text: err.Error()}
  101. } else {
  102. refundSuccessCounter.Add(1)
  103. logger.Error(fmt.Sprintf("call refund成功微信响应次数: %v", refundSuccessCounter.Load()))
  104. param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
  105. param.ReturnMsg = CDATA{Text: "OK"}
  106. }
  107. xmlRes, _ := xml.Marshal(param)
  108. if t.Request.OriginRequest.Method == "GET" {
  109. t.JSON(map[string]interface{}{
  110. "time": time.Now(),
  111. "xml": param,
  112. })
  113. }
  114. t.XML(xmlRes)
  115. }