ServiceCollectionExtensions.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #if NETCOREAPP3_1
  2. using System;
  3. using Microsoft.Extensions.Configuration;
  4. using PaySharp.Alipay;
  5. using PaySharp.Core;
  6. namespace Microsoft.Extensions.DependencyInjection
  7. {
  8. public static class ServiceCollectionExtensions
  9. {
  10. public static IGateways UseAlipay(this IGateways gateways, Action<Merchant> action)
  11. {
  12. if (action != null)
  13. {
  14. var merchant = new Merchant();
  15. action(merchant);
  16. gateways.Add(new AlipayGateway(merchant));
  17. }
  18. return gateways;
  19. }
  20. public static IGateways UseAlipay(this IGateways gateways, IConfiguration configuration)
  21. {
  22. var merchants = configuration.GetSection("PaySharp:Alipays").Get<Merchant[]>();
  23. if (merchants != null)
  24. {
  25. for (var i = 0; i < merchants.Length; i++)
  26. {
  27. var alipayGateway = new AlipayGateway(merchants[i]);
  28. var gatewayUrl = configuration.GetSection($"PaySharp:Alipays:{i}:GatewayUrl").Value;
  29. if (!string.IsNullOrEmpty(gatewayUrl))
  30. {
  31. alipayGateway.GatewayUrl = gatewayUrl;
  32. }
  33. gateways.Add(alipayGateway);
  34. }
  35. }
  36. return gateways;
  37. }
  38. }
  39. }
  40. #endif