支付模块
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.

152 lines
4.4 KiB

  1. package icbc
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  4. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  5. "crypto"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "encoding/base64"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "golang.org/x/crypto/ssh"
  13. "sort"
  14. "strings"
  15. "time"
  16. )
  17. func ShowIntegral(appId, privateKeyStr string, param map[string]string) (int, error) {
  18. bizContent := map[string]string{
  19. "name": param["name"], //客户姓名
  20. "mobile": param["mobile"], //手机号码
  21. "mobileArea": param["mobileArea"], //手机国际代码 853、852、86
  22. "optFlag": param["optFlag"], //客户姓名
  23. "cardNo": param["cardNo"],
  24. "cardType": param["cardType"],
  25. }
  26. if param["optFlag"] == "1" {
  27. bizContent["otp"] = param["otp"]
  28. }
  29. marshal, _ := json.Marshal(bizContent)
  30. param["biz_content"] = string(marshal)
  31. send := Send("/macaufh/mybank/account/cuscredit/cuscreditquery/V1", appId, privateKeyStr, param)
  32. var data = md.IcbcRes{}
  33. err := json.Unmarshal([]byte(send), &data)
  34. fmt.Println("icbc===============", send)
  35. fmt.Println("icbc===============", err)
  36. var cusCredit = 0
  37. if err != nil {
  38. return cusCredit, err
  39. }
  40. if data.ResponseBizContent.Success {
  41. return data.ResponseBizContent.CusCredit, nil
  42. }
  43. return cusCredit, errors.New(data.ResponseBizContent.ReturnMsg)
  44. }
  45. //积分扣除
  46. func DeductIntegral(appId, privateKeyStr string, param map[string]string) error {
  47. bizContent := map[string]string{
  48. "name": param["name"], //客户姓名
  49. "mobile": param["mobile"], //手机号码
  50. "mobileArea": param["mobileArea"], //手机国际代码 853、852、86
  51. "optFlag": param["optFlag"], //客户姓名
  52. "cardNo": param["cardNo"],
  53. "cardType": param["cardType"],
  54. "cusCredit": param["cusCredit"],
  55. }
  56. if param["optFlag"] == "1" {
  57. bizContent["otp"] = param["otp"]
  58. }
  59. marshal, _ := json.Marshal(bizContent)
  60. param["biz_content"] = string(marshal)
  61. send := Send("/macaufh/mybank/account/cuscredit/cuscreditconsume/V1", appId, privateKeyStr, param)
  62. var data = md.IcbcRes{}
  63. err := json.Unmarshal([]byte(send), &data)
  64. fmt.Println("icbc===============", send)
  65. fmt.Println("icbc===============", err)
  66. if err != nil {
  67. return err
  68. }
  69. if data.ResponseBizContent.Success {
  70. return nil
  71. }
  72. return errors.New(data.ResponseBizContent.ReturnMsg)
  73. }
  74. func DefaultParam(method, appId, privateKeyStr string, param map[string]string) map[string]interface{} {
  75. now := time.Now()
  76. arg := map[string]interface{}{
  77. "app_id": appId,
  78. "msg_id": zhios_pay_utils.Int64ToStr(now.Unix()),
  79. "format": "json",
  80. "charset": "utf-8",
  81. "sign_type": "RSA2",
  82. "timestamp": now.Format("2006-01-02 15:04:05"),
  83. }
  84. if param["biz_content"] != "" {
  85. arg["biz_content"] = param["biz_content"]
  86. }
  87. getSign, err2 := GetSign(method, privateKeyStr, arg)
  88. if err2 != nil {
  89. return nil
  90. }
  91. fmt.Println(getSign)
  92. arg["sign"] = getSign
  93. return arg
  94. }
  95. func Send(method string, appId, privateKeyStr string, param map[string]string) string {
  96. req := DefaultParam(method, appId, privateKeyStr, param)
  97. url := "https://apipcs.icbcmo.site/api" + method
  98. headers := map[string]string{
  99. "Content-Type": "application/x-www-form-urlencoded",
  100. }
  101. fmt.Println(req)
  102. //for k, v := range req {
  103. // if strings.Contains(url, "?") == false {
  104. // url += "?" + k + "=" + php2go.URLEncode(v.(string))
  105. // } else {
  106. // url += "&" + k + "=" + php2go.URLEncode(v.(string))
  107. // }
  108. //}
  109. fmt.Println(url)
  110. res, err := zhios_pay_utils.CurlPost(url, req, headers)
  111. if err != nil {
  112. fmt.Println(err)
  113. return ""
  114. }
  115. fmt.Println(string(res))
  116. return string(res)
  117. }
  118. func GetSign(method, privateKeyStr string, param map[string]interface{}) (string, error) {
  119. privateKey, err := ssh.ParseRawPrivateKey([]byte(privateKeyStr))
  120. if err != nil {
  121. return "", err
  122. }
  123. keys := make([]string, 0, len(param))
  124. for k := range param {
  125. keys = append(keys, k)
  126. }
  127. sort.Strings(keys)
  128. data := "/api" + method
  129. for _, v := range keys {
  130. if strings.Contains(data, "?") == false {
  131. data += "?" + v + "=" + param[v].(string)
  132. } else {
  133. data += "&" + v + "=" + param[v].(string)
  134. }
  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. }