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

72 lines
1.6 KiB

5 years ago
  1. package pay_service
  2. import (
  3. "crypto/md5"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/hex"
  7. "encoding/xml"
  8. "errors"
  9. "fmt"
  10. "github.com/iGoogle-ink/gopay/v2"
  11. "hudongzhuanjia/libs/wx"
  12. "strings"
  13. )
  14. func ClientExtend(bm gopay.BodyMap, url string) ([]byte, error) {
  15. //bm.Set("appid", wx.Appid)
  16. //bm.Set("mch_id", wx.Mchid)
  17. h := md5.New()
  18. h.Write([]byte(bm.EncodeWeChatSignParams(wx.ApiKey)))
  19. bm.Set("sign", strings.ToUpper(hex.EncodeToString(h.Sum(nil))))
  20. certP12, err := Asset("cacert/apiclient_cert.p12")
  21. if err != nil {
  22. return nil, err
  23. }
  24. certPem, err := Asset("cacert/apiclient_cert.pem")
  25. if err != nil {
  26. return nil, err
  27. }
  28. keyPem, err := Asset("cacert/apiclient_key.pem")
  29. if err != nil {
  30. return nil, err
  31. }
  32. pkcsPool := x509.NewCertPool()
  33. pkcsPool.AppendCertsFromPEM(certP12)
  34. certificate, err := tls.X509KeyPair(certPem, keyPem)
  35. if err != nil {
  36. return nil, fmt.Errorf("tls.LoadX509KeyPair:%s", err)
  37. }
  38. tlsConfig := &tls.Config{
  39. Certificates: []tls.Certificate{certificate},
  40. RootCAs: pkcsPool,
  41. InsecureSkipVerify: true}
  42. httpClient := gopay.NewHttpClient()
  43. httpClient.SetTLSConfig(tlsConfig)
  44. bs, err := xml.Marshal(bm)
  45. if err != nil {
  46. return nil, err
  47. }
  48. res, bs, errs := httpClient.Type(gopay.TypeXML).Post(url).SendString(string(bs)).EndBytes()
  49. if len(errs) > 0 {
  50. return nil, fmt.Errorf("http client return error: %v", errs[0])
  51. }
  52. if res.StatusCode != 200 {
  53. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  54. }
  55. if strings.Contains(string(bs), "HTML") || strings.Contains(string(bs), "html") {
  56. return nil, errors.New(string(bs))
  57. }
  58. return bs, nil
  59. }