/// <summary> /// Initialize the repository for the Xero API /// </summary> /// <param name="orgId">organization id to fetch the required credentials</param> private void Initialize(string costomerKey, int orgId) { string applicationName = string.Empty; string consumerKey = string.Empty; string certificatePath = string.Empty; string certificatePassword = string.Empty; DataTable dt = GetXeroCredentials(costomerKey,orgId); if (dt != null) { if (dt.Rows.Count > 0) { if (dt.Rows[0][0] != null) { applicationName = dt.Rows[0][0].ToString(); } if (dt.Rows[0][1] != null) { consumerKey = dt.Rows[0][1].ToString(); } if (dt.Rows[0][2] != null) { certificatePath = dt.Rows[0][2].ToString(); } if (dt.Rows[0][3] != null) { certificatePassword = dt.Rows[0][3].ToString(); } } } if (string.IsNullOrEmpty(applicationName) || string.IsNullOrEmpty(consumerKey) || string.IsNullOrEmpty(certificatePath) || string.IsNullOrEmpty(certificatePassword)) { return; } IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( applicationName, consumerKey, new X509Certificate2(certificatePath, certificatePassword)); repository = new Repository(session); }
private static IOAuthSession XeroAPI() { /* IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( "DPI CampaignTrack", "KAMS635LAPADU8CK3HH2EC1KMBRLRJ", new X509Certificate2(@"D:\public_privatekey.pfx", "!zfca1999")); */ //IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( // "DPI Photography", // "ZFRY6DJBFEMVYJYBFKJFYDVDJZUXYV", // new X509Certificate2(@"D:\CampaignTrack\Certificate\public_privatekey.pfx","!zfca1999")); IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( "CT-DPI",// Photography", "N2KFABNCYXNHCGNU7GDVRL3AOUYMS2",//"ZFRY6DJBFEMVYJYBFKJFYDVDJZUXYV", new X509Certificate2(@"D:\CampaignTrack\Certificate\public_privatekey.pfx", "zerofootprint"));//"!zfca1999")); // Repository repository = new Repository(session); // var items = repository.Items.ToList(); // XElement xmlitems = new XElement("XeroItems", // items.Select(i => new XElement("Items", new XAttribute("Code", i.Code), new XAttribute("SalesUnitPrice", i.SalesDetails.UnitPrice == null ? decimal.Zero : i.SalesDetails.UnitPrice), new XAttribute("SalesAccountCode", i.SalesDetails.AccountCode == null ? string.Empty : i.SalesDetails.AccountCode), new XAttribute("SalesTaxType", i.SalesDetails.TaxType == null ? string.Empty : i.SalesDetails.TaxType), // i.Description //))); // SyncProducts(xmlitems.ToString()); // var contacts = repository.Contacts.ToList(); // XElement xmlcontacts = new XElement("ContactList", // contacts.Select(i => new XElement("Items", new XAttribute("Name", i.Name)))); // SyncContacts(xmlcontacts.ToString()); // GoogleCalSvc.CheckCalendarMovements(); return session; }
private int ProcessOrder(string subject, string htmlBody, string strippedText, int returnCode, DateTime stringSentDate, int emailId, string fileName) { returnCode = 200; Dictionary<string, string> attributeValues; List<OrderItem> orderItems; bool isCancelled = false; List<MultipleOrder> multipleOrdersList; List<SalesContact> salesContacts; List<AdminContact> adminContacts; bool hasMultipleOrders = false; ParseRecievedOrder(out attributeValues, out isCancelled, strippedText, out orderItems, out hasMultipleOrders, out multipleOrdersList, out salesContacts, out adminContacts); invoice = null; if (!isCancelled) { if (hasMultipleOrders) { //process multiple orders foreach (var ord in multipleOrdersList) { if (!exceptionOccured) { if (attributeValues.ContainsKey("OrderId")) { attributeValues["OrderId"] = ord.OrderId; } else { attributeValues.Add("OrderId", ord.OrderId); } attributeValues.Add("ProductDescription", ord.ProductDescription); attributeValues.Add("RequiredDate", ord.RequiredDate); ProcessOrder(attributeValues, stringSentDate, ord.OrderItems, subject, emailId, fileName, htmlBody, salesContacts, adminContacts); attributeValues.Remove("OrderId"); attributeValues.Remove("ProductDescription"); attributeValues.Remove("RequiredDate"); } } if (!exceptionOccured) { if (createInvoice) { try { IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( "CT-DPI", // Photography", "N2KFABNCYXNHCGNU7GDVRL3AOUYMS2", //"ZFRY6DJBFEMVYJYBFKJFYDVDJZUXYV", new X509Certificate2(@"D:\CampaignTrack\Certificate\public_privatekey.pfx", "zerofootprint")); Repository repository = new Repository(session); var inv = repository.Create<XeroApi.Model.Invoice>(invoice); if (invoice.ValidationStatus == ValidationStatus.ERROR) { foreach (var message in invoice.ValidationErrors) { } } try { if (inv.InvoiceNumber != null) { Guid invoiceId = inv.InvoiceID; string invoiceNo = inv.InvoiceNumber; // System.Threading.Thread.Sleep(5000); string jpegFileName = System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + fileName + ".jpg"; //string AnyAttachmentFilename = fileName; var SalesInvoice = repository.Invoices.FirstOrDefault(it => it.InvoiceID == invoiceId); if (File.Exists(jpegFileName)) { var newAttachment = repository.Attachments.Create(SalesInvoice, new FileInfo(jpegFileName)); } InsertPropertyInvoice(propertyId, invoiceNo); } } catch { } } catch (Exception sqlError) { Email eml = new Email(); string supportEmail = System.Configuration.ConfigurationSettings.AppSettings["supportEmail"]; string supportCCEmail = System.Configuration.ConfigurationSettings.AppSettings["supportCCEmail"]; string strLog = sqlError.StackTrace.ToString() + "~~~~~~~~~~~~Error~~~~~~~~~~~:" + sqlError.Message.ToString(); UpdateFailedOrder(emailId); eml.SendMail("Error while creating invoice at Xero : " + fileName + ".htm", "EmailInbox table Row_Id = " + emailId + Environment.NewLine + "Sentdate : " + stringSentDate + Environment.NewLine + "Error :- " + strLog, supportEmail, supportCCEmail, false); } } } } else { ProcessOrder(attributeValues, stringSentDate, orderItems, subject, emailId, fileName, htmlBody, salesContacts, adminContacts); if (!exceptionOccured) { if (createInvoice) { try { IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( "CT-DPI", "N2KFABNCYXNHCGNU7GDVRL3AOUYMS2", new X509Certificate2(@"D:\CampaignTrack\Certificate\public_privatekey.pfx", "zerofootprint")); Repository repository = new Repository(session); var inv = repository.Create<XeroApi.Model.Invoice>(invoice); if (invoice.ValidationStatus == ValidationStatus.ERROR) { foreach (var message in invoice.ValidationErrors) { } } try { if (inv.InvoiceNumber != null) { Guid invoiceId = inv.InvoiceID; string invoiceNo = inv.InvoiceNumber; // System.Threading.Thread.Sleep(5000); string jpegFileName = System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + fileName + ".jpg"; var SalesInvoice = repository.Invoices.FirstOrDefault(it => it.InvoiceID == invoiceId); if (File.Exists(jpegFileName)) { var newAttachment = repository.Attachments.Create(SalesInvoice, new FileInfo(jpegFileName)); } InsertPropertyInvoice(propertyId, invoiceNo); } } catch (Exception sqlError) { } } catch (Exception sqlError) { Email eml = new Email(); string supportEmail = System.Configuration.ConfigurationSettings.AppSettings["supportEmail"]; string supportCCEmail = System.Configuration.ConfigurationSettings.AppSettings["supportCCEmail"]; string strLog = sqlError.StackTrace.ToString() + "~~~~~~~~~~~~Error~~~~~~~~~~~:" + sqlError.Message.ToString(); UpdateFailedOrder(emailId); eml.SendMail("Error while creating invoice at Xero : " + fileName + ".htm", "EmailInbox table Row_Id = " + emailId + Environment.NewLine + "Sentdate : " + stringSentDate + Environment.NewLine + "Error :- " + strLog, supportEmail, supportCCEmail, false); } } } } } else // process cancelled order { List<OrderItem> ordItems = new List<OrderItem>(); InsertCancelledOrders(attributeValues["ProductDescription"], attributeValues["PropertyAddress"], attributeValues["RequiredDate"], fileName); } MoveRecievedFile(fileName, false, isCancelled); return returnCode; }
private static IOAuthSession XeroAPI() { IOAuthSession session = new XeroApi.OAuth.XeroApiPrivateSession( "DPI CampaignTrack", "KAMS635LAPADU8CK3HH2EC1KMBRLRJ", new X509Certificate2(@"D:\public_privatekey.pfx", "!zfca1999")); Repository repository = new Repository(session); var items = repository.Items.ToList(); XElement xmlitems = new XElement("XeroItems", items.Select(i => new XElement("Items", new XAttribute("Code", i.Code), new XAttribute("SalesUnitPrice", i.SalesDetails.UnitPrice == null ? decimal.Zero : i.SalesDetails.UnitPrice), new XAttribute("SalesAccountCode", i.SalesDetails.AccountCode == null ? string.Empty : i.SalesDetails.AccountCode), new XAttribute("SalesTaxType", i.SalesDetails.TaxType == null ? string.Empty : i.SalesDetails.TaxType), i.Description ))); SyncProducts(xmlitems.ToString()); var contacts = repository.Contacts.ToList(); XElement xmlcontacts = new XElement("ContactList", contacts.Select(i => new XElement("Items", new XAttribute("Name", i.Name)))); SyncContacts(xmlcontacts.ToString()); return session; }