蛋蛋星球-客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

262 行
8.3 KiB

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