第三方api接口
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.

165 lines
7.7 KiB

  1. package card_pay
  2. import (
  3. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/x509"
  8. "encoding/base64"
  9. "fmt"
  10. "golang.org/x/crypto/ssh"
  11. mathRand "math/rand"
  12. "time"
  13. )
  14. const (
  15. PrivateKeyStr = "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCVaPSBsm6j+kRm\n4v+oy2b6hn3xt1czBcgUGhy00i97fccfOZI42FXr1dF+Y5EKefvcPSjiJZHezQyb\n/e6D9DIATF95r71cyLbmbxzb4PaDkVUuHYM1XeQSkTAD6kp3KAN8q6svxWYRkBaj\nq0CZ359zT+Py8lKkfCXpj6mx6XlUrbmORPaRTu9m0V08Mg+hCOXiPXXLCxBpx+Sj\nWHSvqgxqqo5R+Ks7Gjqtb0UJs0xE43w3Iuwpo4If/Bv4ScZ25RyhvL8v30x8kp2H\nI8z/BgXom6OrpMp/RBr27asXS/2TbIUFr3uLgG4mAysy/JYRIQU/mzneH59/7vRU\n8lt8C44TAgMBAAECggEAeRTYaa8nuN3dxx+ZHrrQFqEdueDh2uiJj7jloKhhcIuq\n7iQi2sx/PSNHpkycQ0g+EurBkhvuTggQeu5b+DXSu6kpCp2gMWw7xR0w3wQ94rvJ\nsxiZt4sChQWCSvn5JP2ZbdRVY95ubDdAEZ18Ofce6j1JNx6oWMJ/mvshA5x5BmSS\nZ7VJ/2MSLwknTWAzzEDdRpm9mDrF7paDSCtVnBjfUyhkrh5FkswFGbX7Jtg9TLP6\n3g1+Gy9Q0bi0khaOa4eUTmUdy1fKun2rUqbgo0CTO3pvvWX7TkSxcP2Mz+Rmpv1Z\nFNScT32+5G9yu1klYKQxztVf4sdRRDuQC/l5ZbiACQKBgQDE4ZnRHAcHb8jUdKH3\nIsoABLmObTJlN8MprXFYSz3YCwCbv/C8Nd3YboApUZBMNq/uTcbH2WS7fITtWaPq\n4xzfe5dL14gDSfRMyX6y/jY0Mi+EjVsZfB5RHtaxPh/aEC1dEtQZeCVPvq/Xim7C\nRPLKL3wCK1XlIy/sly6LJCHnZwKBgQDCRjRXkcMyUHziiWo7phIo+mC9gRRYdOyO\n5FhTiq1sWWsam8XLOKYh3rXIl8DhtphG736CI10AskZx2fQl5xe6UnhHNIyHQUxr\nBy3ncdAjyfR4qDxHT65aTcgygt9XpRpg3kgOZzJFfSdwZFhvvP/VzChhxbI9TCgB\ndoHG2glUdQKBgQCf9egiZNI1MZaLP2Pkxmg20Z/tXz0lzcOMVjYUFbXelTneUXmH\ngYCIe2c3quRBd+FDDgAmmbNlPQtHSKCMlA/+Z40KesgwW+LYjJGbXj2t9OR0nuDY\neOas5BBtG1uHscMZrtWTB/HQs85sojXRM3HL4f6wSINWU1t5r2+Kb0X4mwKBgH8O\nx0DWdiE7/tFyKeZlEM94ITkPaK0Jm4VA1nuc4RtRx/iZFF166TyRmbZdeaqlbyZF\nkCmw0yyXa/ceIs1/PEeBlGSNe7MiA8aj5WPPTd96P8eB0BqbhF5PLLXMXiHGO2BL\nQDkbSQeleNvK5QP/Shsb8PUxBQeN/UQnq2zQQPXZAoGBALcEWSE0pjJTI36isHPz\n005GVe4TTyIzXDQ24I8JP7nMg+Z+zyjcAePmcg49vvmMD32rrSynb+CqeL8RUD3N\nkDU1KZ8HgKOOapub1YP6KqWfKX5nN1id70voVefNeKxnOcyk09KNT32TTguo6WQs\nDWxJI9MWHEeoHXLOT5K0FVpP\n-----END PRIVATE KEY-----"
  16. SignNo = "562265003122220003"
  17. CustomerCode = "562265003122220"
  18. PublicKeyStr = "MIID0jCCArqgAwIBAgIJAP7Hut/02gF6MA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV\nBAYTAkdaMQswCQYDVQQIDAJHRDELMAkGA1U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VLAbaLY1l0CGouNmrkzT\n15CxLnB29m0Xq3V+YsLfDV5ohUNtbFX3NrBUXSHUUf5n8YWqJWTQl2TkeMoYAJbs\nWSdkZGNB5KGDUORRPjabpEs2+0+uGHgVr3Llxc3rEVgLSX0wFe1ytLbIW0ibmQV+\ntpiZYSSfN6lzzX9NnQk3zGLv+UfIag=="
  19. )
  20. func CardPaySend(method string, keyArr map[string]string, param map[string]string) (string, error) {
  21. url := "http://test-efps.epaylinks.cn/" + method
  22. //https://efps.epaylinks.cn/
  23. for key, value := range param {
  24. if value == "" {
  25. delete(param, key)
  26. }
  27. }
  28. param["customerCode"] = keyArr["customerCode"]
  29. param["nonceStr"] = randomString(32)
  30. data := zhios_third_party_utils.SerializeStr(param)
  31. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  32. if err != nil {
  33. return "", err
  34. }
  35. headers := map[string]string{
  36. "Content-Type": "application/json",
  37. "x-efps-sign-no": keyArr["signNo"],
  38. "x-efps-sign-type": "SHA256withRSA",
  39. "x-efps-sign": sign,
  40. "x-efps-timestamp": time.Now().Format("20060102150405"),
  41. }
  42. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  43. zhios_third_party_utils.CurlDebug = true
  44. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  45. if err != nil {
  46. return "", err
  47. }
  48. fmt.Println(string(res))
  49. return string(res), nil
  50. }
  51. func CardPaySendSecond(method string, keyArr map[string]string, param map[string]interface{}) (string, error) {
  52. url := "http://test-efps.epaylinks.cn/" + method
  53. //https://efps.epaylinks.cn/
  54. for key, value := range param {
  55. if value == "" {
  56. delete(param, key)
  57. }
  58. }
  59. param["customerCode"] = keyArr["customerCode"]
  60. param["nonceStr"] = randomString(32)
  61. data := zhios_third_party_utils.SerializeStr(param)
  62. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  63. if err != nil {
  64. return "", err
  65. }
  66. headers := map[string]string{
  67. "Content-Type": "application/json",
  68. "x-efps-sign-no": keyArr["signNo"],
  69. "x-efps-sign-type": "SHA256withRSA",
  70. "x-efps-sign": sign,
  71. "x-efps-timestamp": time.Now().Format("20060102150405"),
  72. }
  73. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  74. zhios_third_party_utils.CurlDebug = true
  75. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  76. if err != nil {
  77. return "", err
  78. }
  79. fmt.Println(string(res))
  80. return string(res), nil
  81. }
  82. func CardPayApplySend(method string, keyArr map[string]string, param map[string]string) (string, error) {
  83. url := "http://test-efps.epaylinks.cn/" + method
  84. //https://efps.epaylinks.cn/
  85. for key, value := range param {
  86. if value == "" {
  87. delete(param, key)
  88. }
  89. }
  90. data := zhios_third_party_utils.SerializeStr(param)
  91. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  92. if err != nil {
  93. return "", err
  94. }
  95. headers := map[string]string{
  96. "Content-Type": "application/json",
  97. "x-efps-sign-no": keyArr["signNo"],
  98. "x-efps-sign-type": "SHA256withRSA",
  99. "x-efps-sign": sign,
  100. "x-efps-timestamp": time.Now().Format("20060102150405"),
  101. }
  102. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  103. zhios_third_party_utils.CurlDebug = true
  104. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  105. if err != nil {
  106. return "", err
  107. }
  108. fmt.Println(string(res))
  109. return string(res), nil
  110. }
  111. func randomString(ln int) string {
  112. letters := []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  113. b := make([]rune, ln)
  114. r := mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
  115. for i := range b {
  116. b[i] = letters[r.Intn(len(letters))]
  117. }
  118. return string(b)
  119. }
  120. func CardPayGetSign(privateKeyStr string, data string) (string, error) {
  121. privateKey, err := ssh.ParseRawPrivateKey([]byte(privateKeyStr))
  122. if err != nil {
  123. return "", err
  124. }
  125. fmt.Println(privateKey.(*rsa.PrivateKey))
  126. fmt.Println(data)
  127. h := crypto.Hash.New(crypto.SHA256)
  128. h.Write([]byte(data))
  129. hashed := h.Sum(nil)
  130. sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.SHA256, hashed)
  131. if err != nil {
  132. return "", nil
  133. }
  134. return base64.StdEncoding.EncodeToString(sign), nil
  135. }
  136. func RsaEncrypts(pubStr, data string) string {
  137. // 公钥加密
  138. // 读取公钥证书
  139. certSlc, errCert := base64.StdEncoding.DecodeString(pubStr)
  140. if errCert != nil {
  141. return ""
  142. }
  143. //证书解析
  144. certBody, errCertBody := x509.ParseCertificate(certSlc)
  145. if errCertBody != nil {
  146. return ""
  147. }
  148. // 提取公钥
  149. rsaPublicKey := certBody.PublicKey.(*rsa.PublicKey)
  150. // 对明文进行加密,PKCS(公钥密码标准),#1就是RSA的标准
  151. cipherByte, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte(data))
  152. if err != nil {
  153. return ""
  154. }
  155. return base64.StdEncoding.EncodeToString(cipherByte)
  156. }