client_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package alipay
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/iGoogle-ink/gopay"
  7. )
  8. var (
  9. client *Client
  10. appid = "2016091200494382"
  11. aliPayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
  12. privateKey = "MIIEogIBAAKCAQEAy+CRzKw4krA2RzCDTqg5KJg92XkOY0RN3pW4sYInPqnGtHV7YDHu5nMuxY6un+dLfo91OFOEg+RI+WTOPoM4xJtsOaJwQ1lpjycoeLq1OyetGW5Q8wO+iLWJASaMQM/t/aXR/JHaguycJyqlHSlxANvKKs/tOHx9AhW3LqumaCwz71CDF/+70scYuZG/7wxSjmrbRBswxd1Sz9KHdcdjqT8pmieyPqnM24EKBexHDmQ0ySXvLJJy6eu1dJsPIz+ivX6HEfDXmSmJ71AZVqZyCI1MhK813R5E7XCv5NOtskTe3y8uiIhgGpZSdB77DOyPLcmVayzFVLAQ3AOBDmsY6wIDAQABAoIBAHjsNq31zAw9FcR9orQJlPVd7vlJEt6Pybvmg8hNESfanO+16rpwg2kOEkS8zxgqoJ1tSzJgXu23fgzl3Go5fHcoVDWPAhUAOFre9+M7onh2nPXDd6Hbq6v8OEmFapSaf2b9biHnBHq5Chk08v/r74l501w3PVVOiPqulJrK1oVb+0/YmCvVFpGatBcNaefKUEcA+vekWPL7Yl46k6XeUvRfTwomCD6jpYLUhsAKqZiQJhMGoaLglZvkokQMF/4G78K7FbbVLMM1+JDh8zJ/DDVdY2vHREUcCGhl4mCVQtkzIbpxG++vFg7/g/fDI+PquG22hFILTDdtt2g2fV/4wmkCgYEA6goRQYSiM03y8Tt/M4u1Mm7OWYCksqAsU7rzQllHekIN3WjD41Xrjv6uklsX3sTG1syo7Jr9PGE1xQgjDEIyO8h/3lDQyLyycYnyUPGNNMX8ZjmGwcM51DQ/QfIrY/CXjnnW+MVpmNclAva3L33KXCWjw20VsROV1EA8LCL94BUCgYEA3wH4ANpzo7NqXf+2WlPPMuyRrF0QPIRGlFBNtaKFy0mvoclkREPmK7+N4NIGtMf5JNODS5HkFRgmU4YNdupA2I8lIYpD+TsIobZxGUKUkYzRZYZ1m1ttL69YYvCVz9Xosw/VoQ+RrW0scS5yUKqFMIUOV2R/Imi//c5TdKx6VP8CgYAnJ1ADugC4vI2sNdvt7618pnT3HEJxb8J6r4gKzYzbszlGlURQQAuMfKcP7RVtO1ZYkRyhmLxM4aZxNA9I+boVrlFWDAchzg+8VuunBwIslgLHx0/4EoUWLzd1/OGtco6oU1HXhI9J9pRGjqfO1iiIifN/ujwqx7AFNknayG/YkQKBgD6yNgA/ak12rovYzXKdp14Axn+39k2dPp6J6R8MnyLlB3yruwW6NSbNhtzTD1GZ+wCQepQvYvlPPc8zm+t3tl1r+Rtx3ORf5XBZc3iPkGdPOLubTssrrAnA+U9vph61W+OjqwLJ9sHUNK9pSHhHSIS4k6ycM2YAHyIC9NGTgB0PAoGAJjwd1DgMaQldtWnuXjvohPOo8cQudxXYcs6zVRbx6vtjKe2v7e+eK1SSVrR5qFV9AqxDfGwq8THenRa0LC3vNNplqostuehLhkWCKE7Y75vXMR7N6KU1kdoVWgN4BhXSwuRxmHMQfSY7q3HG3rDGz7mzXo1FVMr/uE4iDGm0IXY="
  13. )
  14. func TestMain(m *testing.M) {
  15. // 初始化支付宝客户端
  16. // appId:应用ID
  17. // privateKey:应用私钥,支持PKCS1和PKCS8
  18. // isProd:是否是正式环境
  19. client = NewClient(appid, privateKey, false)
  20. // 配置公共参数
  21. client.SetCharset("utf-8").
  22. SetSignType(RSA2)
  23. // SetReturnUrl("https://www.gopay.ink").
  24. // SetNotifyUrl("https://www.gopay.ink")
  25. // err := client.SetCertSnByPath("cert/appCertPublicKey.crt", "cert/alipayRootCert.crt", "cert/alipayCertPublicKey_RSA2.crt")
  26. // if err != nil {
  27. // fmt.Println("SetCertSnByPath:", err)
  28. // return
  29. // }
  30. os.Exit(m.Run())
  31. }
  32. func TestClient_TradePrecreate(t *testing.T) {
  33. // 请求参数
  34. bm := make(gopay.BodyMap)
  35. bm.Set("subject", "预创建创建订单")
  36. bm.Set("out_trade_no", gopay.GetRandomString(32))
  37. bm.Set("total_amount", "100")
  38. // 创建订单
  39. aliRsp, err := client.TradePrecreate(bm)
  40. if err != nil {
  41. fmt.Println("err:", err)
  42. return
  43. }
  44. fmt.Println("aliRsp:", *aliRsp)
  45. fmt.Println("aliRsp.QrCode:", aliRsp.Response.QrCode)
  46. fmt.Println("aliRsp.OutTradeNo:", aliRsp.Response.OutTradeNo)
  47. }
  48. func TestClient_TradeCreate(t *testing.T) {
  49. // 请求参数
  50. bm := make(gopay.BodyMap)
  51. bm.Set("subject", "创建订单")
  52. bm.Set("buyer_id", "2088802095984694")
  53. bm.Set("out_trade_no", "GZ201901301040355709")
  54. bm.Set("total_amount", "0.01")
  55. // 创建订单
  56. aliRsp, err := client.TradeCreate(bm)
  57. if err != nil {
  58. fmt.Println("err:", err)
  59. return
  60. }
  61. fmt.Println("aliRsp:", *aliRsp)
  62. fmt.Println("aliRsp.TradeNo:", aliRsp.Response.TradeNo)
  63. }
  64. func TestClient_TradeAppPay(t *testing.T) {
  65. // 请求参数
  66. bm := make(gopay.BodyMap)
  67. bm.Set("subject", "测试APP支付")
  68. bm.Set("out_trade_no", "GZ201901301040355706100469")
  69. bm.Set("total_amount", "1.00")
  70. // 手机APP支付参数请求
  71. payParam, err := client.TradeAppPay(bm)
  72. if err != nil {
  73. fmt.Println("err:", err)
  74. return
  75. }
  76. fmt.Println("payParam:", payParam)
  77. }
  78. func TestClient_TradeCancel(t *testing.T) {
  79. // 请求参数
  80. bm := make(gopay.BodyMap)
  81. bm.Set("out_trade_no", "GZ201909081743431443")
  82. // 撤销支付订单
  83. aliRsp, err := client.TradeCancel(bm)
  84. if err != nil {
  85. fmt.Println("err:", err)
  86. return
  87. }
  88. fmt.Println("aliRsp:", *aliRsp)
  89. }
  90. func TestClient_TradeClose(t *testing.T) {
  91. // 请求参数
  92. bm := make(gopay.BodyMap)
  93. bm.Set("out_trade_no", "GZ201909081743431443")
  94. // 条码支付
  95. aliRsp, err := client.TradeClose(bm)
  96. if err != nil {
  97. fmt.Println("err:", err)
  98. return
  99. }
  100. fmt.Println("aliRsp:", *aliRsp)
  101. }
  102. func TestClient_TradePay(t *testing.T) {
  103. // 请求参数
  104. bm := make(gopay.BodyMap)
  105. bm.Set("subject", "条码支付")
  106. bm.Set("scene", "bar_code")
  107. bm.Set("auth_code", "286248566432274952")
  108. bm.Set("out_trade_no", "GZ201909081743431443")
  109. bm.Set("total_amount", "0.01")
  110. bm.Set("timeout_express", "2m")
  111. // 条码支付
  112. aliRsp, err := client.TradePay(bm)
  113. if err != nil {
  114. fmt.Println("err:", err)
  115. return
  116. }
  117. fmt.Println("aliRsp:", *aliRsp)
  118. // 同步返回验签
  119. ok, err := VerifySyncSign(aliPayPublicKey, aliRsp.SignData, aliRsp.Sign)
  120. if err != nil {
  121. fmt.Println("err:::", err)
  122. }
  123. fmt.Println("同步返回验签:", ok)
  124. }
  125. func TestClient_TradeQuery(t *testing.T) {
  126. // 请求参数
  127. bm := make(gopay.BodyMap)
  128. bm.Set("out_trade_no", "GZ201909081743431443")
  129. // 查询订单
  130. aliRsp, err := client.TradeQuery(bm)
  131. if err != nil {
  132. fmt.Println("err:", err)
  133. return
  134. }
  135. fmt.Println("aliRsp:", *aliRsp)
  136. }
  137. func TestClient_TradeWapPay(t *testing.T) {
  138. // 请求参数
  139. bm := make(gopay.BodyMap)
  140. bm.Set("subject", "手机网站测试支付")
  141. bm.Set("out_trade_no", "GZ201909081743431443")
  142. bm.Set("quit_url", "https://www.gopay.ink")
  143. bm.Set("total_amount", "100.00")
  144. bm.Set("product_code", "QUICK_WAP_WAY")
  145. // 手机网站支付请求
  146. payUrl, err := client.TradeWapPay(bm)
  147. if err != nil {
  148. fmt.Println("err:", err)
  149. return
  150. }
  151. fmt.Println("payUrl:", payUrl)
  152. }
  153. func TestClient_TradePagePay(t *testing.T) {
  154. // 请求参数
  155. bm := make(gopay.BodyMap)
  156. bm.Set("subject", "网站测试支付")
  157. bm.Set("out_trade_no", "GZ201909081743431443")
  158. bm.Set("total_amount", "88.88")
  159. bm.Set("product_code", "FAST_INSTANT_TRADE_PAY")
  160. // 电脑网站支付请求
  161. payUrl, err := client.TradePagePay(bm)
  162. if err != nil {
  163. fmt.Println("err:", err)
  164. return
  165. }
  166. fmt.Println("payUrl:", payUrl)
  167. }
  168. func TestClient_TradeRefund(t *testing.T) {
  169. // 请求参数
  170. body := make(gopay.BodyMap)
  171. body.Set("out_trade_no", "GZ201909081743431443")
  172. body.Set("refund_amount", "5")
  173. body.Set("refund_reason", "测试退款")
  174. // 发起退款请求
  175. aliRsp, err := client.TradeRefund(body)
  176. if err != nil {
  177. fmt.Println("err:", err)
  178. return
  179. }
  180. fmt.Println("aliRsp:", *aliRsp)
  181. }
  182. func TestClient_TradePageRefund(t *testing.T) {
  183. // 请求参数
  184. bm := make(gopay.BodyMap)
  185. bm.Set("out_trade_no", "GZ201909081743431443")
  186. bm.Set("refund_amount", "5")
  187. bm.Set("out_request_no", gopay.GetRandomString(32))
  188. // 发起退款请求
  189. aliRsp, err := client.TradePageRefund(bm)
  190. if err != nil {
  191. fmt.Println("err:", err)
  192. return
  193. }
  194. fmt.Println("aliRsp:", *aliRsp)
  195. }
  196. func TestClient_SystemOauthToken(t *testing.T) {
  197. // 请求参数
  198. bm := make(gopay.BodyMap)
  199. bm.Set("grant_type", "authorization_code")
  200. bm.Set("code", "3a06216ac8f84b8c93507bb9774bWX11")
  201. // 发起请求
  202. aliRsp, err := client.SystemOauthToken(bm)
  203. if err != nil {
  204. fmt.Println("err:", err)
  205. return
  206. }
  207. fmt.Println("aliRsp:", *aliRsp)
  208. fmt.Println("aliRsp:", aliRsp.Response.AccessToken)
  209. fmt.Println("aliRsp:", aliRsp.SignData)
  210. }
  211. func TestClient_TradeOrderSettle(t *testing.T) {
  212. // 请求参数
  213. bm := make(gopay.BodyMap)
  214. bm.Set("out_request_no", "201907301518083384")
  215. bm.Set("trade_no", "2019072522001484690549776067")
  216. var listParams []OpenApiRoyaltyDetailInfoPojo
  217. listParams = append(listParams, OpenApiRoyaltyDetailInfoPojo{"transfer", "2088802095984694", "userId", "userId", "2088102363632794", "0.01", "分账给2088102363632794"})
  218. bm.Set("royalty_parameters", listParams)
  219. // fmt.Println("listParams:", bm.Get("royalty_parameters"))
  220. // 发起交易结算接口
  221. aliRsp, err := client.TradeOrderSettle(bm)
  222. if err != nil {
  223. fmt.Println("err:", err)
  224. return
  225. }
  226. fmt.Println("aliRsp:", *aliRsp)
  227. }
  228. func TestClient_OpenAuthTokenApp(t *testing.T) {
  229. // 请求参数
  230. bm := make(gopay.BodyMap)
  231. bm.Set("grant_type", "authorization_code")
  232. bm.Set("code", "866185490c4e40efa9f71efea6766X02")
  233. // 发起请求
  234. aliRsp, err := client.OpenAuthTokenApp(bm)
  235. if err != nil {
  236. fmt.Println("err:", err)
  237. return
  238. }
  239. fmt.Println("aliRsp:", *aliRsp)
  240. }
  241. func TestClient_TradeFastPayRefundQuery(t *testing.T) {
  242. // 请求参数
  243. bm := make(gopay.BodyMap)
  244. bm.Set("out_trade_no", "GZ201909081743431443")
  245. bm.Set("out_request_no", "GZ201909081743431443")
  246. // 发起退款查询请求
  247. aliRsp, err := client.TradeFastPayRefundQuery(bm)
  248. if err != nil {
  249. fmt.Println("err:", err)
  250. return
  251. }
  252. fmt.Println("aliRsp:", *aliRsp)
  253. }
  254. func TestClient_FundTransToaccountTransfer(t *testing.T) {
  255. // 请求参数
  256. bm := make(gopay.BodyMap)
  257. bm.Set("out_biz_no", gopay.GetRandomString(32))
  258. bm.Set("payee_type", "ALIPAY_LOGONID")
  259. bm.Set("payee_account", "otmdfd2378@sandbox.com")
  260. bm.Set("amount", "1000")
  261. bm.Set("payer_show_name", "发钱人名字")
  262. bm.Set("payee_real_name", "沙箱环境")
  263. bm.Set("remark", "转账测试")
  264. // 转账
  265. aliRsp, err := client.FundTransToaccountTransfer(bm)
  266. if err != nil {
  267. fmt.Println("err:", err)
  268. return
  269. }
  270. fmt.Println("aliRsp:", *aliRsp)
  271. }
  272. func TestClient_UserCertifyOpenInit(t *testing.T) {
  273. // 请求参数
  274. bm := make(gopay.BodyMap)
  275. bm.Set("outer_order_no", "ZGYD201809132323000001234")
  276. // 认证场景码:FACE:多因子人脸认证,CERT_PHOTO:多因子证照认证,CERT_PHOTO_FACE :多因子证照和人脸认证,SMART_FACE:多因子快捷认证
  277. bm.Set("biz_code", "FACE")
  278. // 需要验证的身份信息参数,格式为json
  279. identity := make(map[string]string)
  280. identity["identity_type"] = "CERT_INFO"
  281. identity["cert_type"] = "IDENTITY_CARD"
  282. identity["cert_name"] = "张三"
  283. identity["cert_no"] = "310123199012301234"
  284. bm.Set("identity_param", identity)
  285. // 商户个性化配置,格式为json
  286. merchant := make(map[string]string)
  287. merchant["return_url"] = "https://www.gopay.ink"
  288. bm.Set("merchant_config", merchant)
  289. // 发起请求
  290. aliRsp, err := client.UserCertifyOpenInit(bm)
  291. if err != nil {
  292. fmt.Println("err:", err)
  293. return
  294. }
  295. fmt.Println("aliRsp:", *aliRsp)
  296. }
  297. func TestClient_UserCertifyOpenCertify(t *testing.T) {
  298. // 请求参数
  299. bm := make(gopay.BodyMap)
  300. // 本次申请操作的唯一标识,由开放认证初始化接口调用后生成,后续的操作都需要用到
  301. bm.Set("certify_id", "53827f9d085b3ce43938c6e5915b4729")
  302. // 发起请求
  303. certifyUrl, err := client.UserCertifyOpenCertify(bm)
  304. if err != nil {
  305. fmt.Println("err:", err)
  306. return
  307. }
  308. fmt.Println("certifyUrl:", certifyUrl)
  309. }
  310. func TestClient_UserCertifyOpenQuery(t *testing.T) {
  311. // 请求参数
  312. bm := make(gopay.BodyMap)
  313. // 本次申请操作的唯一标识,由开放认证初始化接口调用后生成,后续的操作都需要用到
  314. bm.Set("certify_id", "OC201809253000000393900404029253")
  315. // 发起请求
  316. aliRsp, err := client.UserCertifyOpenQuery(bm)
  317. if err != nil {
  318. fmt.Println("err:", err)
  319. return
  320. }
  321. fmt.Println("aliRsp:", *aliRsp)
  322. fmt.Println("aliRsp.Response.Passed:", aliRsp.Response.Passed)
  323. }
  324. func TestClient_UserInfoAuth(t *testing.T) {
  325. // 请求参数
  326. bm := make(gopay.BodyMap)
  327. // 接口权限值,目前只支持auth_user和auth_base两个值。具体说明看文档介绍
  328. bm.Set("scopes", []string{"auth_user"})
  329. bm.Set("state", "init")
  330. // 发起请求
  331. aliRsp, err := client.UserInfoAuth(bm)
  332. if err != nil {
  333. fmt.Println("err:", err)
  334. return
  335. }
  336. fmt.Println("aliRsp:", *aliRsp)
  337. }
  338. func TestClient_UserInfoShare(t *testing.T) {
  339. // 发起请求
  340. aliRsp, err := client.UserInfoShare()
  341. if err != nil {
  342. fmt.Println("err:", err)
  343. return
  344. }
  345. fmt.Println("aliRsp:", *aliRsp)
  346. // 同步返回验签
  347. ok, err := VerifySyncSign(aliPayPublicKey, aliRsp.SignData, aliRsp.Sign)
  348. if err != nil {
  349. fmt.Println("VerifySign-err:", err)
  350. return
  351. }
  352. fmt.Println("ok:", ok)
  353. }
  354. func TestClient_ZhimaCreditScoreGet(t *testing.T) {
  355. // 请求参数
  356. body := make(gopay.BodyMap)
  357. body.Set("transaction_id", gopay.GetRandomString(48))
  358. body.Set("product_code", "w1010100100000000001")
  359. // 芝麻分
  360. aliRsp, err := client.ZhimaCreditScoreGet(body)
  361. if err != nil {
  362. fmt.Println("err:", err)
  363. return
  364. }
  365. fmt.Println("aliRsp:", *aliRsp)
  366. }
  367. // =================================================
  368. func TestSyncVerifySign(t *testing.T) {
  369. signData := `{"code":"10000","msg":"Success","buyer_logon_id":"854***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088102363632794","fund_bill_list":[{"amount":"0.01","fund_channel":"PCREDIT"}],"gmt_payment":"2019-08-29 20:14:05","invoice_amount":"0.01","out_trade_no":"GZ201901301040361012","point_amount":"0.00","receipt_amount":"0.01","total_amount":"0.01","trade_no":"2019082922001432790585537960"}`
  370. sign := "bk3SzX0CZRI811IJioS2XKQHcgMixUT8mYyGQj+vcOAQas7GIYi6LpykqqSc3m7+yvqoG0TdX/c2JjYnpw/J53JxtC2IC4vsLuIPIgghVo5qafsfSxEJ22w20RZDatI2dYqFVcj8Jp+4aesQ8zMMNw7cX9NLyk7kw3DecYeyQp+zrZMueZPqLh88Z+54G+e6QuSU++0ouqQVd4PkpPqy6YI+8MdMUX4Ve0jOQxMmYH8BC6n5ZsTH/uEaLEtzYVZdSw/xdSQ7K1SH73aEH8XbRYx6rL7RkKksrdvhezX+ThDjQ+fTWjvNFrGcg3fmqXRy2elvoalu+BQmqlkWWjEJYA=="
  371. aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
  372. pKey := FormatPublicKey(aliPayPublicKey)
  373. err := verifySign(signData, sign, RSA2, pKey)
  374. if err != nil {
  375. fmt.Println("err:", err)
  376. }
  377. }
  378. func TestVerifySign(t *testing.T) {
  379. // 测试,假数据,无法验签通过
  380. publicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
  381. bm := make(gopay.BodyMap)
  382. bm.Set("sign", "f19WZ3rko3cVpSG3uEEJF0eb4DuZVLt4/GXnNw9qg8iHUsJLkav0V91R5SSTDhW5lgkn3Xhq7TkFRJiDXdVXMu3XUlsONArp3Iu4tXagYJWt9jbcnc2/l29VYDXPLNcs7BXEWFEaCZLutQY2U82AumEwSc1XBKtsLC4mVX3M3f/ExFQHWklJEBHArYBGe4535uFRlsT2fk6WVuX8CuYZatCrVF1o02xMS5aD29eICPkmin/h87OcTbE1syktyCU1WVgcypagUdGGPTF0SVDFf7FRov7+w7fiCGGGL10tNlK/MLzcewtN2dyGF6RLUX3m+HQ7sNEk2wylRXLNUFig==")
  383. bm.Set("seller_email", "imonkey@100tal.com")
  384. bm.Set("sign_type", RSA2)
  385. bm.Set("total_amount", "0.02")
  386. bm.Set("buyer_id", "2088812847201551")
  387. bm.Set("invoice_amount", "0.02")
  388. bm.Set("fund_bill_list", `[{"amount":"0.02","fundChannel":"PCREDIT"}]`)
  389. bm.Set("trade_no", "2020010222001401551430614892")
  390. bm.Set("receipt_amount", "0.02")
  391. bm.Set("buyer_pay_amount", "0.02")
  392. bm.Set("notify_time", "2020-01-02 16:18:21")
  393. bm.Set("subject", "商品")
  394. bm.Set("auth_app_id", "2015102700040153")
  395. bm.Set("charset", "utf-8")
  396. bm.Set("point_amount", "0.00")
  397. bm.Set("notify_type", "trade_status_sync")
  398. bm.Set("out_trade_no", "1086209247658383466")
  399. bm.Set("gmt_payment", "2020-01-02 16:18:21")
  400. bm.Set("trade_status", "TRADE_SUCCESS")
  401. bm.Set("version", "1.0")
  402. bm.Set("buyer_logon_id", "185****2920")
  403. bm.Set("gmt_create", "2020-01-02 16:18:21")
  404. bm.Set("app_id", "2015102700040153")
  405. bm.Set("seller_id", "2088631240818980")
  406. bm.Set("notify_id", "2020010200222161821001551453140885")
  407. ok, err := VerifySign(publicKey, bm)
  408. if err != nil {
  409. fmt.Println("err:", err)
  410. return
  411. }
  412. fmt.Println("OK:", ok)
  413. }
  414. func TestVerifySignWithCert(t *testing.T) {
  415. // 测试,假数据,无法验签通过
  416. bm := make(gopay.BodyMap)
  417. bm.Set("sign", "kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=")
  418. bm.Set("sign_type", RSA2)
  419. bm.Set("total_amount", "2.00")
  420. bm.Set("buyer_id", "2088102116773037")
  421. bm.Set("body", "大乐透2.1")
  422. bm.Set("trade_no", "2016071921001003030200089909")
  423. bm.Set("refund_fee", "0.00")
  424. bm.Set("notify_time", "2016-07-19 14:10:49")
  425. bm.Set("subject", "大乐透2.1")
  426. bm.Set("charset", "utf-8")
  427. bm.Set("notify_type", "trade_status_sync")
  428. bm.Set("out_trade_no", "0719141034-6418")
  429. bm.Set("gmt_close", "2016-07-19 14:10:46")
  430. bm.Set("gmt_payment", "2016-07-19 14:10:47")
  431. bm.Set("trade_status", "TRADE_SUCCESS")
  432. bm.Set("version", "1.0")
  433. bm.Set("gmt_create", "2016-07-19 14:10:44")
  434. bm.Set("app_id", "2015102700040153")
  435. bm.Set("seller_id", "2088102119685838")
  436. bm.Set("notify_id", "4a91b7a78a503640467525113fb7d8bg8e")
  437. ok, err := VerifySignWithCert("/cert/alipayCertPublicKey_RSA2.crt", bm)
  438. if err != nil {
  439. fmt.Println("err:", err)
  440. return
  441. }
  442. fmt.Println("OK:", ok)
  443. }
  444. func TestGetCertSN(t *testing.T) {
  445. sn, err := GetCertSN("cert/alipayCertPublicKey_RSA2.crt")
  446. if err != nil {
  447. fmt.Println("err:", err)
  448. return
  449. }
  450. // 04afd423ea5bd6f5c5482854ed73278c
  451. fmt.Println("alipayCertPublicKey_RSA2:", sn)
  452. sn, err = GetCertSN("cert/appCertPublicKey.crt")
  453. if err != nil {
  454. fmt.Println("err:", err)
  455. return
  456. }
  457. // 4498aaa8ab0c8986c15c41b36186db7d
  458. fmt.Println("appCertPublicKey:", sn)
  459. sn, err = GetRootCertSN("cert/alipayRootCert.crt")
  460. if err != nil {
  461. fmt.Println("err:", err)
  462. return
  463. }
  464. // 687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6
  465. fmt.Println("alipay_root_cert_sn:", sn)
  466. }
  467. func TestDecryptOpenDataToBodyMap(t *testing.T) {
  468. data := "MkvuiIZsGOC8S038cu/JIpoRKnF+ZFjoIRGf5d/K4+ctYjCtb/eEkwgrdB5TeH/93bxff1Ylb+SE+UGStlpvcg=="
  469. key := "TDftre9FpItr46e9BVNJcw=="
  470. bm, err := DecryptOpenDataToBodyMap(data, key)
  471. if err != nil {
  472. fmt.Println("err:", err)
  473. return
  474. }
  475. fmt.Println("bm:", bm)
  476. }