alipay_params.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package gopay
  2. import (
  3. "crypto"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/sha1"
  7. "crypto/sha256"
  8. "crypto/x509"
  9. "encoding/base64"
  10. "encoding/pem"
  11. "errors"
  12. "hash"
  13. "net/url"
  14. )
  15. // AppId string `json:"app_id"` //支付宝分配给开发者的应用ID
  16. // Method string `json:"method"` //接口名称
  17. // Format string `json:"format"` //仅支持 JSON
  18. // ReturnUrl string `json:"return_url"` //HTTP/HTTPS开头字符串
  19. // Charset string `json:"charset"` //请求使用的编码格式,如utf-8,gbk,gb2312等,推荐使用 utf-8
  20. // SignType string `json:"sign_type"` //商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用 RSA2
  21. // Sign string `json:"sign"` //商户请求参数的签名串
  22. // Timestamp string `json:"timestamp"` //发送请求的时间,格式"yyyy-MM-dd HH:mm:ss"
  23. // Version string `json:"version"` //调用的接口版本,固定为:1.0
  24. // NotifyUrl string `json:"notify_url"` //支付宝服务器主动通知商户服务器里指定的页面http/https路径。
  25. // BizContent string `json:"biz_content"` //业务请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档
  26. type OpenApiRoyaltyDetailInfoPojo struct {
  27. RoyaltyType string `json:"royalty_type,omitempty"`
  28. TransOut string `json:"trans_out,omitempty"`
  29. TransOutType string `json:"trans_out_type,omitempty"`
  30. TransInType string `json:"trans_in_type,omitempty"`
  31. TransIn string `json:"trans_in"`
  32. Amount string `json:"amount,omitempty"`
  33. Desc string `json:"desc,omitempty"`
  34. }
  35. //设置 应用公钥证书SN
  36. // appCertSN:应用公钥证书SN,通过 gopay.GetCertSN() 获取
  37. func (a *AliPayClient) SetAppCertSN(appCertSN string) (client *AliPayClient) {
  38. a.AppCertSN = appCertSN
  39. return a
  40. }
  41. //设置 支付宝根证书SN
  42. // alipayRootCertSN:支付宝根证书SN,通过 gopay.GetCertSN() 获取
  43. func (a *AliPayClient) SetAliPayRootCertSN(alipayRootCertSN string) (client *AliPayClient) {
  44. a.AlipayRootCertSN = alipayRootCertSN
  45. return a
  46. }
  47. //设置支付后的ReturnUrl
  48. func (a *AliPayClient) SetReturnUrl(url string) (client *AliPayClient) {
  49. a.ReturnUrl = url
  50. return a
  51. }
  52. //设置支付宝服务器主动通知商户服务器里指定的页面http/https路径。
  53. func (a *AliPayClient) SetNotifyUrl(url string) (client *AliPayClient) {
  54. a.NotifyUrl = url
  55. return a
  56. }
  57. //设置编码格式,如utf-8,gbk,gb2312等,默认推荐使用 utf-8
  58. func (a *AliPayClient) SetCharset(charset string) (client *AliPayClient) {
  59. if charset == null {
  60. a.Charset = "utf-8"
  61. } else {
  62. a.Charset = charset
  63. }
  64. return a
  65. }
  66. //设置签名算法类型,目前支持RSA2和RSA,默认推荐使用 RSA2
  67. func (a *AliPayClient) SetSignType(signType string) (client *AliPayClient) {
  68. if signType == null {
  69. a.SignType = "RSA2"
  70. } else {
  71. a.SignType = signType
  72. }
  73. return a
  74. }
  75. //设置应用授权
  76. func (a *AliPayClient) SetAppAuthToken(appAuthToken string) (client *AliPayClient) {
  77. a.AppAuthToken = appAuthToken
  78. return a
  79. }
  80. //设置用户信息授权
  81. func (a *AliPayClient) SetAuthToken(authToken string) (client *AliPayClient) {
  82. a.AuthToken = authToken
  83. return a
  84. }
  85. //获取参数签名
  86. func getRsaSign(bm BodyMap, signType, privateKey string) (sign string, err error) {
  87. var (
  88. block *pem.Block
  89. h hash.Hash
  90. key *rsa.PrivateKey
  91. hashs crypto.Hash
  92. encryptedBytes []byte
  93. )
  94. if block, _ = pem.Decode([]byte(privateKey)); block == nil {
  95. return null, errors.New("pem.Decode:privateKey decode error")
  96. }
  97. if key, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
  98. return
  99. }
  100. switch signType {
  101. case "RSA":
  102. h = sha1.New()
  103. hashs = crypto.SHA1
  104. case "RSA2":
  105. h = sha256.New()
  106. hashs = crypto.SHA256
  107. default:
  108. h = sha256.New()
  109. hashs = crypto.SHA256
  110. }
  111. if _, err = h.Write([]byte(bm.EncodeAliPaySignParams())); err != nil {
  112. return
  113. }
  114. if encryptedBytes, err = rsa.SignPKCS1v15(rand.Reader, key, hashs, h.Sum(nil)); err != nil {
  115. return
  116. }
  117. sign = base64.StdEncoding.EncodeToString(encryptedBytes)
  118. return
  119. }
  120. //格式化请求URL参数
  121. func FormatAliPayURLParam(body BodyMap) (urlParam string) {
  122. v := url.Values{}
  123. for key, value := range body {
  124. v.Add(key, value.(string))
  125. }
  126. urlParam = v.Encode()
  127. return
  128. }