public async Task <T> ExecuteAsync <T>(IAlipayRequest <T> request, string accessToken, string appAuthToken) where T : AlipayResponse { var multiMediaDownloadRequest = ((AlipayMobilePublicMultiMediaDownloadRequest)request); // 添加协议级请求参数 var txtParams = new AlipayDictionary(request.GetParameters()) { { METHOD, request.GetApiName() }, { VERSION, Options.Version }, { APP_ID, Options.AppId }, { FORMAT, Options.Format }, { TIMESTAMP, DateTime.Now }, { ACCESS_TOKEN, accessToken }, { SIGN_TYPE, Options.SignType }, { TERMINAL_TYPE, request.GetTerminalType() }, { TERMINAL_INFO, request.GetTerminalInfo() }, { PROD_CODE, request.GetProdCode() } }; if (!string.IsNullOrEmpty(appAuthToken)) { txtParams.Add(APP_AUTH_TOKEN, appAuthToken); } // 添加签名参数 txtParams.Add(SIGN, AlipaySignature.RSASign(txtParams, RSAPrivateParameters, Options.SignType)); var outStream = multiMediaDownloadRequest.Stream; var rsp = await DoGetAsync(txtParams, outStream); return((T)rsp); }
public Task <T> SdkExecuteAsync <T>(IAlipayRequest <T> request) where T : AlipayResponse { // 构造请求参数 var requestParams = BuildRequestParams(request, null, null); // 字典排序 var sortedParams = new SortedDictionary <string, string>(requestParams); var sortedAlipayDic = new AlipayDictionary(sortedParams); // 参数签名 var signContent = AlipaySignature.GetSignContent(sortedAlipayDic); var signResult = AlipaySignature.RSASignContent(signContent, PrivateRSAParameters, Options.SignType); // 添加签名结果参数 sortedAlipayDic.Add(SIGN, signResult); // 参数拼接 var signedResult = HttpClientEx.BuildQuery(sortedAlipayDic); // 构造结果 var rsp = Activator.CreateInstance <T>(); rsp.Body = signedResult; return(Task.FromResult(rsp)); }
public Task <T> SdkExecuteAsync <T>(IAlipayRequest <T> request, string optionsName) where T : AlipayResponse { var options = string.IsNullOrEmpty(optionsName) ? _optionsSnapshotAccessor.Value : _optionsSnapshotAccessor.Get(optionsName); // 构造请求参数 var requestParams = BuildRequestParams(request, null, null, options); // 字典排序 var sortedParams = new SortedDictionary <string, string>(requestParams); var sortedAlipayDic = new AlipayDictionary(sortedParams); // 参数签名 var signContent = AlipaySignature.GetSignContent(sortedAlipayDic); var signResult = AlipaySignature.RSASignContent(signContent, options.PrivateRSAParameters, options.SignType); // 添加签名结果参数 sortedAlipayDic.Add(SIGN, signResult); // 参数拼接 var signedResult = AlipayUtility.BuildQuery(sortedAlipayDic); // 构造结果 var rsp = Activator.CreateInstance <T>(); rsp.Body = signedResult; return(Task.FromResult(rsp)); }
public AlipayPurchaseService(IAlipayRequest alipayRequest, IPurchaseService purchaseService, IRepository <UserPayOrderInfo, string> uerPayOrderInfoRepository, IPurchaseServiceRepository purchaseServiceRepository) { _alipayRequest = alipayRequest; _purchaseService = purchaseService; _uerPayOrderInfoRepository = uerPayOrderInfoRepository; _purchaseServiceRepository = purchaseServiceRepository; }
private void CheckResponseSign <T>(IAlipayRequest <T> request, string responseBody, bool isError, IAlipayParser <T> parser, AlipayOptions options) where T : AlipayResponse { var signItem = parser.GetSignItem(request, responseBody); if (signItem == null) { throw new AlipayException("sign check fail: Body is Empty!"); } if (!isError || isError && !string.IsNullOrEmpty(signItem.Sign)) { var rsaCheckContent = AlipaySignature.RSACheckContent(signItem.SignSourceDate, signItem.Sign, options.AlipayPublicKey, options.SignType); if (!rsaCheckContent) { if (!string.IsNullOrEmpty(signItem.SignSourceDate) && signItem.SignSourceDate.Contains("\\/")) { var srouceData = signItem.SignSourceDate.Replace("\\/", "/"); var jsonCheck = AlipaySignature.RSACheckContent(srouceData, signItem.Sign, options.AlipayPublicKey, options.SignType); if (!jsonCheck) { throw new AlipayException("sign check fail: check Sign and Data Fail JSON also"); } } else { throw new AlipayException("sign check fail: check Sign and Data Fail!"); } } } }
private ResponseParseItem ParseRespItem <T>(IAlipayRequest <T> request, string respBody, IAlipayParser <T> parser, string encryptKey, string encryptType) where T : AlipayResponse { try { logger?.LogDebug($"{DateTime.Now} 支付宝ParseRespItem"); string realContent = null; if (request.GetNeedEncrypt()) { realContent = parser.EncryptSourceData(request, respBody, encryptType, encryptKey); } else { realContent = respBody; } var item = new ResponseParseItem() { realContent = realContent, respContent = respBody }; return(item); } catch (Exception ex) { logger?.LogError($"{DateTime.Now} 支付宝ParseRespItem报错", ex.Message); return(null); } }
private AlipayDictionary BuildRequestParams <T>(IAlipayRequest <T> request, string accessToken, string appAuthToken) where T : AlipayResponse { try { logger?.LogDebug($"{DateTime.Now} 支付宝BuildRequestParams"); // 默认参数 var result = new AlipayDictionary(request.GetParameters()) { // 序列化BizModel { BIZ_CONTENT, Serialize(request.GetBizModel()) }, // 添加协议级请求参数,为空的参数后面会自动过滤,这里不做处理。 { METHOD, request.GetApiName() }, { VERSION, string.IsNullOrEmpty(request.GetApiVersion()) ? Options.Version : request.GetApiVersion() }, { APP_ID, Options.AppId }, { FORMAT, Options.Format }, { TIMESTAMP, DateTime.Now }, { ACCESS_TOKEN, accessToken }, { SIGN_TYPE, Options.SignType }, { TERMINAL_TYPE, request.GetTerminalType() }, { TERMINAL_INFO, request.GetTerminalInfo() }, { PROD_CODE, request.GetProdCode() }, { NOTIFY_URL, request.GetNotifyUrl() }, { CHARSET, Options.Charset }, { RETURN_URL, request.GetReturnUrl() }, { APP_AUTH_TOKEN, appAuthToken } }; if (request.GetNeedEncrypt()) { if (string.IsNullOrEmpty(result[BIZ_CONTENT])) { throw new Exception("api request Fail ! The reason: encrypt request is not supported!"); } if (string.IsNullOrEmpty(Options.EncyptKey) || string.IsNullOrEmpty(Options.EncyptType)) { throw new Exception("encryptType or encryptKey must not null!"); } if (!"AES".Equals(Options.EncyptType)) { throw new Exception("api only support Aes!"); } var encryptContent = AES.Encrypt(result[BIZ_CONTENT], Options.EncyptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7); result.Remove(BIZ_CONTENT); result.Add(BIZ_CONTENT, encryptContent); result.Add(ENCRYPT_TYPE, Options.EncyptType); } return(result); } catch (Exception ex) { logger?.LogError($"{DateTime.Now} 支付宝BuildRequestParams报错", ex.Message); return(null); } }
public string EncryptSourceData(IAlipayRequest <T> request, string body, string encryptType, string encryptKey) { if (!"AES".Equals(encryptType)) { throw new AlipayException("API only support AES!"); } var item = ParseEncryptData(request, body); var bodyStartContent = body[0..item.StartIndex];
public string EncryptSourceData(IAlipayRequest <T> request, string body, string encryptType, string encryptKey) { var item = ParseEncryptData(request, body); var bodyIndexContent = body.Substring(0, item.startIndex); var bodyEndContent = body.Substring(item.endIndex); var encryptContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7); return(bodyIndexContent + encryptContent + bodyEndContent); }
public string EncryptSourceData(IAlipayRequest <T> request, string body, string encryptType, string encryptKey) { if (!"AES".Equals(encryptType)) { throw new AlipayException("API only support AES!"); } var item = ParseEncryptData(request, body); var bodyIndexContent = body.Substring(0, item.startIndex); var bodyEndexContent = body[item.endIndex..];
private AlipayDictionary BuildRequestParams <T>(IAlipayRequest <T> request, string accessToken, string appAuthToken, AlipayOptions options) where T : AlipayResponse { var apiVersion = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion(); // 添加协议级请求参数 var result = new AlipayDictionary(request.GetParameters()) { { AlipayConstants.METHOD, request.GetApiName() }, { AlipayConstants.VERSION, apiVersion }, { AlipayConstants.APP_ID, options.AppId }, { AlipayConstants.FORMAT, options.Format }, { AlipayConstants.TIMESTAMP, DateTime.Now }, { AlipayConstants.ACCESS_TOKEN, accessToken }, { AlipayConstants.SIGN_TYPE, options.SignType }, { AlipayConstants.TERMINAL_TYPE, request.GetTerminalType() }, { AlipayConstants.TERMINAL_INFO, request.GetTerminalInfo() }, { AlipayConstants.PROD_CODE, request.GetProdCode() }, { AlipayConstants.NOTIFY_URL, request.GetNotifyUrl() }, { AlipayConstants.CHARSET, options.Charset }, { AlipayConstants.RETURN_URL, request.GetReturnUrl() }, { AlipayConstants.APP_AUTH_TOKEN, appAuthToken }, { AlipayConstants.ALIPAY_ROOT_CERT_SN, options.RootCertSN }, { AlipayConstants.APP_CERT_SN, options.AppCertSN } }; // 序列化BizModel result = SerializeBizModel(result, request); if (request.GetNeedEncrypt()) { if (string.IsNullOrEmpty(result[AlipayConstants.BIZ_CONTENT])) { throw new AlipayException("api request Fail ! The reason: encrypt request is not supported!"); } if (string.IsNullOrEmpty(options.EncyptKey) || string.IsNullOrEmpty(options.EncyptType)) { throw new AlipayException("encryptType or encryptKey must not null!"); } if (!"AES".Equals(options.EncyptType)) { throw new AlipayException("api only support Aes!"); } var encryptContent = AlipaySignature.AESEncrypt(result[AlipayConstants.BIZ_CONTENT], options.EncyptKey); result.Remove(AlipayConstants.BIZ_CONTENT); result.Add(AlipayConstants.BIZ_CONTENT, encryptContent); result.Add(AlipayConstants.ENCRYPT_TYPE, options.EncyptType); } return(result); }
private AlipayDictionary BuildRequestParams <T>(IAlipayRequest <T> request, string accessToken, string appAuthToken) where T : AlipayResponse { // 默认参数 var result = new AlipayDictionary(request.GetParameters()) { // 序列化BizModel { BIZ_CONTENT, JsonConvert.SerializeObject(request.GetBizModel(), new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }) }, // 添加协议级请求参数,为空的参数后面会自动过滤,这里不做处理。 { METHOD, request.GetApiName() }, // 获取参数 { VERSION, string.IsNullOrEmpty(request.GetApiVersion()) ? Options.Version : request.GetApiVersion() }, { APP_ID, Options.AppId }, { FORMAT, Options.Format }, { TIMESTAMP, DateTime.Now }, { ACCESS_TOKEN, accessToken }, { SIGN_TYPE, Options.SignType }, { TERMINAL_TYPE, request.GetTerminalType() }, { TERMINAL_INFO, request.GetTerminalInfo() }, { PROD_CODE, request.GetProdCode() }, { NOTIFY_URL, request.GetNotifyUrl() }, { CHARSET, Options.Charset }, { RETURN_URL, request.GetReturnUrl() }, { APP_AUTH_TOKEN, appAuthToken } }; if (request.GetNeedEncrypt()) { if (string.IsNullOrEmpty(result[BIZ_CONTENT])) { throw new AlipayException("api request Fail ! The reason: encrypt request is not supported!"); } if (string.IsNullOrEmpty(Options.EncyptKey) || string.IsNullOrEmpty(Options.EncyptType)) { throw new AlipayException("encryptType or encryptKey must not null!"); } if (!"AES".Equals(Options.EncyptType)) { throw new AlipayException("api only support Aes!"); } var encryptContent = AlipayUtility.AesEncrypt(Options.EncyptKey, result[BIZ_CONTENT]); result.Remove(BIZ_CONTENT); result.Add(BIZ_CONTENT, encryptContent); result.Add(ENCRYPT_TYPE, Options.EncyptType); } return(result); }
public Task <T> SdkExecuteAsync <T>(IAlipayRequest <T> request, AlipayOptions options) where T : AlipayResponse { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(options.AppId)) { throw new ArgumentNullException(nameof(options.AppId)); } if (string.IsNullOrEmpty(options.SignType)) { throw new ArgumentNullException(nameof(options.SignType)); } if (string.IsNullOrEmpty(options.AppPrivateKey)) { throw new ArgumentNullException(nameof(options.AppPrivateKey)); } if (string.IsNullOrEmpty(options.ServerUrl)) { throw new ArgumentNullException(nameof(options.ServerUrl)); } // 构造请求参数 var requestParams = BuildRequestParams(request, null, null, options); // 字典排序 var sortedParams = new SortedDictionary <string, string>(requestParams); var sortedDic = new AlipayDictionary(sortedParams); // 参数签名 var signContent = AlipaySignature.GetSignContent(sortedDic); var signResult = AlipaySignature.RSASignContent(signContent, options.AppPrivateKey, options.SignType); // 添加签名结果参数 sortedDic.Add(AlipayConstants.SIGN, signResult); // 参数拼接 var signedResult = AlipayUtility.BuildQuery(sortedDic); // 构造结果 var rsp = Activator.CreateInstance <T>(); rsp.ResponseBody = signedResult; return(Task.FromResult(rsp)); }
public string EncryptSourceData(IAlipayRequest <T> request, string body, string encryptType, string encryptKey) { if (!"AES".Equals(encryptType)) { throw new AlipayException("API only support AES!"); } var item = ParseEncryptData(request, body); var bodyIndexContent = body.Substring(0, item.startIndex); var bodyEndexContent = body.Substring(item.endIndex); var bizContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, CipherMode.CBC, PaddingMode.PKCS7); return(bodyIndexContent + bizContent + bodyEndexContent); }
public SignItem GetSignItem(IAlipayRequest <T> request, string body) { if (string.IsNullOrEmpty(body)) { return(null); } var signItem = new SignItem { Sign = GetSign(body), SignSourceData = GetSignSourceData(request, body) }; return(signItem); }
public SignItem GetSignItem(IAlipayRequest <T> request, string responseBody) { if (string.IsNullOrEmpty(responseBody)) { return(null); } var signItem = new SignItem() { Sign = GetSign(responseBody), SignSourceDate = GetSignSourceData(request, responseBody) }; return(signItem); }
public CertItem GetCertItem(IAlipayRequest <T> request, string body) { if (string.IsNullOrEmpty(body)) { return(null); } var json = JsonSerializer.Deserialize <IDictionary>(body, JsonParser.JsonSerializerOptions); var certItem = new CertItem() { Sign = json[AlipayConstants.SIGN]?.ToString(), CertSN = json[AlipayConstants.ALIPAY_CERT_SN]?.ToString(), SignSourceData = GetSignSourceData(request, body) }; return(certItem); }
public string EncryptSourceData(IAlipayRequest <T> request, string body, string encryptType, string encryptKey) { if (!"AES".Equals(encryptType)) { throw new AlipayException("API only support AES!"); } var item = ParseEncryptData(request, body); var bodyIndexContent = body.Substring(0, item.startIndex); var bodyEndexContent = body.Substring(item.endIndex); //TODO 解密逻辑 var bizContent = AlipayEncrypt.AesDencrypt(encryptKey, item.encryptContent); return(bodyIndexContent + bizContent + bodyEndexContent); }
private ResponseParseItem ParseRespItem <T>(IAlipayRequest <T> request, string respBody, IAlipayParser <T> parser, string encryptKey, string encryptType) where T : AlipayResponse { string realContent; if (request.GetNeedEncrypt()) { realContent = parser.EncryptSourceData(request, respBody, encryptType, encryptKey); } else { realContent = respBody; } return(new ResponseParseItem { RealContent = realContent, RespContent = respBody }); }
public CertItem GetCertItem(IAlipayRequest <T> request, string responseBody) { if (string.IsNullOrEmpty(responseBody)) { return(null); } var certItem = new CertItem(); var json = JsonSerializer.Deserialize <IDictionary>(responseBody); certItem.Sign = json["sign"]?.ToString(); certItem.CertSN = json["alipay_cert_sn"]?.ToString(); var signSourceData = GetSignSourceData(request, responseBody); certItem.SignSourceDate = signSourceData; return(certItem); }
private static string GetSignSourceData(IAlipayRequest <T> request, string body) { var rootNode = AlipayUtility.GetRootElement(request.GetApiName()); var errorRootNode = AlipayConstants.ERROR_RESPONSE; var indexOfRootNode = body.IndexOf(rootNode); var indexOfErrorRoot = body.IndexOf(errorRootNode); string result = null; if (indexOfRootNode > 0) { result = ParseSignSourceData(body, rootNode, indexOfRootNode); } else if (indexOfErrorRoot > 0) { result = ParseSignSourceData(body, errorRootNode, indexOfErrorRoot); } return(result); }
private static string GetSignSourceData(IAlipayRequest <T> request, string body) { var rootNode = request.GetApiName().Replace('.', '_') + AlipayConstants.RESPONSE_SUFFIX; var errorRootNode = AlipayConstants.ERROR_RESPONSE; var indexOfRootNode = body.IndexOf(rootNode, StringComparison.Ordinal); var indexOfErrorRoot = body.IndexOf(errorRootNode, StringComparison.Ordinal); string result = null; if (indexOfRootNode > 0) { result = ParseSignSourceData(body, rootNode, indexOfRootNode); } else if (indexOfErrorRoot > 0) { result = ParseSignSourceData(body, errorRootNode, indexOfErrorRoot); } return(result); }
private static EncryptParseItem ParseEncryptData(IAlipayRequest <T> request, string body) { var rootNode = request.GetApiName().Replace(".", "_") + AlipayConstants.RESPONSE_SUFFIX; var errorRootNode = AlipayConstants.ERROR_RESPONSE; var indexOfRootNode = body.IndexOf(rootNode); var indexOfErrorRoot = body.IndexOf(errorRootNode); EncryptParseItem result = null; if (indexOfRootNode > 0) { result = ParseEncryptItem(body, rootNode, indexOfRootNode); } else if (indexOfErrorRoot > 0) { result = ParseEncryptItem(body, errorRootNode, indexOfErrorRoot); } return(result); }
private void CheckResponseSign <T>(IAlipayRequest <T> request, string responseBody, bool isError, IAlipayParser <T> parser, RSAParameters parameters, string signType) where T : AlipayResponse { try { logger?.LogDebug($"{DateTime.Now} 支付宝CheckResponseSign"); var signItem = parser.GetSignItem(request, responseBody); if (signItem == null) { throw new Exception("sign check fail: Body is Empty!"); } if (!isError || (isError && !string.IsNullOrEmpty(signItem.Sign))) { var rsaCheckContent = AlipaySignature.RSACheckContent(signItem.SignSourceDate, signItem.Sign, parameters, signType); if (!rsaCheckContent) { if (!string.IsNullOrEmpty(signItem.SignSourceDate) && signItem.SignSourceDate.Contains("\\/")) { var srouceData = signItem.SignSourceDate.Replace("\\/", "/"); var jsonCheck = AlipaySignature.RSACheckContent(srouceData, signItem.Sign, parameters, signType); if (!jsonCheck) { throw new Exception("sign check fail: check Sign and Data Fail JSON also"); } } else { throw new Exception("sign check fail: check Sign and Data Fail!"); } } } } catch (Exception ex) { logger?.LogError($"{DateTime.Now} 支付宝CheckResponseSign报错", ex.Message); } }
private async Task CheckResponseCertSignAsync <T>(IAlipayRequest <T> request, string responseBody, bool isError, IAlipayParser <T> parser, AlipayOptions options) where T : AlipayResponse { if (request is AlipayOpenAppAlipaycertDownloadRequest) { return; } var certItem = parser.GetCertItem(request, responseBody); if (certItem == null) { throw new AlipayException("sign check fail: Body is Empty!"); } if (!isError || isError && !string.IsNullOrEmpty(certItem.Sign)) { var currentAlipayPublicKey = await LoadAlipayPublicKeyAsync(certItem, options); var rsaCheckContent = AlipaySignature.RSACheckContent(certItem.SignSourceDate, certItem.Sign, currentAlipayPublicKey, options.SignType); if (!rsaCheckContent) { if (!string.IsNullOrEmpty(certItem.SignSourceDate) && certItem.SignSourceDate.Contains("\\/")) { var srouceData = certItem.SignSourceDate.Replace("\\/", "/"); var jsonCheck = AlipaySignature.RSACheckContent(srouceData, certItem.Sign, currentAlipayPublicKey, options.SignType); if (!jsonCheck) { throw new AlipayException("sign check fail: check Sign and Data Fail JSON also"); } } else { throw new AlipayException("sign check fail: check Sign and Data Fail!"); } } } }
public T SdkExecute <T>(IAlipayRequest <T> request) where T : AlipayResponse { // 构造请求参数 var requestParams = BuildRequestParams(request, null, null); // 字典排序 var sortedParams = new SortedDictionary <string, string>(requestParams); var sortedAlipayDic = new AlipayDictionary(sortedParams); // 参数签名 var signResult = AlipaySignature.RSASign(sortedAlipayDic, Options.RsaPrivateKey, Options.SignType); // 添加签名结果参数 sortedAlipayDic.Add(SIGN, signResult); // 参数拼接 var signedResult = HttpClientEx.BuildQuery(sortedAlipayDic); // 构造结果 var rsp = (T)Activator.CreateInstance(typeof(T)); rsp.Body = signedResult; return(rsp); }
private AlipayDictionary SerializeBizModel <T>(AlipayDictionary requestParams, IAlipayRequest <T> request) where T : AlipayResponse { var result = requestParams; var isBizContentEmpty = !requestParams.ContainsKey(AlipayConstants.BIZ_CONTENT) || string.IsNullOrEmpty(requestParams[AlipayConstants.BIZ_CONTENT]); var bizModel = request.GetBizModel(); if (isBizContentEmpty && bizModel != null) { var content = JsonSerializer.Serialize(bizModel, bizModel.GetType(), _jsonSerializerOptions); result.Add(AlipayConstants.BIZ_CONTENT, content); } return(result); }
public async Task <T> PageExecuteAsync <T>(IAlipayRequest <T> request, AlipayOptions options) where T : AlipayResponse { return(await PageExecuteAsync(request, options, null, "POST")); }
public async Task <T> CertificateExecuteAsync <T>(IAlipayRequest <T> request, AlipayOptions options, string accessToken, string appAuthToken) where T : AlipayResponse { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(options.AppId)) { throw new ArgumentNullException(nameof(options.AppId)); } if (string.IsNullOrEmpty(options.SignType)) { throw new ArgumentNullException(nameof(options.SignType)); } if (string.IsNullOrEmpty(options.AppPrivateKey)) { throw new ArgumentNullException(nameof(options.AppPrivateKey)); } if (string.IsNullOrEmpty(options.AppCert)) { throw new ArgumentNullException(nameof(options.AppCert)); } if (string.IsNullOrEmpty(options.AlipayPublicCert)) { throw new ArgumentNullException(nameof(options.AlipayPublicCert)); } if (string.IsNullOrEmpty(options.RootCert)) { throw new ArgumentNullException(nameof(options.RootCert)); } if (string.IsNullOrEmpty(options.ServerUrl)) { throw new ArgumentNullException(nameof(options.ServerUrl)); } var apiVersion = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion(); // 添加协议级请求参数 var txtParams = new AlipayDictionary(request.GetParameters()) { { AlipayConstants.METHOD, request.GetApiName() }, { AlipayConstants.VERSION, apiVersion }, { AlipayConstants.APP_ID, options.AppId }, { AlipayConstants.FORMAT, options.Format }, { AlipayConstants.TIMESTAMP, DateTime.Now }, { AlipayConstants.ACCESS_TOKEN, accessToken }, { AlipayConstants.SIGN_TYPE, options.SignType }, { AlipayConstants.TERMINAL_TYPE, request.GetTerminalType() }, { AlipayConstants.TERMINAL_INFO, request.GetTerminalInfo() }, { AlipayConstants.PROD_CODE, request.GetProdCode() }, { AlipayConstants.CHARSET, options.Charset }, { AlipayConstants.APP_CERT_SN, options.AppCertSN }, { AlipayConstants.ALIPAY_ROOT_CERT_SN, options.RootCertSN } }; // 序列化BizModel txtParams = SerializeBizModel(txtParams, request); if (!string.IsNullOrEmpty(request.GetNotifyUrl())) { txtParams.Add(AlipayConstants.NOTIFY_URL, request.GetNotifyUrl()); } if (!string.IsNullOrEmpty(appAuthToken)) { txtParams.Add(AlipayConstants.APP_AUTH_TOKEN, appAuthToken); } if (request.GetNeedEncrypt()) { if (string.IsNullOrEmpty(txtParams[AlipayConstants.BIZ_CONTENT])) { throw new AlipayException("api request Fail ! The reason: encrypt request is not supported!"); } if (string.IsNullOrEmpty(options.EncyptKey) || string.IsNullOrEmpty(options.EncyptType)) { throw new AlipayException("encryptType or encryptKey must not null!"); } if (!"AES".Equals(options.EncyptType)) { throw new AlipayException("api only support Aes!"); } var encryptContent = AlipaySignature.AESEncrypt(txtParams[AlipayConstants.BIZ_CONTENT], options.EncyptKey); txtParams.Remove(AlipayConstants.BIZ_CONTENT); txtParams.Add(AlipayConstants.BIZ_CONTENT, encryptContent); txtParams.Add(AlipayConstants.ENCRYPT_TYPE, options.EncyptType); } // 添加签名参数 var signContent = AlipaySignature.GetSignContent(txtParams); txtParams.Add(AlipayConstants.SIGN, AlipaySignature.RSASignContent(signContent, options.AppPrivateKey, options.SignType)); string body; var client = _httpClientFactory.CreateClient(nameof(AlipayClient)); // 是否需要上传文件 if (request is IAlipayUploadRequest <T> uRequest) { var fileParams = AlipayUtility.CleanupDictionary(uRequest.GetFileParameters()); body = await client.PostAsync(options.ServerUrl, txtParams, fileParams); } else { body = await client.PostAsync(options.ServerUrl, txtParams); } var parser = new AlipayJsonParser <T>(); var item = ParseRespItem(request, body, parser, options.EncyptKey, options.EncyptType); var rsp = parser.Parse(item.RealContent); await CheckResponseCertSignAsync(request, item.RespContent, rsp.IsError, parser, options); return(rsp); }
public async Task <T> PageExecuteAsync <T>(IAlipayRequest <T> request, AlipayOptions options, string accessToken, string reqMethod) where T : AlipayResponse { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(options.AppId)) { throw new ArgumentNullException(nameof(options.AppId)); } if (string.IsNullOrEmpty(options.SignType)) { throw new ArgumentNullException(nameof(options.SignType)); } if (string.IsNullOrEmpty(options.AppPrivateKey)) { throw new ArgumentNullException(nameof(options.AppPrivateKey)); } if (string.IsNullOrEmpty(options.ServerUrl)) { throw new ArgumentNullException(nameof(options.ServerUrl)); } var apiVersion = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion(); // 添加协议级请求参数 var txtParams = new AlipayDictionary(request.GetParameters()) { { AlipayConstants.METHOD, request.GetApiName() }, { AlipayConstants.VERSION, apiVersion }, { AlipayConstants.APP_ID, options.AppId }, { AlipayConstants.FORMAT, options.Format }, { AlipayConstants.TIMESTAMP, DateTime.Now }, { AlipayConstants.ACCESS_TOKEN, accessToken }, { AlipayConstants.SIGN_TYPE, options.SignType }, { AlipayConstants.TERMINAL_TYPE, request.GetTerminalType() }, { AlipayConstants.TERMINAL_INFO, request.GetTerminalInfo() }, { AlipayConstants.PROD_CODE, request.GetProdCode() }, { AlipayConstants.NOTIFY_URL, request.GetNotifyUrl() }, { AlipayConstants.CHARSET, options.Charset }, { AlipayConstants.RETURN_URL, request.GetReturnUrl() } }; // 序列化BizModel txtParams = SerializeBizModel(txtParams, request); // 添加签名参数 var signContent = AlipaySignature.GetSignContent(txtParams); txtParams.Add(AlipayConstants.SIGN, AlipaySignature.RSASignContent(signContent, options.AppPrivateKey, options.SignType)); string body; // 是否需要上传文件 if (request is IAlipayUploadRequest <T> uploadRequest) { var fileParams = AlipayUtility.CleanupDictionary(uploadRequest.GetFileParameters()); var client = _httpClientFactory.CreateClient(nameof(AlipayClient)); body = await client.PostAsync(options.ServerUrl, txtParams, fileParams); } else { if (reqMethod.ToUpperInvariant() == "GET") { var url = options.ServerUrl; if (txtParams != null && txtParams.Count > 0) { if (url.Contains("?")) { url += "&" + AlipayUtility.BuildQuery(txtParams); } else { url += "?" + AlipayUtility.BuildQuery(txtParams); } } body = url; } else { body = BuildHtmlRequest(txtParams, options.ServerUrl, options.Charset, reqMethod); } } var parser = new AlipayJsonParser <T>(); var rsp = parser.Parse(body); return(rsp); }