123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web;
- using System.IO;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Alipay.EasySDK.Kernel.Util;
- using Tea;
- namespace Alipay.EasySDK.Kernel
- {
-
-
-
- public class Client
- {
-
-
-
- private readonly Context context;
-
-
-
- private readonly Dictionary<string, string> optionalTextParams = new Dictionary<string, string>();
-
-
-
- private readonly Dictionary<string, object> optionalBizParams = new Dictionary<string, object>();
-
-
-
-
- public Client(Context context)
- {
- this.context = context;
- }
-
-
-
-
-
-
- public Client InjectTextParam(String key, String value)
- {
- optionalTextParams.Add(key, value);
- return this;
- }
-
-
-
-
-
-
- public Client InjectBizParam(String key, Object value)
- {
- optionalBizParams.Add(key, value);
- return this;
- }
-
-
-
-
-
- public string GetConfig(string key)
- {
- return context.GetConfig(key);
- }
-
-
-
-
- public bool IsCertMode()
- {
- return context.CertEnvironment != null;
- }
-
-
-
-
- public string GetTimestamp()
- {
- return DateTime.UtcNow.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
-
- public string Sign(Dictionary<string, string> systemParams, Dictionary<string, object> bizParams,
- Dictionary<string, string> textParams, string privateKey)
- {
- IDictionary<string, string> sortedMap = GetSortedMap(systemParams, bizParams, textParams);
- StringBuilder content = new StringBuilder();
- foreach (var pair in sortedMap)
- {
- if (!string.IsNullOrEmpty(pair.Key) && !string.IsNullOrEmpty(pair.Value))
- {
- content.Append(pair.Key).Append("=").Append(pair.Value).Append("&");
- }
- }
- if (content.Length > 0)
- {
-
- content.Remove(content.Length - 1, 1);
- }
- return Signer.Sign(content.ToString(), privateKey);
- }
- private IDictionary<string, string> GetSortedMap(Dictionary<string, string> systemParams,
- Dictionary<string, object> bizParams, Dictionary<string, string> textParams)
- {
- AddOtherParams(textParams, bizParams);
- IDictionary<string, string> sortedMap = new SortedDictionary<string, string>(systemParams, StringComparer.Ordinal);
- if (bizParams != null && bizParams.Count != 0)
- {
- sortedMap.Add(AlipayConstants.BIZ_CONTENT_FIELD, JsonUtil.ToJsonString(bizParams));
- }
- if (textParams != null)
- {
- foreach (var pair in textParams)
- {
- sortedMap.Add(pair.Key, pair.Value);
- }
- }
- SetNotifyUrl(sortedMap);
- return sortedMap;
- }
- private void SetNotifyUrl(IDictionary<string, string> paramters)
- {
- if (GetConfig(AlipayConstants.NOTIFY_URL_CONFIG_KEY) != null && !paramters.ContainsKey(AlipayConstants.NOTIFY_URL_FIELD))
- {
- paramters.Add(AlipayConstants.NOTIFY_URL_FIELD, GetConfig(AlipayConstants.NOTIFY_URL_CONFIG_KEY));
- }
- }
-
-
-
-
- public string GetMerchantCertSN()
- {
- if (context.CertEnvironment == null)
- {
- return null;
- }
- return context.CertEnvironment.MerchantCertSN;
- }
-
-
-
-
- public string GetAlipayRootCertSN()
- {
- if (context.CertEnvironment == null)
- {
- return null;
- }
- return context.CertEnvironment.RootCertSN;
- }
-
-
-
-
-
- public byte[] ToUrlEncodedRequestBody(Dictionary<string, object> bizParams)
- {
- IDictionary<string, string> sortedMap = GetSortedMap(new Dictionary<string, string>(), bizParams, null);
- return AlipayConstants.DEFAULT_CHARSET.GetBytes(BuildQueryString(sortedMap));
- }
- private string BuildQueryString(IDictionary<string, string> sortedMap)
- {
- StringBuilder content = new StringBuilder();
- int index = 0;
- foreach (var pair in sortedMap)
- {
- if (!string.IsNullOrEmpty(pair.Key) && !string.IsNullOrEmpty(pair.Value))
- {
- content.Append(index == 0 ? "" : "&")
- .Append(pair.Key)
- .Append("=")
- .Append(HttpUtility.UrlEncode(pair.Value, AlipayConstants.DEFAULT_CHARSET));
- index++;
- }
- }
- return content.ToString();
- }
-
-
-
-
- public string GetRandomBoundary()
- {
- return DateTime.Now.Ticks.ToString("X");
- }
-
-
-
-
-
-
- public string ConcatStr(string a, string b)
- {
- return a + b;
- }
-
-
-
-
-
-
-
- public Stream ToMultipartRequestBody(Dictionary<string, string> textParams, Dictionary<string, string> fileParams, string boundary)
- {
- MemoryStream stream = new MemoryStream();
-
- AddOtherParams(textParams, null);
- foreach (var pair in textParams)
- {
- if (!string.IsNullOrEmpty(pair.Key) && !string.IsNullOrEmpty(pair.Value))
- {
- MultipartUtil.WriteToStream(stream, MultipartUtil.GetEntryBoundary(boundary));
- MultipartUtil.WriteToStream(stream, MultipartUtil.GetTextEntry(pair.Key, pair.Value));
- }
- }
-
- foreach (var pair in fileParams)
- {
- if (!string.IsNullOrEmpty(pair.Key) && pair.Value != null)
- {
- MultipartUtil.WriteToStream(stream, MultipartUtil.GetEntryBoundary(boundary));
- MultipartUtil.WriteToStream(stream, MultipartUtil.GetFileEntry(pair.Key, pair.Value));
- MultipartUtil.WriteToStream(stream, File.ReadAllBytes(pair.Value));
- }
- }
-
- MultipartUtil.WriteToStream(stream, MultipartUtil.GetEndBoundary(boundary));
- stream.Seek(0, SeekOrigin.Begin);
- return stream;
- }
-
-
-
-
-
-
- public Dictionary<string, object> ReadAsJson(TeaResponse response, string method)
- {
- string responseBody = TeaCore.GetResponseBody(response);
- Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseBody);
- dictionary.Add(AlipayConstants.BODY_FIELD, responseBody);
- dictionary.Add(AlipayConstants.METHOD_FIELD, method);
- return DictionaryUtil.ObjToDictionary(dictionary);
- }
-
-
-
- public async Task<Dictionary<string, object>> ReadAsJsonAsync(TeaResponse response, string method)
- {
- return ReadAsJson(response, method);
- }
-
-
-
-
-
- public string GetAlipayCertSN(Dictionary<string, object> respMap)
- {
- return (string)respMap[AlipayConstants.ALIPAY_CERT_SN_FIELD];
- }
-
-
-
-
-
-
- public string ExtractAlipayPublicKey(string alipayCertSN)
- {
- if (context.CertEnvironment == null)
- {
- return null;
- }
- return context.CertEnvironment.GetAlipayPublicKey(alipayCertSN);
- }
-
-
-
-
-
-
- public bool Verify(Dictionary<string, object> respMap, string alipayPublicKey)
- {
- string sign = (string)respMap[AlipayConstants.SIGN_FIELD];
- string content = SignContentExtractor.GetSignSourceData((string)respMap[AlipayConstants.BODY_FIELD],
- (string)respMap[AlipayConstants.METHOD_FIELD]);
- return Signer.Verify(content, sign, alipayPublicKey);
- }
-
-
-
-
-
- public Dictionary<string, object> ToRespModel(Dictionary<string, object> respMap)
- {
- string methodName = (string)respMap[AlipayConstants.METHOD_FIELD];
- string responseNodeName = methodName.Replace('.', '_') + AlipayConstants.RESPONSE_SUFFIX;
- string errorNodeName = AlipayConstants.ERROR_RESPONSE;
-
- foreach (var pair in respMap)
- {
- if (responseNodeName.Equals(pair.Key))
- {
- Dictionary<string, object> model = (Dictionary<string, object>)pair.Value;
- model.Add(AlipayConstants.BODY_FIELD, respMap[AlipayConstants.BODY_FIELD]);
- return model;
- }
- }
-
- foreach (var pair in respMap)
- {
- if (errorNodeName.Equals(pair.Key))
- {
- Dictionary<string, object> model = (Dictionary<string, object>)pair.Value;
- model.Add(AlipayConstants.BODY_FIELD, respMap[AlipayConstants.BODY_FIELD]);
- return model;
- }
- }
- throw new Exception("响应格式不符合预期,找不到" + responseNodeName + "节点");
- }
-
-
-
-
-
-
-
-
-
- public string GeneratePage(string method, Dictionary<string, string> systemParams, Dictionary<string, object> bizParams,
- Dictionary<string, string> textParams, string sign)
- {
- if (AlipayConstants.GET.Equals(method))
- {
-
- IDictionary<string, string> sortedMap = GetSortedMap(systemParams, bizParams, textParams);
- sortedMap.Add(AlipayConstants.SIGN_FIELD, sign);
-
- return GetGatewayServerUrl() + "?" + BuildQueryString(sortedMap);
- }
- else if (AlipayConstants.POST.Equals(method))
- {
-
- IDictionary<string, string> urlParams = GetSortedMap(systemParams, null, textParams);
- urlParams.Add(AlipayConstants.SIGN_FIELD, sign);
- string actionUrl = GetGatewayServerUrl() + "?" + BuildQueryString(urlParams);
-
- AddOtherParams(null, bizParams);
- IDictionary<string, string> formParams = new SortedDictionary<string, string>()
- {
- { AlipayConstants.BIZ_CONTENT_FIELD, JsonUtil.ToJsonString(bizParams)}
- };
- return PageUtil.BuildForm(actionUrl, formParams);
- }
- else
- {
- throw new Exception("_generatePage中method只支持传入GET或POST");
- }
- }
-
-
-
-
-
-
-
-
- public string GenerateOrderString(Dictionary<string, string> systemParams, Dictionary<string, object> bizParams,
- Dictionary<string, string> textParams, string sign)
- {
-
- IDictionary<string, string> sortedMap = GetSortedMap(systemParams, bizParams, textParams);
- sortedMap.Add(AlipayConstants.SIGN_FIELD, sign);
-
- return BuildQueryString(sortedMap);
- }
- private string GetGatewayServerUrl()
- {
- return GetConfig(AlipayConstants.PROTOCOL_CONFIG_KEY) + "://" + GetConfig(AlipayConstants.HOST_CONFIG_KEY) + "/gateway.do";
- }
-
-
-
-
-
-
- public string AesEncrypt(string plainText, string key)
- {
- return AES.Encrypt(plainText, key);
- }
-
-
-
-
-
-
- public string AesDecrypt(string chiperText, string key)
- {
- return AES.Decrypt(chiperText, key);
- }
-
-
-
-
-
-
- public bool VerifyParams(Dictionary<string, string> parameters, string alipayPublicKey)
- {
- return Signer.VerifyParams(parameters, alipayPublicKey);
- }
-
-
-
-
- public string GetSdkVersion()
- {
- return context.SdkVersion;
- }
-
-
-
-
-
- public Dictionary<string, string> SortMap(Dictionary<string, string> input)
- {
-
- return input;
- }
- private void AddOtherParams(Dictionary<string, string> textParams, Dictionary<string, object> bizParams)
- {
-
- if (textParams != null)
- {
- foreach (var pair in optionalTextParams)
- {
- if (!textParams.ContainsKey(pair.Key))
- {
- textParams.Add(pair.Key, pair.Value);
- }
- }
- SetNotifyUrl(textParams);
- }
-
- if (bizParams != null)
- {
- foreach (var pair in optionalBizParams)
- {
- if (!bizParams.ContainsKey(pair.Key))
- {
- bizParams.Add(pair.Key, pair.Value);
- }
- }
- }
- }
- }
- }
|