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

255 lines
7.9 KiB

  1. package local_alipay
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  4. "fmt"
  5. "github.com/iGoogle-ink/gopay"
  6. "github.com/iGoogle-ink/gopay/alipay"
  7. )
  8. // TradeAppPay is 支付宝APP支付
  9. // 抖音头条小程序使用APP调起
  10. func TradeAppPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (string, error) {
  11. //初始化支付宝客户端
  12. // appID 是在支付宝申请的APPID
  13. // priKey 是支付宝私钥
  14. // subject 是支付订单的主题
  15. // orderID 是智莺这边生成的订单id
  16. // amount 是付费金额
  17. // notiURL 通知地址url
  18. // passback_params 回调通知参数
  19. client := alipay.NewClient(appID, priKey, true)
  20. client.DebugSwitch = gopay.DebugOn
  21. //判断密钥的类型
  22. rsa_type := alipay.RSA2
  23. pkcs_type := alipay.PKCS1
  24. if RSA == "1" {
  25. rsa_type = alipay.RSA
  26. }
  27. if PKCS == "1" {
  28. pkcs_type = alipay.PKCS8
  29. }
  30. if paySet.PayAliUseType == "1" {
  31. rsa_type = alipay.RSA2
  32. pkcs_type = alipay.PKCS8
  33. }
  34. //配置公共参数
  35. client.SetCharset("utf-8").
  36. SetSignType(rsa_type).
  37. SetPrivateKeyType(pkcs_type)
  38. if notiURL != "" {
  39. client.SetNotifyUrl(notiURL)
  40. }
  41. //新支付宝支付
  42. if paySet.PayAliUseType == "1" {
  43. appCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAppCertSn)
  44. fmt.Println("-应用-")
  45. fmt.Println(appCertSN)
  46. if err != nil {
  47. fmt.Println(err)
  48. return "", err
  49. }
  50. if appCertSN == "" {
  51. fmt.Println(err)
  52. return "", err
  53. }
  54. client.SetAppCertSN(appCertSN)
  55. //aliPayRootCertSN, err := local_alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
  56. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  57. client.SetAliPayRootCertSN(aliPayRootCertSN)
  58. aliPayPublicCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAlipayrsaPublicKey)
  59. fmt.Println("-公钥-")
  60. fmt.Println(aliPayPublicCertSN)
  61. if err != nil {
  62. fmt.Println(err)
  63. return "", err
  64. }
  65. if aliPayPublicCertSN == "" {
  66. fmt.Println(err)
  67. return "", err
  68. }
  69. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  70. }
  71. fmt.Println(client)
  72. //请求参数
  73. body := make(gopay.BodyMap)
  74. body.Set("subject", subject)
  75. body.Set("body", subject)
  76. body.Set("out_trade_no", orderID)
  77. body.Set("total_amount", amount)
  78. body.Set("timeout_express", "30m")
  79. // body.Set("passback_params", orderID)
  80. //手机APP支付参数请求
  81. payParam, err := client.TradeAppPay(body)
  82. if err != nil {
  83. return "", err
  84. }
  85. return payParam, nil
  86. }
  87. // TradeAppPay is 支付宝H5支付
  88. func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, page_url string, paySet *md.PayData) (string, error) {
  89. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  90. privateKey := priKey
  91. //判断密钥的类型
  92. rsa_type := alipay.RSA2
  93. pkcs_type := alipay.PKCS1
  94. if RSA == "1" {
  95. rsa_type = alipay.RSA
  96. }
  97. if PKCS == "1" {
  98. pkcs_type = alipay.PKCS8
  99. }
  100. if paySet.PayAliUseType == "1" {
  101. rsa_type = alipay.RSA2
  102. pkcs_type = alipay.PKCS8
  103. }
  104. //初始化支付宝客户端
  105. // appId:应用ID
  106. // privateKey:应用秘钥
  107. // isProd:是否是正式环境
  108. client := alipay.NewClient(appID, privateKey, true)
  109. //配置公共参数
  110. client.SetCharset("utf-8").
  111. SetSignType(rsa_type).
  112. SetPrivateKeyType(pkcs_type).
  113. SetReturnUrl(page_url).
  114. SetNotifyUrl(notiURL)
  115. //新支付宝支付
  116. if paySet.PayAliUseType == "1" {
  117. appCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAppCertSn)
  118. if err != nil {
  119. fmt.Println(err)
  120. return "", err
  121. }
  122. if appCertSN == "" {
  123. fmt.Println(err)
  124. return "", err
  125. }
  126. client.SetAppCertSN(appCertSN)
  127. //aliPayRootCertSN, err := local_alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAlipayRootCertSn)
  128. //if err != nil {
  129. // fmt.Println(err)
  130. // return "", err
  131. //}
  132. //if aliPayRootCertSN == "" {
  133. // fmt.Println(err)
  134. // return "", err
  135. //}
  136. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  137. client.SetAliPayRootCertSN(aliPayRootCertSN)
  138. aliPayPublicCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAlipayrsaPublicKey)
  139. if err != nil {
  140. fmt.Println(err)
  141. return "", err
  142. }
  143. if aliPayPublicCertSN == "" {
  144. fmt.Println(err)
  145. return "", err
  146. }
  147. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  148. }
  149. //请求参数
  150. body := make(gopay.BodyMap)
  151. body.Set("subject", subject)
  152. body.Set("out_trade_no", orderID)
  153. // quit_url is 用户付款中途退出返回商户网站的地址
  154. body.Set("quit_url", notiURL)
  155. body.Set("total_amount", amount)
  156. // product_code is 销售产品码,商家和支付宝签约的产品码
  157. body.Set("product_code", "QUICK_WAP_WAY")
  158. //手机网站支付请求
  159. payUrl, err := client.TradeWapPay(body)
  160. if err != nil {
  161. return "", err
  162. }
  163. return payUrl, nil
  164. }
  165. // TradeAppPay is 支付宝小程序本身支付
  166. func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (*alipay.TradeCreateResponse, error) {
  167. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  168. privateKey := priKey
  169. rsa_type := alipay.RSA2
  170. pkcs_type := alipay.PKCS1
  171. if RSA == "1" {
  172. rsa_type = alipay.RSA
  173. }
  174. if PKCS == "1" {
  175. pkcs_type = alipay.PKCS8
  176. }
  177. if paySet.PayAliUseType == "1" {
  178. rsa_type = alipay.RSA2
  179. pkcs_type = alipay.PKCS8
  180. }
  181. //初始化支付宝客户端
  182. // appId:应用ID
  183. // privateKey:应用私钥,支持PKCS1和PKCS8
  184. // isProd:是否是正式环境
  185. client := alipay.NewClient(appID, privateKey, true)
  186. //配置公共参数
  187. client.SetCharset("utf-8").
  188. SetSignType(rsa_type).
  189. SetPrivateKeyType(pkcs_type).
  190. SetNotifyUrl(notiURL)
  191. if paySet.PayAliUseType == "1" {
  192. appCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAppCertSn)
  193. if err != nil {
  194. fmt.Println(err)
  195. return nil, err
  196. }
  197. if appCertSN == "" {
  198. fmt.Println(err)
  199. return nil, err
  200. }
  201. client.SetAppCertSN(appCertSN)
  202. //aliPayRootCertSN, err := local_alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAlipayRootCertSn)
  203. //if err != nil {
  204. // fmt.Println(err)
  205. // return nil, err
  206. //}
  207. //if aliPayRootCertSN == "" {
  208. // fmt.Println(err)
  209. // return nil, err
  210. //}
  211. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  212. client.SetAliPayRootCertSN(aliPayRootCertSN)
  213. aliPayPublicCertSN, err := alipay.GetCertSN(paySet.WxAppletFilepathUrl + "/" + paySet.PayAlipayrsaPublicKey)
  214. if err != nil {
  215. fmt.Println(err)
  216. return nil, err
  217. }
  218. if aliPayPublicCertSN == "" {
  219. fmt.Println(err)
  220. return nil, err
  221. }
  222. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  223. }
  224. //请求参数
  225. body := make(gopay.BodyMap)
  226. body.Set("subject", subject)
  227. // 支付宝小程序支付时 buyer_id 为必传参数,需要提前获取,获取方法如下两种
  228. // 1、local_alipay.SystemOauthToken() 返回取值:rsp.SystemOauthTokenResponse.UserId
  229. // 2、client.SystemOauthToken() 返回取值:aliRsp.SystemOauthTokenResponse.UserId
  230. buyer_id, err := client.SystemOauthToken(body)
  231. if err != nil {
  232. return nil, err
  233. }
  234. body.Set("buyer_id", buyer_id)
  235. body.Set("out_trade_no", orderID)
  236. body.Set("total_amount", amount)
  237. //创建订单
  238. aliRsp, err := client.TradeCreate(body)
  239. if err != nil {
  240. return nil, err
  241. }
  242. return aliRsp, nil
  243. }