提交 8f089672 authored 作者: 屈传平's avatar 屈传平

Merge branch 'featrue/hcgyl'

...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
const pkgName = "hcw" const pkgName = "hcw"
type Config struct { type Config struct {
AppId string
SecretKey string SecretKey string
ApiUrl string ApiUrl string
} }
...@@ -165,9 +166,9 @@ func (s *HcwClient) post(ctx context.Context, method string, req interface{}) (r ...@@ -165,9 +166,9 @@ func (s *HcwClient) post(ctx context.Context, method string, req interface{}) (r
Url := s.Config.ApiUrl + method Url := s.Config.ApiUrl + method
Request := g.Client() Request := g.Client()
Request.SetHeader("Content-Type", "application/json") Request.SetHeader("Content-Type", "application/json;charset=UTF-8")
Request.SetHeader("Format", "json")
Request.SetHeader("signature", signature) Request.SetHeader("signature", signature)
Request.SetHeader("appid", s.Config.AppId)
resp, err := Request.Timeout(time.Second*10).Post(Url, param.MustToJsonString()) resp, err := Request.Timeout(time.Second*10).Post(Url, param.MustToJsonString())
defer func() { defer func() {
...@@ -196,9 +197,9 @@ func (s *HcwClient) get(ctx context.Context, method string, req interface{}) (re ...@@ -196,9 +197,9 @@ func (s *HcwClient) get(ctx context.Context, method string, req interface{}) (re
query := mapToSortedQuery(queryParams) query := mapToSortedQuery(queryParams)
Url := s.Config.ApiUrl + method + "?" + query Url := s.Config.ApiUrl + method + "?" + query
Request := g.Client() Request := g.Client()
Request.SetHeader("Content-Type", "text/xml; charset=utf-8") Request.SetHeader("Content-Type", "application/json;charset=UTF-8")
Request.SetHeader("Format", "json")
Request.SetHeader("signature", signature) Request.SetHeader("signature", signature)
Request.SetHeader("appid", s.Config.AppId)
resp, err := Request.Timeout(time.Second * 10).Get(Url) resp, err := Request.Timeout(time.Second * 10).Get(Url)
defer func() { defer func() {
......
...@@ -2,7 +2,9 @@ package hcw ...@@ -2,7 +2,9 @@ package hcw
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/util/gconv"
) )
//notify_id string 唯一请求ID(用于幂等校验)示例:20240320140000_123456 //notify_id string 唯一请求ID(用于幂等校验)示例:20240320140000_123456
...@@ -32,19 +34,25 @@ type OrderNotifyData struct { ...@@ -32,19 +34,25 @@ type OrderNotifyData struct {
// 回调 // 回调
func (s *HcwClient) CallBack(r *ghttp.Request) (res *OrderNotifyRes, err error) { func (s *HcwClient) CallBack(r *ghttp.Request) (res *OrderNotifyRes, err error) {
body, err := CheckSign(r) body := r.GetBodyString()
if nil != err {
return
}
if body == "" { if body == "" {
return return
} }
err = json.Unmarshal([]byte(body), &res) err = json.Unmarshal([]byte(body), &res)
if nil != err {
return
}
err = s.CheckSign(res)
return return
} }
func CheckSign(r *ghttp.Request) (body string, err error) { func (s *HcwClient) CheckSign(req *OrderNotifyRes) (err error) {
body = r.GetBodyString() reqMap := gconv.Map(req.Data)
signature, err := s.EncryptWithSignature(reqMap)
if req.Signature != signature {
err = errors.New("incorrect signature")
}
return return
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论