SubmitProcess.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using PaySharp.Alipay.Response;
  5. using PaySharp.Core;
  6. using PaySharp.Core.Exceptions;
  7. using PaySharp.Core.Request;
  8. using PaySharp.Core.Response;
  9. using PaySharp.Core.Utils;
  10. namespace PaySharp.Alipay
  11. {
  12. internal static class SubmitProcess
  13. {
  14. private static string _gatewayUrl;
  15. internal static TResponse Execute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl = null) where TResponse : IResponse
  16. {
  17. AddMerchant(merchant, request, gatewayUrl);
  18. var result = HttpUtil.Post(request.RequestUrl, request.GatewayData.ToUrl());
  19. var jObject = JObject.Parse(result);
  20. var jToken = jObject.First.First;
  21. var sign = jObject.Value<string>("sign");
  22. if (!string.IsNullOrEmpty(sign) &&
  23. !CheckSign(
  24. jToken.ToString(Formatting.None),
  25. sign,
  26. merchant.CertEnvironment == null ? merchant.AlipayPublicKey : merchant.CertEnvironment.GetAlipayPublicKey(jObject.Value<string>("alipay_cert_sn")),
  27. merchant.SignType
  28. )
  29. )
  30. {
  31. throw new GatewayException("签名验证失败");
  32. }
  33. var baseResponse = (BaseResponse)jToken.ToObject(typeof(TResponse));
  34. baseResponse.Raw = result;
  35. baseResponse.Sign = sign;
  36. baseResponse.Execute(merchant, request);
  37. return (TResponse)(object)baseResponse;
  38. }
  39. internal static TResponse SdkExecute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl) where TResponse : IResponse
  40. {
  41. AddMerchant(merchant, request, gatewayUrl);
  42. return (TResponse)Activator.CreateInstance(typeof(TResponse), request);
  43. }
  44. private static void AddMerchant<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request, string gatewayUrl) where TResponse : IResponse
  45. {
  46. if (!string.IsNullOrEmpty(gatewayUrl))
  47. {
  48. _gatewayUrl = gatewayUrl;
  49. }
  50. if (!request.RequestUrl.StartsWith("http"))
  51. {
  52. request.RequestUrl = _gatewayUrl + request.RequestUrl;
  53. }
  54. request.GatewayData.Add(merchant, StringCase.Snake);
  55. if (merchant.CertEnvironment != null)
  56. {
  57. request.GatewayData.Add("app_cert_sn", merchant.CertEnvironment.MerchantCertSN);
  58. request.GatewayData.Add("alipay_root_cert_sn", merchant.CertEnvironment.RootCertSN);
  59. }
  60. if (!string.IsNullOrEmpty(request.NotifyUrl))
  61. {
  62. request.GatewayData.Add("notify_url", request.NotifyUrl);
  63. }
  64. if (!string.IsNullOrEmpty(request.ReturnUrl))
  65. {
  66. request.GatewayData.Add("return_url", request.ReturnUrl);
  67. }
  68. request.GatewayData.Add("sign", BuildSign(request.GatewayData, merchant.Privatekey));
  69. }
  70. internal static string BuildSign(GatewayData gatewayData, string privatekey)
  71. {
  72. gatewayData.Remove("sign");
  73. return EncryptUtil.Sign(gatewayData.ToUrl(false), privatekey);
  74. }
  75. internal static bool CheckSign(string data, string sign, string alipayPublicKey, string signType)
  76. {
  77. return EncryptUtil.Verify(data, sign, alipayPublicKey);
  78. }
  79. }
  80. }