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

257 lines
8.1 KiB

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