蛋蛋星球-制度模式
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.

149 lines
6.1 KiB

  1. package alipay
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/go-pay/crypto/xrsa"
  6. "github.com/go-pay/gopay"
  7. "github.com/go-pay/xlog"
  8. )
  9. func TestSyncVerifySign(t *testing.T) {
  10. 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"}`
  11. sign := "bk3SzX0CZRI811IJioS2XKQHcgMixUT8mYyGQj+vcOAQas7GIYi6LpykqqSc3m7+yvqoG0TdX/c2JjYnpw/J53JxtC2IC4vsLuIPIgghVo5qafsfSxEJ22w20RZDatI2dYqFVcj8Jp+4aesQ8zMMNw7cX9NLyk7kw3DecYeyQp+zrZMueZPqLh88Z+54G+e6QuSU++0ouqQVd4PkpPqy6YI+8MdMUX4Ve0jOQxMmYH8BC6n5ZsTH/uEaLEtzYVZdSw/xdSQ7K1SH73aEH8XbRYx6rL7RkKksrdvhezX+ThDjQ+fTWjvNFrGcg3fmqXRy2elvoalu+BQmqlkWWjEJYA=="
  12. alipayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
  13. pKey := xrsa.FormatAlipayPublicKey(alipayPublicKey)
  14. err := verifySign(signData, sign, RSA2, pKey)
  15. if err != nil {
  16. xlog.Errorf("verifySign(),error:%+v", err)
  17. }
  18. }
  19. func TestVerifySign(t *testing.T) {
  20. // 测试,假数据,无法验签通过
  21. publicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
  22. bm := make(gopay.BodyMap)
  23. bm.Set("sign", "f19WZ3rko3cVpSG3uEEJF0eb4DuZVLt4/GXnNw9qg8iHUsJLkav0V91R5SSTDhW5lgkn3Xhq7TkFRJiDXdVXMu3XUlsONArp3Iu4tXagYJWt9jbcnc2/l29VYDXPLNcs7BXEWFEaCZLutQY2U82AumEwSc1XBKtsLC4mVX3M3f/ExFQHWklJEBHArYBGe4535uFRlsT2fk6WVuX8CuYZatCrVF1o02xMS5aD29eICPkmin/h87OcTbE1syktyCU1WVgcypagUdGGPTF0SVDFf7FRov7+w7fiCGGGL10tNlK/MLzcewtN2dyGF6RLUX3m+HQ7sNEk2wylRXLNUFig==")
  24. bm.Set("seller_email", "imonkey@100tal.com")
  25. bm.Set("sign_type", RSA2)
  26. bm.Set("total_amount", "0.02")
  27. bm.Set("buyer_id", "2088812847201551")
  28. bm.Set("invoice_amount", "0.02")
  29. bm.Set("fund_bill_list", `[{"amount":"0.02","fundChannel":"PCREDIT"}]`)
  30. bm.Set("trade_no", "2020010222001401551430614892")
  31. bm.Set("receipt_amount", "0.02")
  32. bm.Set("buyer_pay_amount", "0.02")
  33. bm.Set("notify_time", "2020-01-02 16:18:21")
  34. bm.Set("subject", "商品")
  35. bm.Set("auth_app_id", "2015102700040153")
  36. bm.Set("charset", "utf-8")
  37. bm.Set("point_amount", "0.00")
  38. bm.Set("notify_type", "trade_status_sync")
  39. bm.Set("out_trade_no", "1086209247658383466")
  40. bm.Set("gmt_payment", "2020-01-02 16:18:21")
  41. bm.Set("trade_status", "TRADE_SUCCESS")
  42. bm.Set("version", "1.0")
  43. bm.Set("buyer_logon_id", "185****2920")
  44. bm.Set("gmt_create", "2020-01-02 16:18:21")
  45. bm.Set("app_id", "2015102700040153")
  46. bm.Set("seller_id", "2088631240818980")
  47. bm.Set("notify_id", "2020010200222161821001551453140885")
  48. ok, err := VerifySign(publicKey, bm)
  49. if err != nil {
  50. xlog.Errorf("VerifySign(%+v),error:%+v", bm, err)
  51. return
  52. }
  53. xlog.Debug("OK:", ok)
  54. }
  55. func TestVerifySignWithCert(t *testing.T) {
  56. // 测试,假数据,无法验签通过
  57. bm := make(gopay.BodyMap)
  58. bm.Set("sign", "kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=")
  59. bm.Set("sign_type", RSA2)
  60. bm.Set("total_amount", "2.00")
  61. bm.Set("buyer_id", "2088102116773037")
  62. bm.Set("body", "大乐透2.1")
  63. bm.Set("trade_no", "2016071921001003030200089909")
  64. bm.Set("refund_fee", "0.00")
  65. bm.Set("notify_time", "2016-07-19 14:10:49")
  66. bm.Set("subject", "大乐透2.1")
  67. bm.Set("charset", "utf-8")
  68. bm.Set("notify_type", "trade_status_sync")
  69. bm.Set("out_trade_no", "0719141034-6418")
  70. bm.Set("gmt_close", "2016-07-19 14:10:46")
  71. bm.Set("gmt_payment", "2016-07-19 14:10:47")
  72. bm.Set("trade_status", "TRADE_SUCCESS")
  73. bm.Set("version", "1.0")
  74. bm.Set("gmt_create", "2016-07-19 14:10:44")
  75. bm.Set("app_id", "2015102700040153")
  76. bm.Set("seller_id", "2088102119685838")
  77. bm.Set("notify_id", "4a91b7a78a503640467525113fb7d8bg8e")
  78. // filePath
  79. filepath := "/cert/alipayPublicCert.crt"
  80. ok, err := VerifySignWithCert(filepath, bm)
  81. if err != nil {
  82. xlog.Errorf("VerifySignWithCert(%+v),error:%+v", bm, err)
  83. return
  84. }
  85. xlog.Debug("VerifySignWithCert", "OK:", ok)
  86. // fileByte
  87. bts, err := os.ReadFile(filepath)
  88. if err != nil {
  89. xlog.Errorf("VerifySignWithCert(%+v),error:%+v", bm, err)
  90. return
  91. }
  92. ok, err = VerifySignWithCert(bts, bm)
  93. if err != nil {
  94. xlog.Errorf("VerifySignWithCert(%+v),error:%+v", bm, err)
  95. return
  96. }
  97. xlog.Debug("VerifySignWithCert", "OK:", ok)
  98. }
  99. func TestGetCertSN(t *testing.T) {
  100. sn, err := GetCertSN("cert/alipayPublicCert.crt")
  101. if err != nil {
  102. xlog.Errorf("GetCertSN(),error:%+v", err)
  103. return
  104. }
  105. xlog.Debug(sn)
  106. pubKeyPath := "cert/appPublicCert.crt"
  107. sn, err = GetCertSN(pubKeyPath)
  108. if err != nil {
  109. xlog.Errorf("GetCertSN(),error:%+v", err)
  110. return
  111. }
  112. xlog.Debug(sn)
  113. bts, _ := os.ReadFile(pubKeyPath)
  114. sn, err = GetCertSN(bts)
  115. if err != nil {
  116. xlog.Errorf("GetCertSN(),error:%+v", err)
  117. return
  118. }
  119. xlog.Debug(sn)
  120. rootCrtPath := "cert/alipayRootCert.crt"
  121. sn, err = GetRootCertSN(rootCrtPath)
  122. if err != nil {
  123. xlog.Errorf("GetCertSN(),error:%+v", err)
  124. return
  125. }
  126. xlog.Debug(sn)
  127. bts, _ = os.ReadFile(rootCrtPath)
  128. sn, err = GetRootCertSN(bts)
  129. if err != nil {
  130. xlog.Errorf("GetCertSN(),error:%+v", err)
  131. return
  132. }
  133. xlog.Debug(sn)
  134. // Output:
  135. // 04afd423ea5bd6f5c5482854ed73278c
  136. // 4498aaa8ab0c8986c15c41b36186db7d
  137. // 4498aaa8ab0c8986c15c41b36186db7d
  138. // 687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6
  139. // 687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6
  140. }