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

167 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 := "https://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. if param["customerCode"] == "" {
  29. param["customerCode"] = keyArr["customerCode"]
  30. }
  31. param["nonceStr"] = randomString(32)
  32. data := zhios_third_party_utils.SerializeStr(param)
  33. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  34. if err != nil {
  35. return "", err
  36. }
  37. headers := map[string]string{
  38. "Content-Type": "application/json",
  39. "x-efps-sign-no": keyArr["signNo"],
  40. "x-efps-sign-type": "SHA256withRSA",
  41. "x-efps-sign": sign,
  42. "x-efps-timestamp": time.Now().Format("20060102150405"),
  43. }
  44. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  45. zhios_third_party_utils.CurlDebug = true
  46. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  47. if err != nil {
  48. return "", err
  49. }
  50. fmt.Println(string(res))
  51. return string(res), nil
  52. }
  53. func CardPaySendSecond(method string, keyArr map[string]string, param map[string]interface{}) (string, error) {
  54. url := "https://test-efps.epaylinks.cn/" + method
  55. //https://efps.epaylinks.cn/
  56. for key, value := range param {
  57. if value == "" {
  58. delete(param, key)
  59. }
  60. }
  61. param["customerCode"] = keyArr["customerCode"]
  62. param["nonceStr"] = randomString(32)
  63. data := zhios_third_party_utils.SerializeStr(param)
  64. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  65. if err != nil {
  66. return "", err
  67. }
  68. headers := map[string]string{
  69. "Content-Type": "application/json",
  70. "x-efps-sign-no": keyArr["signNo"],
  71. "x-efps-sign-type": "SHA256withRSA",
  72. "x-efps-sign": sign,
  73. "x-efps-timestamp": time.Now().Format("20060102150405"),
  74. }
  75. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  76. zhios_third_party_utils.CurlDebug = true
  77. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  78. if err != nil {
  79. return "", err
  80. }
  81. fmt.Println(string(res))
  82. return string(res), nil
  83. }
  84. func CardPayApplySend(method string, keyArr map[string]string, param map[string]string) (string, error) {
  85. url := "https://test-efps.epaylinks.cn/" + method
  86. //https://efps.epaylinks.cn/
  87. for key, value := range param {
  88. if value == "" {
  89. delete(param, key)
  90. }
  91. }
  92. data := zhios_third_party_utils.SerializeStr(param)
  93. sign, err := CardPayGetSign(keyArr["privateKeyStr"], data)
  94. if err != nil {
  95. return "", err
  96. }
  97. headers := map[string]string{
  98. "Content-Type": "application/json",
  99. "x-efps-sign-no": keyArr["signNo"],
  100. "x-efps-sign-type": "SHA256withRSA",
  101. "x-efps-sign": sign,
  102. "x-efps-timestamp": time.Now().Format("20060102150405"),
  103. }
  104. fmt.Println("参数:", zhios_third_party_utils.SerializeStr(param))
  105. zhios_third_party_utils.CurlDebug = true
  106. res, err := zhios_third_party_utils.CurlPost(url, data, headers)
  107. if err != nil {
  108. return "", err
  109. }
  110. fmt.Println(string(res))
  111. return string(res), nil
  112. }
  113. func randomString(ln int) string {
  114. letters := []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  115. b := make([]rune, ln)
  116. r := mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
  117. for i := range b {
  118. b[i] = letters[r.Intn(len(letters))]
  119. }
  120. return string(b)
  121. }
  122. func CardPayGetSign(privateKeyStr string, data string) (string, error) {
  123. privateKey, err := ssh.ParseRawPrivateKey([]byte(privateKeyStr))
  124. if err != nil {
  125. return "", err
  126. }
  127. fmt.Println(privateKey.(*rsa.PrivateKey))
  128. fmt.Println(data)
  129. h := crypto.Hash.New(crypto.SHA256)
  130. h.Write([]byte(data))
  131. hashed := h.Sum(nil)
  132. sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.SHA256, hashed)
  133. if err != nil {
  134. return "", nil
  135. }
  136. return base64.StdEncoding.EncodeToString(sign), nil
  137. }
  138. func RsaEncrypts(pubStr, data string) string {
  139. // 公钥加密
  140. // 读取公钥证书
  141. certSlc, errCert := base64.StdEncoding.DecodeString(pubStr)
  142. if errCert != nil {
  143. return ""
  144. }
  145. //证书解析
  146. certBody, errCertBody := x509.ParseCertificate(certSlc)
  147. if errCertBody != nil {
  148. return ""
  149. }
  150. // 提取公钥
  151. rsaPublicKey := certBody.PublicKey.(*rsa.PublicKey)
  152. // 对明文进行加密,PKCS(公钥密码标准),#1就是RSA的标准
  153. cipherByte, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte(data))
  154. if err != nil {
  155. return ""
  156. }
  157. return base64.StdEncoding.EncodeToString(cipherByte)
  158. }