private static void CyberSourceReport(string strUsername, string password, DateTime reportDate, string merchantid) { string strReportURL=""; string strReportName=""; string strReportFormat=""; string reportURL=""; string authInfo=""; string strReportDownloadLocation=""; string filepath=""; try { strReportName = ConfigurationSettings.AppSettings["reportName"].ToString(); strReportFormat = ConfigurationSettings.AppSettings["reportFormat"].ToString(); reportURL = ConfigurationSettings.AppSettings["reportURL"].ToString(); strReportURL = reportURL + getYear(reportDate) + "/" + getMonth(reportDate) + "/" + getDay(reportDate) + "/" + strUsername + "/" + strReportName + "." + strReportFormat; strReportDownloadLocation = ConfigurationSettings.AppSettings["ReportDownloadLocation"].ToString(); if (string.IsNullOrEmpty(strReportName)) throw new NullReferenceException("ReportName should not be null"); if (string.IsNullOrEmpty(password)) throw new NullReferenceException("Password should not be null"); if (string.IsNullOrEmpty(strReportFormat)) throw new NullReferenceException("Report Format should not be null"); if (string.IsNullOrEmpty(reportURL)) throw new NullReferenceException("Report URL should not be null"); } catch(Exception ex) { logger.LogException(LogLevel.Error, "Validation Error occured " + ex.Message.ToString(), ex); } HttpWebResponse webresponse=null; HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(strReportURL); Stream str; authInfo = strUsername + ":" + password; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); webrequest.Headers["Authorization"] = "Basic " + authInfo; webrequest.PreAuthenticate = false; webrequest.KeepAlive = false; webrequest.Method = "GET"; webrequest.ContentType = "application/csv"; try { webresponse = (HttpWebResponse)webrequest.GetResponse(); if (webresponse.ContentLength < 1) throw new NullReferenceException("Web response is null"); else str = webresponse.GetResponseStream(); filepath = strReportDownloadLocation + strReportName + "_" + strUsername + "_" + reportDate.ToString("MM/dd/yyyy").Replace("/", "-") + "." + strReportFormat; byte[] inBuf = new byte[10000000]; int bytesToRead = (int)inBuf.Length; int bytesRead = 0; while (bytesToRead > 0) { int n = str.Read(inBuf, bytesRead, bytesToRead); if (n == 0) break; bytesRead += n; bytesToRead -= n; } FileStream fstr = new FileStream(filepath, FileMode.Create, FileAccess.Write); fstr.Write(inBuf, 0, bytesRead); str.Close(); fstr.Close(); tdc.ExecuteCommand("delete from dbo.T_CybersourceGatewayTranDownloadTracking where MerchantId= '" + merchantid + "'"); CybersourceGatewayTranDownloadTracking trandwld = new CybersourceGatewayTranDownloadTracking() { FileName = filepath, isDownLoadSuccess = true, MerchantId = merchantid, Password = password, UserName = strUsername, ReportDate = reportDate, Message = "success" }; tdc.CybersourceGatewayTranDownloadTrackings.InsertOnSubmit(trandwld); tdc.SubmitChanges(); tdc.ExecuteCommand("update dbo.T_CybersourceGatewayMerchantConfig set Password='******' where MerchantId = '" + merchantid + "'"); } catch (Exception ex) { tdc.ExecuteCommand("delete from dbo.T_CybersourceGatewayTranDownloadTracking where MerchantId= '" + merchantid + "'"); if (!((System.Net.HttpWebResponse)(((System.Net.WebException)(ex)).Response)).StatusDescription.ToString().Contains("The report requested cannot be found on this server")) { CybersourceGatewayTranDownloadTracking trandwld = new CybersourceGatewayTranDownloadTracking() { FileName = filepath, isDownLoadSuccess = false, MerchantId = merchantid, Password = password, UserName = strUsername, ReportDate = reportDate, Message = ((System.Net.HttpWebResponse)(((System.Net.WebException)(ex)).Response)).StatusDescription.ToString() }; tdc.CybersourceGatewayTranDownloadTrackings.InsertOnSubmit(trandwld); tdc.SubmitChanges(); //logger.LogException(LogLevel.Error, "Error occured while downloading report " + ex.Message.ToString(), ex); } } }
partial void DeleteCybersourceGatewayTranDownloadTracking(CybersourceGatewayTranDownloadTracking instance);
partial void InsertCybersourceGatewayTranDownloadTracking(CybersourceGatewayTranDownloadTracking instance);
private static void CyberSourceReport(string strUsername, string password, DateTime reportDate, string merchantid) { string strReportURL = ""; string strReportName = ""; string strReportFormat = ""; string reportURL = ""; string authInfo = ""; string strReportDownloadLocation = ""; string filepath = ""; try { strReportName = ConfigurationSettings.AppSettings["reportName"].ToString(); strReportFormat = ConfigurationSettings.AppSettings["reportFormat"].ToString(); reportURL = ConfigurationSettings.AppSettings["reportURL"].ToString(); strReportURL = reportURL + getYear(reportDate) + "/" + getMonth(reportDate) + "/" + getDay(reportDate) + "/" + strUsername + "/" + strReportName + "." + strReportFormat; strReportDownloadLocation = ConfigurationSettings.AppSettings["ReportDownloadLocation"].ToString(); if (string.IsNullOrEmpty(strReportName)) { throw new NullReferenceException("ReportName should not be null"); } if (string.IsNullOrEmpty(password)) { throw new NullReferenceException("Password should not be null"); } if (string.IsNullOrEmpty(strReportFormat)) { throw new NullReferenceException("Report Format should not be null"); } if (string.IsNullOrEmpty(reportURL)) { throw new NullReferenceException("Report URL should not be null"); } } catch (Exception ex) { logger.LogException(LogLevel.Error, "Validation Error occured " + ex.Message.ToString(), ex); } HttpWebResponse webresponse = null; HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(strReportURL); Stream str; authInfo = strUsername + ":" + password; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); webrequest.Headers["Authorization"] = "Basic " + authInfo; webrequest.PreAuthenticate = false; webrequest.KeepAlive = false; webrequest.Method = "GET"; webrequest.ContentType = "application/csv"; try { webresponse = (HttpWebResponse)webrequest.GetResponse(); if (webresponse.ContentLength < 1) { throw new NullReferenceException("Web response is null"); } else { str = webresponse.GetResponseStream(); } filepath = strReportDownloadLocation + strReportName + "_" + strUsername + "_" + reportDate.ToString("MM/dd/yyyy").Replace("/", "-") + "." + strReportFormat; byte[] inBuf = new byte[10000000]; int bytesToRead = (int)inBuf.Length; int bytesRead = 0; while (bytesToRead > 0) { int n = str.Read(inBuf, bytesRead, bytesToRead); if (n == 0) { break; } bytesRead += n; bytesToRead -= n; } FileStream fstr = new FileStream(filepath, FileMode.Create, FileAccess.Write); fstr.Write(inBuf, 0, bytesRead); str.Close(); fstr.Close(); tdc.ExecuteCommand("delete from dbo.T_CybersourceGatewayTranDownloadTracking where MerchantId= '" + merchantid + "'"); CybersourceGatewayTranDownloadTracking trandwld = new CybersourceGatewayTranDownloadTracking() { FileName = filepath, isDownLoadSuccess = true, MerchantId = merchantid, Password = password, UserName = strUsername, ReportDate = reportDate, Message = "success" }; tdc.CybersourceGatewayTranDownloadTrackings.InsertOnSubmit(trandwld); tdc.SubmitChanges(); tdc.ExecuteCommand("update dbo.T_CybersourceGatewayMerchantConfig set Password='******' where MerchantId = '" + merchantid + "'"); } catch (Exception ex) { tdc.ExecuteCommand("delete from dbo.T_CybersourceGatewayTranDownloadTracking where MerchantId= '" + merchantid + "'"); if (!((System.Net.HttpWebResponse)(((System.Net.WebException)(ex)).Response)).StatusDescription.ToString().Contains("The report requested cannot be found on this server")) { CybersourceGatewayTranDownloadTracking trandwld = new CybersourceGatewayTranDownloadTracking() { FileName = filepath, isDownLoadSuccess = false, MerchantId = merchantid, Password = password, UserName = strUsername, ReportDate = reportDate, Message = ((System.Net.HttpWebResponse)(((System.Net.WebException)(ex)).Response)).StatusDescription.ToString() }; tdc.CybersourceGatewayTranDownloadTrackings.InsertOnSubmit(trandwld); tdc.SubmitChanges(); //logger.LogException(LogLevel.Error, "Error occured while downloading report " + ex.Message.ToString(), ex); } } }