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

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