Startup.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Essensoft.AspNetCore.Payment.Alipay;
  2. using Essensoft.AspNetCore.Payment.JdPay;
  3. using Essensoft.AspNetCore.Payment.QPay;
  4. using Essensoft.AspNetCore.Payment.UnionPay;
  5. using Essensoft.AspNetCore.Payment.WeChatPay;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using System.Text.Encodings.Web;
  11. using System.Text.Unicode;
  12. namespace WebApplicationSample
  13. {
  14. public class Startup
  15. {
  16. public Startup(IConfiguration configuration)
  17. {
  18. Configuration = configuration;
  19. }
  20. public IConfiguration Configuration { get; }
  21. // This method gets called by the runtime. Use this method to add services to the container.
  22. public void ConfigureServices(IServiceCollection services)
  23. {
  24. services.AddMvc();
  25. // 添加依赖注入
  26. // 依赖注入介绍:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection
  27. services.AddAlipay();
  28. services.AddJdPay();
  29. services.AddQPay();
  30. services.AddUnionPay();
  31. services.AddWeChatPay();
  32. // 可在添加依赖注入时设置参数
  33. // 例如:
  34. //services.AddAlipay(opt =>
  35. //{
  36. // //一般设置 AppId、RsaPrivateKey、RsaPublicKey,其余默认即可.
  37. // //此处为蚂蚁金服开放平台上创建的APPID,而非老版本的商户号
  38. // opt.AppId = "";
  39. // // 这里的公私钥 默认均为支付宝官方推荐使用的RSAWithSHA256.
  40. // // 商户私钥
  41. // opt.RsaPrivateKey = "";
  42. // // 支付宝公钥
  43. // opt.RsaPublicKey = "";
  44. //});
  45. // 配置介绍: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration?tabs=basicconfiguration
  46. // 用户机密介绍: https://docs.microsoft.com/zh-cn/aspnet/core/security/app-secrets?tabs=visual-studio
  47. // 注册配置实例(使用配置文件或用户机密的方式设置参数)
  48. services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
  49. services.Configure<JdPayOptions>(Configuration.GetSection("JdPay"));
  50. services.Configure<QPayOptions>(Configuration.GetSection("QPay"));
  51. services.Configure<UnionPayOptions>(Configuration.GetSection("UnionPay"));
  52. services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
  53. services.AddWebEncoders(opt =>
  54. {
  55. opt.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
  56. });
  57. // Json格式 配置参数. 具体参数见 AlipayOptions、JdPayOptions、QPayOptions、UnionPayOptions、WeChatPayOptions类
  58. //{
  59. // "Alipay": {
  60. // "AppId": "xxx",
  61. // "RsaPublicKey": "xxx",
  62. // "RsaPrivateKey": "xxx",
  63. // "SignType" : "RSA2"
  64. // },
  65. // "JdPay": {
  66. // "Merchant": "xxx",
  67. // "RsaPublicKey": "xxx",
  68. // "RsaPrivateKey": "xxx",
  69. // "DesKey": "xxx"
  70. // },
  71. // "QPay": {
  72. // "MchId": "xxx",
  73. // "Key": "xxx",
  74. // "Certificate": "xxx",
  75. // },
  76. // "UnionPay": {
  77. // "MerId": "xxx",
  78. // "SignCert": "xxx",
  79. // "SignCertPassword": "xxx",
  80. // "EncryptCert": "xxx",
  81. // "MiddleCert": "xxx",
  82. // "RootCert": "xxx",
  83. // "SecureKey": "xxx",
  84. // },
  85. // "WeChatPay": {
  86. // "AppId": "xxx",
  87. // "MchId": "xxx",
  88. // "Key": "xxx",
  89. // "Certificate": "xxx",
  90. // "RsaPublicKey": "xxx",
  91. // }
  92. //}
  93. }
  94. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  95. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  96. {
  97. if (env.IsDevelopment())
  98. {
  99. app.UseBrowserLink();
  100. app.UseDeveloperExceptionPage();
  101. }
  102. else
  103. {
  104. app.UseExceptionHandler("/Home/Error");
  105. }
  106. app.UseStaticFiles();
  107. app.UseMvc(routes =>
  108. {
  109. routes.MapRoute(
  110. name: "default",
  111. template: "{controller=Home}/{action=Index}/{id?}");
  112. });
  113. }
  114. }
  115. }