AlipayNotifyClient.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if NETCOREAPP3_1
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Essensoft.AspNetCore.Payment.Alipay.Parser;
  7. using Essensoft.AspNetCore.Payment.Alipay.Utility;
  8. using Microsoft.AspNetCore.Http;
  9. namespace Essensoft.AspNetCore.Payment.Alipay
  10. {
  11. public class AlipayNotifyClient : IAlipayNotifyClient
  12. {
  13. #region AlipayNotifyClient Constructors
  14. public AlipayNotifyClient()
  15. {
  16. }
  17. #endregion
  18. #region IAlipayNotifyClient Members
  19. public Task<T> ExecuteAsync<T>(HttpRequest request, AlipayOptions options) where T : AlipayNotify
  20. {
  21. if (options == null)
  22. {
  23. throw new ArgumentNullException(nameof(options));
  24. }
  25. if (string.IsNullOrEmpty(options.SignType))
  26. {
  27. throw new ArgumentNullException(nameof(options.SignType));
  28. }
  29. if (string.IsNullOrEmpty(options.AppPrivateKey))
  30. {
  31. throw new ArgumentNullException(nameof(options.AppPrivateKey));
  32. }
  33. var parameters = GetParameters(request);
  34. var rsp = AlipayDictionaryParser.Parse<T>(parameters);
  35. CheckNotifySign(parameters, options);
  36. return Task.FromResult(rsp);
  37. }
  38. #endregion
  39. #region Common Method
  40. private Dictionary<string, string> GetParameters(HttpRequest request)
  41. {
  42. var parameters = new Dictionary<string, string>();
  43. if (request.Method == "POST")
  44. {
  45. foreach (var iter in request.Form)
  46. {
  47. parameters.Add(iter.Key, iter.Value);
  48. }
  49. }
  50. else
  51. {
  52. foreach (var iter in request.Query)
  53. {
  54. parameters.Add(iter.Key, iter.Value);
  55. }
  56. }
  57. return parameters;
  58. }
  59. private void CheckNotifySign(IDictionary<string, string> dictionary, AlipayOptions options)
  60. {
  61. if (dictionary == null || dictionary.Count == 0)
  62. {
  63. throw new AlipayException("sign check fail: dictionary is Empty!");
  64. }
  65. if (!dictionary.TryGetValue("sign", out var sign))
  66. {
  67. throw new AlipayException("sign check fail: sign is Empty!");
  68. }
  69. var prestr = GetSignContent(dictionary);
  70. if (!AlipaySignature.RSACheckContent(prestr, sign, options.AlipayPublicKey, options.SignType))
  71. {
  72. throw new AlipayException("sign check fail: check Sign Data Fail!");
  73. }
  74. }
  75. private static string GetSignContent(IDictionary<string, string> dictionary)
  76. {
  77. if (dictionary == null || dictionary.Count == 0)
  78. {
  79. throw new ArgumentNullException(nameof(dictionary));
  80. }
  81. var sortPara = new SortedDictionary<string, string>(dictionary);
  82. var sb = new StringBuilder();
  83. foreach (var iter in sortPara)
  84. {
  85. if (!string.IsNullOrEmpty(iter.Value) && iter.Key != "sign" && iter.Key != "sign_type")
  86. {
  87. sb.Append(iter.Key).Append("=").Append(iter.Value).Append("&");
  88. }
  89. }
  90. return sb.Remove(sb.Length - 1, 1).ToString();
  91. }
  92. #endregion
  93. }
  94. }
  95. #endif