TestAccount.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System;
  4. using Alipay.EasySDK.Kernel;
  5. using Newtonsoft.Json;
  6. namespace UnitTest
  7. {
  8. public static class TestAccount
  9. {
  10. /// <summary>
  11. /// 从文件中读取私钥
  12. ///
  13. /// 注意:实际开发过程中,请务必注意不要将私钥信息配置在源码中(比如配置为常量或储存在配置文件的某个字段中等),因为私钥的保密等级往往比源码高得多,将会增加私钥泄露的风险。
  14. /// 推荐将私钥信息储存在专用的私钥文件中,将私钥文件通过安全的流程分发到服务器的安全储存区域上,仅供自己的应用运行时读取。
  15. /// </summary>
  16. /// <param name="appId">私钥对应的APP_ID</param>
  17. /// <returns>私钥字符串</returns>
  18. private static string GetPrivateKey(string appId)
  19. {
  20. IDictionary<string, string> json = JsonConvert.DeserializeObject<IDictionary<string, string>>(
  21. File.ReadAllText(GetSolutionBasePath() + "/UnitTest/Fixture/privateKey.json"));
  22. return json[appId];
  23. }
  24. /// <summary>
  25. /// 获取解决方案所在路径
  26. /// </summary>
  27. /// <returns>解决方案所在绝对路径</returns>
  28. public static string GetSolutionBasePath()
  29. {
  30. string current = Directory.GetCurrentDirectory();
  31. do
  32. {
  33. current = Directory.GetParent(current).ToString();
  34. }
  35. while (!current.EndsWith("bin", StringComparison.Ordinal));
  36. return current + "/../..";
  37. }
  38. /// <summary>
  39. /// 线上小程序测试账号
  40. /// </summary>
  41. public static class Mini
  42. {
  43. public static Config CONFIG = GetConfig();
  44. public static Config GetConfig()
  45. {
  46. return new Config
  47. {
  48. Protocol = "https",
  49. GatewayHost = "openapi.alipay.com",
  50. AppId = "<-- 请填写您的AppId,例如:2019022663440152 -->",
  51. SignType = "RSA2",
  52. AlipayPublicKey = "<-- 请填写您的支付宝公钥,例如:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAumX1EaLM4ddn1Pia4SxTRb62aVYxU8I2mHMqrc"
  53. + "pQU6F01mIO/DjY7R4xUWcLi0I2oH/BK/WhckEDCFsGrT7mO+JX8K4sfaWZx1aDGs0m25wOCNjp+DCVBXotXSCurqgGI/9UrY+"
  54. + "QydYDnsl4jB65M3p8VilF93MfS01omEDjUW+1MM4o3FP0khmcKsoHnYGs21btEeh0LK1gnnTDlou6Jwv3Ew36CbCNY2cYkuyP"
  55. + "AW0j47XqzhWJ7awAx60fwgNBq6ZOEPJnODqH20TAdTLNxPSl4qGxamjBO+RuInBy+Bc2hFHq3pNv6hTAfktggRKkKzDlDEUwg"
  56. + "SLE7d2eL7P6rwIDAQAB -->",
  57. MerchantPrivateKey = GetPrivateKey("<-- 请填写您的AppId,例如:2019022663440152 -->"),
  58. NotifyUrl = "<-- 请填写您的异步通知接收服务器地址,例如:https://www.test.com/callback"
  59. };
  60. }
  61. }
  62. /// <summary>
  63. /// 线上生活号测试账号
  64. /// </summary>
  65. public static class OpenLife
  66. {
  67. public static Config CONFIG = new Config
  68. {
  69. Protocol = "https",
  70. GatewayHost = "openapi.alipay.com",
  71. AppId = "<-- 请填写您的AppId,例如:2019051064521003 -->",
  72. SignType = "RSA2",
  73. AlipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径,例如:GetSolutionBasePath() + \"/UnitTest/Fixture/alipayCertPublicKey_RSA2.crt\" -->",
  74. AlipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径,例如:GetSolutionBasePath() + \"/UnitTest/Fixture/alipayRootCert.crt\" -->",
  75. MerchantCertPath = "<-- 请填写您的应用公钥证书文件路径,例如:GetSolutionBasePath() + \"/UnitTest/Fixture/appCertPublicKey_2019051064521003.crt\" -->",
  76. MerchantPrivateKey = GetPrivateKey("<-- 请填写您的AppId,例如:2019051064521003 -->")
  77. };
  78. }
  79. }
  80. }