SubmitProcess.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Threading.Tasks;
  4. using PaySharp.Core;
  5. using PaySharp.Core.Exceptions;
  6. using PaySharp.Core.Request;
  7. using PaySharp.Core.Response;
  8. using PaySharp.Core.Utils;
  9. using PaySharp.Wechatpay.Request;
  10. using PaySharp.Wechatpay.Response;
  11. namespace PaySharp.Wechatpay
  12. {
  13. internal static class SubmitProcess
  14. {
  15. private static string _gatewayUrl;
  16. internal static TResponse Execute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl = null) where TResponse : IResponse
  17. {
  18. AddMerchant(merchant, request, gatewayUrl);
  19. var sign = BuildSign(request.GatewayData, merchant.Key, request.GatewayData.GetStringValue("sign_type") == "HMAC-SHA256");
  20. request.GatewayData.Add("sign", sign);
  21. X509Certificate2 cert = null;
  22. if (((BaseRequest<TModel, TResponse>)request).IsUseCert)
  23. {
  24. cert = new X509Certificate2(merchant.SslCertPath, merchant.SslCertPassword, X509KeyStorageFlags.MachineKeySet);
  25. }
  26. var result = HttpUtil.Post(request.RequestUrl, request.GatewayData.ToXml(), cert);
  27. BaseResponse baseResponse;
  28. if (!(request is BillDownloadRequest || request is FundFlowDownloadRequest))
  29. {
  30. var gatewayData = new GatewayData();
  31. gatewayData.FromXml(result);
  32. baseResponse = (BaseResponse)(object)gatewayData.ToObject<TResponse>(StringCase.Snake);
  33. baseResponse.Raw = result;
  34. baseResponse.GatewayData = gatewayData;
  35. if (baseResponse.ReturnCode == "SUCCESS")
  36. {
  37. sign = gatewayData.GetStringValue("sign");
  38. if (!string.IsNullOrEmpty(sign) && !CheckSign(gatewayData, merchant.Key, sign))
  39. {
  40. throw new GatewayException("签名验证失败");
  41. }
  42. baseResponse.Sign = sign;
  43. baseResponse.Execute(merchant, request);
  44. }
  45. }
  46. else
  47. {
  48. baseResponse = (BaseResponse)Activator.CreateInstance(typeof(TResponse));
  49. baseResponse.Raw = result;
  50. baseResponse.Execute(merchant, request);
  51. }
  52. return (TResponse)(object)baseResponse;
  53. }
  54. internal static TResponse AuthExecute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl = null) where TResponse : IResponse
  55. {
  56. AddMerchant(merchant, request, gatewayUrl);
  57. string result = null;
  58. Task.Run(async () =>
  59. {
  60. result = await HttpUtil
  61. .GetAsync($"{request.RequestUrl}?{request.GatewayData.ToUrl()}");
  62. })
  63. .GetAwaiter()
  64. .GetResult();
  65. var gatewayData = new GatewayData();
  66. gatewayData.FromJson(result);
  67. var baseResponse = (OAuthResponse)(object)gatewayData.ToObject<TResponse>(StringCase.Snake);
  68. baseResponse.Raw = result;
  69. baseResponse.GatewayData = gatewayData;
  70. return (TResponse)(object)baseResponse;
  71. }
  72. private static void AddMerchant<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl) where TResponse : IResponse
  73. {
  74. if (!string.IsNullOrEmpty(gatewayUrl))
  75. {
  76. _gatewayUrl = gatewayUrl;
  77. }
  78. if (!request.RequestUrl.StartsWith("http"))
  79. {
  80. request.RequestUrl = _gatewayUrl + request.RequestUrl;
  81. }
  82. request.GatewayData.Add(merchant, StringCase.Snake);
  83. ((BaseRequest<TModel, TResponse>)request).Execute(merchant);
  84. }
  85. internal static string BuildSign(GatewayData gatewayData, string key, bool isHMACSHA256 = false)
  86. {
  87. gatewayData.Remove("sign");
  88. var data = $"{gatewayData.ToUrl(false)}&key={key}";
  89. return isHMACSHA256 ? EncryptUtil.HMACSHA256(data, key) : EncryptUtil.MD5(data);
  90. }
  91. internal static bool CheckSign(GatewayData gatewayData, string key, string sign)
  92. {
  93. return BuildSign(gatewayData, key) == sign;
  94. }
  95. }
  96. }