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

177 line
5.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. 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. // TradeAppPay is 支付宝H5支付
  65. func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, page_url string, paySet *md.PayData) (string, error) {
  66. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  67. privateKey := priKey
  68. //判断密钥的类型
  69. rsa_type := alipay.RSA2
  70. pkcs_type := alipay.PKCS1
  71. if RSA == "1" {
  72. rsa_type = alipay.RSA
  73. }
  74. if PKCS == "1" {
  75. pkcs_type = alipay.PKCS8
  76. }
  77. if paySet.PayAliUseType == "1" {
  78. rsa_type = alipay.RSA2
  79. pkcs_type = alipay.PKCS8
  80. }
  81. //初始化支付宝客户端
  82. // appId:应用ID
  83. // privateKey:应用秘钥
  84. // isProd:是否是正式环境
  85. client := alipay.NewClient(appID, privateKey, true)
  86. //配置公共参数
  87. client.SetCharset("utf-8").
  88. SetSignType(rsa_type).
  89. SetPrivateKeyType(pkcs_type).
  90. SetReturnUrl(page_url).
  91. SetNotifyUrl(notiURL)
  92. //新支付宝支付
  93. if paySet.PayAliUseType == "1" {
  94. client.SetAppCertSN(paySet.PayAppCertSn)
  95. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  96. client.SetAliPayRootCertSN(aliPayRootCertSN)
  97. client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey)
  98. }
  99. //请求参数
  100. body := make(gopay.BodyMap)
  101. body.Set("subject", subject)
  102. body.Set("out_trade_no", orderID)
  103. // quit_url is 用户付款中途退出返回商户网站的地址
  104. body.Set("quit_url", notiURL)
  105. body.Set("total_amount", amount)
  106. // product_code is 销售产品码,商家和支付宝签约的产品码
  107. body.Set("product_code", "QUICK_WAP_WAY")
  108. //手机网站支付请求
  109. payUrl, err := client.TradeWapPay(body)
  110. if err != nil {
  111. return "", err
  112. }
  113. return payUrl, nil
  114. }
  115. // TradeAppPay is 支付宝小程序本身支付
  116. func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (*alipay.TradeCreateResponse, error) {
  117. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  118. privateKey := priKey
  119. rsa_type := alipay.RSA2
  120. pkcs_type := alipay.PKCS1
  121. if RSA == "1" {
  122. rsa_type = alipay.RSA
  123. }
  124. if PKCS == "1" {
  125. pkcs_type = alipay.PKCS8
  126. }
  127. if paySet.PayAliUseType == "1" {
  128. rsa_type = alipay.RSA2
  129. pkcs_type = alipay.PKCS8
  130. }
  131. //初始化支付宝客户端
  132. // appId:应用ID
  133. // privateKey:应用私钥,支持PKCS1和PKCS8
  134. // isProd:是否是正式环境
  135. client := alipay.NewClient(appID, privateKey, true)
  136. //配置公共参数
  137. client.SetCharset("utf-8").
  138. SetSignType(rsa_type).
  139. SetPrivateKeyType(pkcs_type).
  140. SetNotifyUrl(notiURL)
  141. if paySet.PayAliUseType == "1" {
  142. client.SetAppCertSN(paySet.PayAppCertSn)
  143. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  144. client.SetAliPayRootCertSN(aliPayRootCertSN)
  145. client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey)
  146. }
  147. //请求参数
  148. body := make(gopay.BodyMap)
  149. body.Set("subject", subject)
  150. // 支付宝小程序支付时 buyer_id 为必传参数,需要提前获取,获取方法如下两种
  151. // 1、local_alipay.SystemOauthToken() 返回取值:rsp.SystemOauthTokenResponse.UserId
  152. // 2、client.SystemOauthToken() 返回取值:aliRsp.SystemOauthTokenResponse.UserId
  153. buyer_id, err := client.SystemOauthToken(body)
  154. if err != nil {
  155. return nil, err
  156. }
  157. body.Set("buyer_id", buyer_id)
  158. body.Set("out_trade_no", orderID)
  159. body.Set("total_amount", amount)
  160. //创建订单
  161. aliRsp, err := client.TradeCreate(body)
  162. if err != nil {
  163. return nil, err
  164. }
  165. return aliRsp, nil
  166. }