互动
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

package common
import (
"crypto/sha1"
"encoding/xml"
"fmt"
"go.uber.org/atomic"
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/filter"
"hudongzhuanjia/logger"
pay_service "hudongzhuanjia/services/pay"
"hudongzhuanjia/utils/define"
"os"
"time"
)
var Now = time.Now()
type WeChatOauthCtl struct {
controllers.BaseCtl
}
const token = "hd_oauth"
// 验证服务器资源配置
func (t *WeChatOauthCtl) Oauth() {
signature := t.MustGet("signature")
timestamp := t.MustGet("timestamp")
nonce := t.MustGet("nonce")
echoStr := t.MustGet("echostr")
tokenStr := fmt.Sprintf("%s%s%s", token, timestamp, nonce)
s := fmt.Sprintf("%s", sha1.New().Sum([]byte(tokenStr)))
if s == signature {
t.OriginResponseWriter.Write([]byte(echoStr))
}
t.OriginResponseWriter.Write([]byte("failed"))
}
func (t *WeChatOauthCtl) Checkin() {
content, _ := t.Get("content")
path, _ := os.Getwd()
t.JSON(map[string]interface{}{
"success": "你好, tommy黄梓健",
"version": Now,
"wd": path,
"content": filter.Replace(content),
})
}
//
//func init() {
// ticker := time.NewTicker(5 * time.Second)
// go func() {
// fmt.Println("循环查询订单数据")
// for range ticker.C {
// pay_service.BatchQuery()
// }
// }()
//
//}
type CDATA struct {
Text string `xml:",cdata"`
}
type CallbackParam struct {
XMLName xml.Name `xml:"xml"`
ReturnCode CDATA `xml:"return_code"`
ReturnMsg CDATA `xml:"return_msg"`
}
var orderSuccessCounter = new(atomic.Int64)
var orderFailedCounter = new(atomic.Int64)
var refundSuccessCounter = new(atomic.Int64)
var refundFailedCounter = new(atomic.Int64)
func (t *WeChatOauthCtl) CallbackOrder() {
//搜索支付的order表, 查找到某条记录
_, err := pay_service.NotifyOrder(t.Request.BODY)
param := new(CallbackParam)
if err != nil {
logger.Error(err)
orderFailedCounter.Add(1)
logger.Error(fmt.Sprintf("call order失败微信响应次数: %v", orderFailedCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_FAIL}
param.ReturnMsg = CDATA{Text: err.Error()}
} else {
orderSuccessCounter.Add(1)
logger.Error(fmt.Sprintf("call order成功微信响应次数: %v", orderSuccessCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
param.ReturnMsg = CDATA{Text: "OK"}
}
xmlRes, _ := xml.Marshal(param)
if t.Request.OriginRequest.Method == "GET" {
t.JSON(map[string]interface{}{
"time": time.Now(),
"xml": param,
})
return
}
t.XML(xmlRes)
}
func (t *WeChatOauthCtl) CallbackRefund() {
_, err := pay_service.NotifyRefund(t.Request.BODY)
param := new(CallbackParam)
if err != nil {
logger.Error(err)
refundFailedCounter.Add(1)
logger.Error(fmt.Sprintf("call refund失败微信响应次数: %v", refundFailedCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_FAIL}
param.ReturnMsg = CDATA{Text: err.Error()}
} else {
refundSuccessCounter.Add(1)
logger.Error(fmt.Sprintf("call refund成功微信响应次数: %v", refundSuccessCounter.Load()))
param.ReturnCode = CDATA{Text: define.CODE_SUCCESS}
param.ReturnMsg = CDATA{Text: "OK"}
}
xmlRes, _ := xml.Marshal(param)
if t.Request.OriginRequest.Method == "GET" {
t.JSON(map[string]interface{}{
"time": time.Now(),
"xml": param,
})
}
t.XML(xmlRes)
}