附近小店
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

335 řádky
10 KiB

  1. package alipay
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/md"
  5. "applet/app/utils/logx"
  6. "fmt"
  7. "github.com/iGoogle-ink/gopay"
  8. "github.com/iGoogle-ink/gopay/alipay"
  9. )
  10. func commClient(appID, priKey, RSA, PKCS string, paySet *md.PayData) (*alipay.Client, error) {
  11. client := alipay.NewClient(appID, priKey, true)
  12. client.DebugSwitch = gopay.DebugOn
  13. //判断密钥的类型
  14. rsaType := alipay.RSA2
  15. pkcsType := alipay.PKCS1
  16. if RSA == "1" {
  17. rsaType = alipay.RSA
  18. }
  19. if PKCS == "1" {
  20. pkcsType = alipay.PKCS8
  21. }
  22. if paySet.PayAliUseType == "1" {
  23. rsaType = alipay.RSA2
  24. pkcsType = alipay.PKCS8
  25. }
  26. //配置公共参数
  27. client.SetCharset("utf-8").
  28. SetSignType(rsaType).
  29. SetPrivateKeyType(pkcsType)
  30. //新支付宝支付
  31. if paySet.PayAliUseType == "1" {
  32. appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if appCertSN == "" {
  37. return nil, err
  38. }
  39. client.SetAppCertSN(appCertSN)
  40. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  41. client.SetAliPayRootCertSN(aliPayRootCertSN)
  42. aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if aliPayPublicCertSN == "" {
  47. return nil, err
  48. }
  49. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  50. }
  51. return client, nil
  52. }
  53. //(实名证件信息比对验证预咨询)
  54. //https://opendocs.alipay.com/apis/api_2/alipay.user.certdoc.certverify.preconsult
  55. func UserCertverifyPreconsult(appID, priKey, userName, certNo, RSA, PKCS string, paySet *md.PayData) (*md.AlipayUserCertdocCertverifyPreconsult, error) {
  56. client, _ := commClient(appID, priKey, RSA, PKCS, paySet)
  57. //请求参数
  58. body := make(gopay.BodyMap)
  59. body.Set("user_name", userName)
  60. body.Set("cert_type", "IDENTITY_CARD")
  61. body.Set("cert_no", certNo)
  62. var aliPsp md.AlipayUserCertdocCertverifyPreconsult
  63. err := client.PostAliPayAPISelfV2(body, "alipay.user.certdoc.certverify.preconsult", aliPsp)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &aliPsp, err
  68. }
  69. //(实名证件信息比对验证咨询)
  70. //https://opendocs.alipay.com/apis/api_2/alipay.user.certdoc.certverify.consult
  71. func UserCertverifyConsult(appID, priKey, verifyId, RSA, PKCS string, paySet *md.PayData) (*md.AlipayUserCertdocCertverifyConsult, error) {
  72. client, _ := commClient(appID, priKey, RSA, PKCS, paySet)
  73. //请求参数
  74. body := make(gopay.BodyMap)
  75. body.Set("verify_id", verifyId)
  76. var aliPsp md.AlipayUserCertdocCertverifyConsult
  77. err := client.PostAliPayAPISelfV2(body, "alipay.user.certdoc.certverify.consult", aliPsp)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &aliPsp, err
  82. }
  83. // TradeAppPay is 支付宝APP支付
  84. // 抖音头条小程序使用APP调起
  85. func TradeAppPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (string, error) {
  86. //初始化支付宝客户端
  87. // appID 是在支付宝申请的APPID
  88. // priKey 是支付宝私钥
  89. // subject 是支付订单的主题
  90. // orderID 是智莺这边生成的订单id
  91. // amount 是付费金额
  92. // notiURL 通知地址url
  93. // passback_params 回调通知参数
  94. client := alipay.NewClient(appID, priKey, true)
  95. client.DebugSwitch = gopay.DebugOn
  96. //判断密钥的类型
  97. rsa_type := alipay.RSA2
  98. pkcs_type := alipay.PKCS1
  99. if RSA == "1" {
  100. rsa_type = alipay.RSA
  101. }
  102. if PKCS == "1" {
  103. pkcs_type = alipay.PKCS8
  104. }
  105. if paySet.PayAliUseType == "1" {
  106. rsa_type = alipay.RSA2
  107. pkcs_type = alipay.PKCS8
  108. }
  109. //配置公共参数
  110. client.SetCharset("utf-8").
  111. SetSignType(rsa_type).
  112. SetPrivateKeyType(pkcs_type)
  113. if notiURL != "" {
  114. client.SetNotifyUrl(notiURL)
  115. }
  116. //新支付宝支付
  117. if paySet.PayAliUseType == "1" {
  118. appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
  119. fmt.Println("-应用-")
  120. fmt.Println(appCertSN)
  121. if err != nil {
  122. fmt.Println(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. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  132. client.SetAliPayRootCertSN(aliPayRootCertSN)
  133. aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
  134. fmt.Println("-公钥-")
  135. fmt.Println(aliPayPublicCertSN)
  136. if err != nil {
  137. fmt.Println(err)
  138. return "", err
  139. }
  140. if aliPayPublicCertSN == "" {
  141. fmt.Println(err)
  142. return "", err
  143. }
  144. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  145. }
  146. fmt.Println(client)
  147. //请求参数
  148. body := make(gopay.BodyMap)
  149. body.Set("subject", subject)
  150. body.Set("body", subject)
  151. body.Set("out_trade_no", orderID)
  152. body.Set("total_amount", amount)
  153. body.Set("timeout_express", "30m")
  154. // body.Set("passback_params", orderID)
  155. //手机APP支付参数请求
  156. payParam, err := client.TradeAppPay(body)
  157. if err != nil {
  158. return "", logx.Warn(err)
  159. }
  160. return payParam, nil
  161. }
  162. // TradeAppPay is 支付宝H5支付
  163. func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, page_url string, paySet *md.PayData) (string, error) {
  164. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  165. privateKey := priKey
  166. //判断密钥的类型
  167. rsa_type := alipay.RSA2
  168. pkcs_type := alipay.PKCS1
  169. if RSA == "1" {
  170. rsa_type = alipay.RSA
  171. }
  172. if PKCS == "1" {
  173. pkcs_type = alipay.PKCS8
  174. }
  175. if paySet.PayAliUseType == "1" {
  176. rsa_type = alipay.RSA2
  177. pkcs_type = alipay.PKCS8
  178. }
  179. //初始化支付宝客户端
  180. // appId:应用ID
  181. // privateKey:应用秘钥
  182. // isProd:是否是正式环境
  183. client := alipay.NewClient(appID, privateKey, true)
  184. //配置公共参数
  185. client.SetCharset("utf-8").
  186. SetSignType(rsa_type).
  187. SetPrivateKeyType(pkcs_type).
  188. SetReturnUrl(page_url).
  189. SetNotifyUrl(notiURL)
  190. //新支付宝支付
  191. if paySet.PayAliUseType == "1" {
  192. appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
  193. if err != nil {
  194. fmt.Println(err)
  195. return "", err
  196. }
  197. if appCertSN == "" {
  198. fmt.Println(err)
  199. return "", err
  200. }
  201. client.SetAppCertSN(appCertSN)
  202. //aliPayRootCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
  203. //if err != nil {
  204. // fmt.Println(err)
  205. // return "", err
  206. //}
  207. //if aliPayRootCertSN == "" {
  208. // fmt.Println(err)
  209. // return "", err
  210. //}
  211. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  212. client.SetAliPayRootCertSN(aliPayRootCertSN)
  213. aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
  214. if err != nil {
  215. fmt.Println(err)
  216. return "", err
  217. }
  218. if aliPayPublicCertSN == "" {
  219. fmt.Println(err)
  220. return "", err
  221. }
  222. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  223. }
  224. //请求参数
  225. body := make(gopay.BodyMap)
  226. body.Set("subject", subject)
  227. body.Set("out_trade_no", orderID)
  228. // quit_url is 用户付款中途退出返回商户网站的地址
  229. body.Set("quit_url", notiURL)
  230. body.Set("total_amount", amount)
  231. // product_code is 销售产品码,商家和支付宝签约的产品码
  232. body.Set("product_code", "QUICK_WAP_WAY")
  233. //手机网站支付请求
  234. payUrl, err := client.TradeWapPay(body)
  235. if err != nil {
  236. return "", logx.Warn(err)
  237. }
  238. return payUrl, nil
  239. }
  240. // TradeAppPay is 支付宝小程序本身支付
  241. func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (*alipay.TradeCreateResponse, error) {
  242. //aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  243. privateKey := priKey
  244. rsa_type := alipay.RSA2
  245. pkcs_type := alipay.PKCS1
  246. if RSA == "1" {
  247. rsa_type = alipay.RSA
  248. }
  249. if PKCS == "1" {
  250. pkcs_type = alipay.PKCS8
  251. }
  252. if paySet.PayAliUseType == "1" {
  253. rsa_type = alipay.RSA2
  254. pkcs_type = alipay.PKCS8
  255. }
  256. //初始化支付宝客户端
  257. // appId:应用ID
  258. // privateKey:应用私钥,支持PKCS1和PKCS8
  259. // isProd:是否是正式环境
  260. client := alipay.NewClient(appID, privateKey, true)
  261. //配置公共参数
  262. client.SetCharset("utf-8").
  263. SetSignType(rsa_type).
  264. SetPrivateKeyType(pkcs_type).
  265. SetNotifyUrl(notiURL)
  266. if paySet.PayAliUseType == "1" {
  267. appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
  268. if err != nil {
  269. fmt.Println(err)
  270. return nil, err
  271. }
  272. if appCertSN == "" {
  273. fmt.Println(err)
  274. return nil, err
  275. }
  276. client.SetAppCertSN(appCertSN)
  277. //aliPayRootCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
  278. //if err != nil {
  279. // fmt.Println(err)
  280. // return nil, err
  281. //}
  282. //if aliPayRootCertSN == "" {
  283. // fmt.Println(err)
  284. // return nil, err
  285. //}
  286. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  287. client.SetAliPayRootCertSN(aliPayRootCertSN)
  288. aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
  289. if err != nil {
  290. fmt.Println(err)
  291. return nil, err
  292. }
  293. if aliPayPublicCertSN == "" {
  294. fmt.Println(err)
  295. return nil, err
  296. }
  297. client.SetAliPayPublicCertSN(aliPayPublicCertSN)
  298. }
  299. //请求参数
  300. body := make(gopay.BodyMap)
  301. body.Set("subject", subject)
  302. // 支付宝小程序支付时 buyer_id 为必传参数,需要提前获取,获取方法如下两种
  303. // 1、alipay.SystemOauthToken() 返回取值:rsp.SystemOauthTokenResponse.UserId
  304. // 2、client.SystemOauthToken() 返回取值:aliRsp.SystemOauthTokenResponse.UserId
  305. buyer_id, err := client.SystemOauthToken(body)
  306. if err != nil {
  307. return nil, logx.Warn(err)
  308. }
  309. body.Set("buyer_id", buyer_id)
  310. body.Set("out_trade_no", orderID)
  311. body.Set("total_amount", amount)
  312. //创建订单
  313. aliRsp, err := client.TradeCreate(body)
  314. if err != nil {
  315. return nil, logx.Warn(err)
  316. }
  317. logx.Warn("aliRsp:", *aliRsp)
  318. logx.Warn("aliRsp.TradeNo:", aliRsp.Response.TradeNo)
  319. return aliRsp, nil
  320. }