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

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