public Task <T> ExecuteAsync <T>(HttpRequest request) where T : UnionPayNotifyResponse { var parameters = new UnionPayDictionary(); if (request.Method == "POST") { foreach (var item in request.Form) { parameters.Add(item.Key, item.Value); } } if (parameters == null || parameters.Count == 0) { throw new Exception("sign check fail: sign is Empty!"); } if (!UnionPaySignature.Validate(parameters, Options.SecureKey)) { throw new Exception("sign check fail: check Sign and Data Fail!"); } var parser = new UnionPayDictionaryParser <T>(); var rsp = parser.Parse(parameters); return(Task.FromResult(rsp)); }
public UnionPayClient(IOptions <UnionPayOptions> optionsAccessor) { Options = optionsAccessor?.Value ?? new UnionPayOptions(); Client = new HttpClientEx(); if (string.IsNullOrEmpty(Options.SignCert) || string.IsNullOrEmpty(Options.SignCertPassword)) { throw new Exception("SignCert or SignCertPassword is Empty!"); } SignCertificate = UnionPaySignature.GetSignCertificate(Options.SignCert, Options.SignCertPassword); if (string.IsNullOrEmpty(Options.EncryptCert)) { throw new Exception("EncryptCert is Empty!"); } EncryptCertificate = UnionPaySignature.GetCertificate(Options.EncryptCert); if (string.IsNullOrEmpty(Options.MiddleCert)) { throw new Exception("MiddleCert is Empty!"); } MiddleCertificate = UnionPaySignature.GetCertificate(Options.MiddleCert); if (string.IsNullOrEmpty(Options.RootCert)) { throw new Exception("RootCert is Empty!"); } RootCertificate = UnionPaySignature.GetCertificate(Options.RootCert); }
private void Init() { if (string.IsNullOrEmpty(Options.SignCert) || string.IsNullOrEmpty(Options.SignCertPassword)) { throw new Exception("SignCert or SignCertPassword is Empty!"); } SignCertificate = UnionPaySignature.GetSignCertificate(Options.SignCert, Options.SignCertPassword); if (string.IsNullOrEmpty(Options.EncryptCert)) { throw new Exception("EncryptCert is Empty!"); } EncryptCertificate = UnionPaySignature.GetCertificate(Options.EncryptCert); if (string.IsNullOrEmpty(Options.MiddleCert)) { throw new Exception("MiddleCert is Empty!"); } MiddleCertificate = UnionPaySignature.GetCertificate(Options.MiddleCert); if (string.IsNullOrEmpty(Options.RootCert)) { throw new Exception("RootCert is Empty!"); } RootCertificate = UnionPaySignature.GetCertificate(Options.RootCert); }
public Task <T> PageExecuteAsync <T>(IUnionPayRequest <T> request, string reqMethod) where T : UnionPayResponse { var version = string.Empty; if (!string.IsNullOrEmpty(request.GetApiVersion())) { version = request.GetApiVersion(); } else { version = Options.Version; } var txtParams = new UnionPayDictionary(request.GetParameters()) { { VERSION, version }, { ENCODING, Options.Encoding }, { SIGNMETHOD, Options.SignMethod }, { TXNTYPE, request.GetTxnType() }, { TXNSUBTYPE, request.GetTxnSubType() }, { BIZTYPE, request.GetBizType() }, { CHANNELTYPE, request.GetChannelType() }, { ACCESSTYPE, Options.AccessType }, { MERID, Options.MerId }, }; UnionPaySignature.Sign(txtParams, SignCertificate.certId, SignCertificate.key, Options.SecureKey); var rsp = Activator.CreateInstance <T>(); var url = request.GetRequestUrl(Options.TestMode); if (reqMethod == "GET") { //拼接get请求的url var tmpUrl = url; if (txtParams != null && txtParams.Count > 0) { if (tmpUrl.Contains("?")) { tmpUrl = tmpUrl + "&" + HttpClientEx.BuildQuery(txtParams); } else { tmpUrl = tmpUrl + "?" + HttpClientEx.BuildQuery(txtParams); } } rsp.Body = tmpUrl; } else { //输出post表单 rsp.Body = BuildHtmlRequest(url, txtParams, reqMethod); } return(Task.FromResult(rsp)); }
public async Task <T> ExecuteAsync <T>(IUnionPayRequest <T> request, UnionPayOptions options) where T : UnionPayResponse { var version = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion(); var merId = options.MerId; if (options.TestMode && (request is UnionPayGatewayPayFileTransferRequest || request is UnionPayNoRedirectPayFileTransferRequest || request is UnionPayQrCodePayFileTransferRequest || request is UnionPayWapPayFileTransferRequest)) { merId = "700000000000001"; } var txtParams = new UnionPayDictionary(request.GetParameters()) { { VERSION, version }, { ENCODING, options.Encoding }, { SIGNMETHOD, options.SignMethod }, { ACCESSTYPE, options.AccessType }, { MERID, merId } }; if (request.HasEncryptCertId() && options.EncryptCertificate?.key != null) { if (txtParams.TryGetValue(ACCNO, out var accNo)) { if (!string.IsNullOrEmpty(accNo)) { // 对敏感信息加密 txtParams[ACCNO] = UnionPaySignature.EncryptData(accNo, options.EncryptCertificate.key); txtParams.Add(ENCRYPTCERTID, options.EncryptCertificate.certId); } } } UnionPaySignature.Sign(txtParams, options.SignCertificate.certId, options.SignCertificate.key, options.SecureKey); var query = UnionPayUtility.BuildQuery(txtParams); using (var client = _httpClientFactory.CreateClient(nameof(UnionPayClient))) { var body = await client.DoPostAsync(request.GetRequestUrl(options.TestMode), query); var dictionary = ParseQueryString(body); var ifValidateCNName = !options.TestMode; if (!UnionPaySignature.Validate(dictionary, options.RootCertificate.cert, options.MiddleCertificate.cert, options.SecureKey, ifValidateCNName)) { throw new UnionPayException("sign check fail: check Sign and Data Fail!"); } var parser = new UnionPayDictionaryParser <T>(); var rsp = parser.Parse(dictionary); rsp.Body = body; return(rsp); } }
private void CheckNotifySign(UnionPayDictionary dictionary, UnionPayOptions options) { if (dictionary == null || dictionary.Count == 0) { throw new UnionPayException("sign check fail: sign is Empty!"); } var ifValidateCNName = !options.TestMode; if (!UnionPaySignature.Validate(dictionary, options.RootCertificate.cert, options.MiddleCertificate.cert, options.SecureKey, ifValidateCNName)) { throw new UnionPayException("sign check fail: check Sign and Data Fail!"); } }
public Task <T> PageExecuteAsync <T>(IUnionPayRequest <T> request, string optionsName) where T : UnionPayResponse { var options = string.IsNullOrEmpty(optionsName) ? _optionsSnapshotAccessor.Value : _optionsSnapshotAccessor.Get(optionsName); var version = string.IsNullOrEmpty(request.GetApiVersion()) ? options.Version : request.GetApiVersion(); var merId = options.MerId; if (options.TestMode && (request is UnionPayGatewayPayFileTransferRequest || request is UnionPayNoRedirectPayFileTransferRequest || request is UnionPayQrCodePayFileTransferRequest || request is UnionPayWapPayFileTransferRequest)) { merId = "700000000000001"; } var txtParams = new UnionPayDictionary(request.GetParameters()) { { VERSION, version }, { ENCODING, options.Encoding }, { SIGNMETHOD, options.SignMethod }, { ACCESSTYPE, options.AccessType }, { MERID, merId } }; if (request.HasEncryptCertId() && options.EncryptCertificate?.key != null) { if (txtParams.TryGetValue(ACCNO, out var accNo)) { if (!string.IsNullOrEmpty(accNo)) { // 对敏感信息加密 txtParams[ACCNO] = UnionPaySignature.EncryptData(accNo, options.EncryptCertificate.key); txtParams.Add(ENCRYPTCERTID, options.EncryptCertificate.certId); } } } UnionPaySignature.Sign(txtParams, options.SignCertificate.certId, options.SignCertificate.key, options.SecureKey); var rsp = Activator.CreateInstance <T>(); var url = request.GetRequestUrl(options.TestMode); //输出post表单 rsp.Body = BuildHtmlRequest(url, txtParams); return(Task.FromResult(rsp)); }
public UnionPayNotifyClient( IOptions <UnionPayOptions> optionsAccessor, ILogger <UnionPayNotifyClient> logger) { Options = optionsAccessor?.Value ?? new UnionPayOptions(); Logger = logger; if (string.IsNullOrEmpty(Options.MiddleCert)) { throw new ArgumentNullException(nameof(Options.MiddleCert)); } if (string.IsNullOrEmpty(Options.RootCert)) { throw new ArgumentNullException(nameof(Options.RootCert)); } MiddleCertificate = UnionPaySignature.GetCertificate(Options.MiddleCert); RootCertificate = UnionPaySignature.GetCertificate(Options.RootCert); }
public UnionPayClient( IOptions <UnionPayOptions> optionsAccessor, ILogger <UnionPayClient> logger) { Options = optionsAccessor.Value; Logger = logger; Client = new HttpClientEx(); if (string.IsNullOrEmpty(Options.SignCert)) { throw new ArgumentNullException(nameof(Options.SignCert)); } if (string.IsNullOrEmpty(Options.SignCertPassword)) { throw new ArgumentNullException(nameof(Options.SignCertPassword)); } if (string.IsNullOrEmpty(Options.EncryptCert)) { throw new ArgumentNullException(nameof(Options.EncryptCert)); } if (string.IsNullOrEmpty(Options.MiddleCert)) { throw new ArgumentNullException(nameof(Options.MiddleCert)); } if (string.IsNullOrEmpty(Options.RootCert)) { throw new ArgumentNullException(nameof(Options.RootCert)); } SignCertificate = UnionPaySignature.GetSignCertificate(Options.SignCert, Options.SignCertPassword); EncryptCertificate = UnionPaySignature.GetCertificate(Options.EncryptCert); MiddleCertificate = UnionPaySignature.GetCertificate(Options.MiddleCert); RootCertificate = UnionPaySignature.GetCertificate(Options.RootCert); }
public async Task <T> ExecuteAsync <T>(IUnionPayRequest <T> request) where T : UnionPayResponse { var version = string.Empty; if (!string.IsNullOrEmpty(request.GetApiVersion())) { version = request.GetApiVersion(); } else { version = Options.Version; } var merId = Options.MerId; if (Options.TestMode && (request is UnionPayForm05_7_FileTransferRequest || request is UnionPayForm_6_6_FileTransferRequest)) { merId = "700000000000001"; } var txtParams = new UnionPayDictionary(request.GetParameters()) { { VERSION, version }, { ENCODING, Options.Encoding }, { SIGNMETHOD, Options.SignMethod }, { ACCESSTYPE, Options.AccessType }, { MERID, merId }, }; if (request.HasEncryptCertId()) { txtParams.Add(ENCRYPTCERTID, EncryptCertificate.certId); } UnionPaySignature.Sign(txtParams, SignCertificate.certId, SignCertificate.key, Options.SecureKey); var query = HttpClientEx.BuildQuery(txtParams); Logger?.LogTrace(0, "Request:{query}", query); var body = await Client.DoPostAsync(request.GetRequestUrl(Options.TestMode), query); Logger?.LogTrace(1, "Response:{content}", body.Content); var dic = ParseQueryString(body.Content); if (string.IsNullOrEmpty(body.Content)) { throw new Exception("sign check fail: Body is Empty!"); } var ifValidateCNName = !Options.TestMode; if (!UnionPaySignature.Validate(dic, RootCertificate.cert, MiddleCertificate.cert, Options.SecureKey, ifValidateCNName)) { throw new Exception("sign check fail: check Sign and Data Fail!"); } var parser = new UnionPayDictionaryParser <T>(); var rsp = parser.Parse(dic); rsp.Body = body.Content; return(rsp); }
public async Task <T> ExecuteAsync <T>(IUnionPayRequest <T> request) where T : UnionPayResponse { var version = string.Empty; if (!string.IsNullOrEmpty(request.GetApiVersion())) { version = request.GetApiVersion(); } else { version = Options.Version; } var merId = Options.MerId; if (Options.TestMode && (request is UnionPayForm05_7_FileTransferRequest || request is UnionPayForm_6_6_FileTransferRequest)) { merId = "700000000000001"; } var txtParams = new UnionPayDictionary(request.GetParameters()) { { VERSION, version }, { ENCODING, Options.Encoding }, { SIGNMETHOD, Options.SignMethod }, { TXNTYPE, request.GetTxnType() }, { TXNSUBTYPE, request.GetTxnSubType() }, { BIZTYPE, request.GetBizType() }, { CHANNELTYPE, request.GetChannelType() }, { ACCESSTYPE, Options.AccessType }, { MERID, merId }, }; UnionPaySignature.Sign(txtParams, SignCertificate.certId, SignCertificate.key, Options.SecureKey); var resContent = await Client.DoPostAsync(request.GetRequestUrl(Options.TestMode), txtParams); if (string.IsNullOrEmpty(resContent.Content)) { throw new Exception("resContent is Empty!"); } if (resContent.ContentType.Contains("text/plain")) { throw new Exception(resContent.Content); } var dic = ParseQueryString(resContent.Content); if (dic == null || dic.Count == 0) { throw new Exception("sign check fail: Body is Empty!"); } if (!UnionPaySignature.Validate(dic, Options.SecureKey)) { throw new Exception("sign check fail: check Sign and Data Fail!"); } var parser = new UnionPayDictionaryParser <T>(); var rsp = parser.Parse(dic); rsp.Body = resContent.Content; return(rsp); }