diff --git a/library.go b/library.go index 5f87300f..33f9d55b 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.159" + return "1.0.160" } diff --git a/service/wechatpayapiv2/api.go b/service/wechatpayapiv2/api.go new file mode 100644 index 00000000..61909ce7 --- /dev/null +++ b/service/wechatpayapiv2/api.go @@ -0,0 +1,16 @@ +package wechatpayapiv2 + +import ( + "context" + "crypto/tls" + "github.com/dtapps/go-library/utils/gorequest" +) + +func (c *Client) Post(ctx context.Context, _method string, certStatus bool, cert *tls.Certificate, notMustParams ...gorequest.Params) ([]byte, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+_method, params, true, cert) + // 定义 + return request.ResponseBody, err +} diff --git a/service/wechatpayapiv2/cert.go b/service/wechatpayapiv2/cert.go new file mode 100644 index 00000000..11c9b5d2 --- /dev/null +++ b/service/wechatpayapiv2/cert.go @@ -0,0 +1,8 @@ +package wechatpayapiv2 + +import "crypto/tls" + +func GetP12ToPem(cert, key string) (*tls.Certificate, error) { + pemCert, err := tls.X509KeyPair([]byte(cert), []byte(key)) + return &pemCert, err +} diff --git a/service/wechatpayapiv2/get.sign.go b/service/wechatpayapiv2/get.sign.go new file mode 100644 index 00000000..e31fccde --- /dev/null +++ b/service/wechatpayapiv2/get.sign.go @@ -0,0 +1,39 @@ +package wechatpayapiv2 + +import ( + "bytes" + "fmt" + "github.com/dtapps/go-library/utils/gomd5" + "sort" + "strings" +) + +// GetSign 获取签名 +func GetSign(param map[string]interface{}, key string) string { + sortString := getSortString(param) + sign := gomd5.Md5(sortString + "&key=" + key) + return strings.ToUpper(sign) +} + +// 支付字符串拼接 +func getSortString(m map[string]interface{}) string { + var buf bytes.Buffer + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + vs := m[k] + if vs == "" { + continue + } + if buf.Len() > 0 { + buf.WriteByte('&') + } + buf.WriteString(k) + buf.WriteByte('=') + buf.WriteString(fmt.Sprintf("%v", vs)) + } + return buf.String() +} diff --git a/service/wechatpayapiv2/request.go b/service/wechatpayapiv2/request.go index ea3a093c..0f1afc31 100644 --- a/service/wechatpayapiv2/request.go +++ b/service/wechatpayapiv2/request.go @@ -14,9 +14,6 @@ func (c *Client) request(ctx context.Context, url string, params map[string]inte // 设置请求地址 client.SetUri(url) - client.SetHeader("app_id", c.GetAppId()) - client.SetHeader("mch_id", c.GetMchId()) - // 设置格式 client.SetContentTypeXml()