|
|
package pay_service
import ( "crypto/md5" "crypto/tls" "crypto/x509" "encoding/hex" "encoding/xml" "errors" "fmt" "github.com/iGoogle-ink/gopay/v2" "hudongzhuanjia/libs/wx" "strings" )
func ClientExtend(bm gopay.BodyMap, url string) ([]byte, error) {
//bm.Set("appid", wx.Appid)
//bm.Set("mch_id", wx.Mchid)
h := md5.New() h.Write([]byte(bm.EncodeWeChatSignParams(wx.ApiKey))) bm.Set("sign", strings.ToUpper(hex.EncodeToString(h.Sum(nil))))
certP12, err := Asset("cacert/apiclient_cert.p12") if err != nil { return nil, err } certPem, err := Asset("cacert/apiclient_cert.pem") if err != nil { return nil, err } keyPem, err := Asset("cacert/apiclient_key.pem") if err != nil { return nil, err }
pkcsPool := x509.NewCertPool() pkcsPool.AppendCertsFromPEM(certP12) certificate, err := tls.X509KeyPair(certPem, keyPem) if err != nil { return nil, fmt.Errorf("tls.LoadX509KeyPair:%s", err) }
tlsConfig := &tls.Config{ Certificates: []tls.Certificate{certificate}, RootCAs: pkcsPool, InsecureSkipVerify: true}
httpClient := gopay.NewHttpClient() httpClient.SetTLSConfig(tlsConfig)
bs, err := xml.Marshal(bm) if err != nil { return nil, err }
res, bs, errs := httpClient.Type(gopay.TypeXML).Post(url).SendString(string(bs)).EndBytes()
if len(errs) > 0 { return nil, fmt.Errorf("http client return error: %v", errs[0]) }
if res.StatusCode != 200 { return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode) }
if strings.Contains(string(bs), "HTML") || strings.Contains(string(bs), "html") { return nil, errors.New(string(bs)) } return bs, nil }
|