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

api.go 7.9 KiB

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