面包店
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

263 líneas
8.3 KiB

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