public static Repository CreateRepository() { // Load the private certificate from disk using the password used to create it X509Certificate2 privateCertificate = new X509Certificate2(@"D:\Stevie-Cert.pfx", "xero"); IOAuthSession consumerSession = new XeroApiPrivateSession(UserAgentString, ConsumerKey, privateCertificate); consumerSession.MessageLogger = new DebugMessageLogger(); // Wrap the authenticated consumerSession in the repository... return new Repository(consumerSession); }
/// <summary> /// Logs in to Xero and initalises all objects /// </summary> /// <returns></returns> public bool LoginToXero() { try { switch (CompanyName) { case InterResolveXeroOrganisation.InterResolve_Ltd: this.XeroSession = new XeroApiPrivateSession( "InterResolve Xero", "QHUM8OQNEMKZL8VLKV2WEDWXEJ1S67", new System.Security.Cryptography.X509Certificates.X509Certificate2(HttpContext.Current.Server.MapPath("~/Certificates/InterResolve Ltd/public_privatekey.pfx"), "GoldenTriangle") ); break; case InterResolveXeroOrganisation.InterMediation_Group_Ltd: this.XeroSession = new XeroApiPrivateSession( "InterResolve Xero Intermediation Ltd", "EHH7HOGJ57U8BAYSOGRUDZDY8ECO9F", new System.Security.Cryptography.X509Certificates.X509Certificate2(HttpContext.Current.Server.MapPath("~/Certificates/InterMediation/public_privatekey.pfx"), "GoldenTriangle") ); break; case InterResolveXeroOrganisation.InterResolve_Claims_Ltd: this.XeroSession = new XeroApiPrivateSession( "InterResolve Xero InterResolve Claims Ltd", "MAZRRCUFNMYKAX6PETEJYFRH6EIXIW", new System.Security.Cryptography.X509Certificates.X509Certificate2(HttpContext.Current.Server.MapPath("~/Certificates/InterResolve Claims Ltd/public_privatekey.pfx"), "GoldenTriangle") ); break; case InterResolveXeroOrganisation.InterResolve_Holdings_Ltd: this.XeroSession = new XeroApiPrivateSession( "InterResolve Xero InterResolve Holdings Ltd", "GISB8EGZYHIZGG2NAPNCWAMI7NZJ25", new System.Security.Cryptography.X509Certificates.X509Certificate2(HttpContext.Current.Server.MapPath("~/Certificates/InterResolve Holdings Ltd/public_privatekey.pfx"), "GoldenTriangle") ); break; case InterResolveXeroOrganisation.MotorResolve_Ltd: this.XeroSession = new XeroApiPrivateSession( "InterResolve Xero MotorResolve Ltd", "AADULHGNZL4LB0TXT7ZXJRSW9BRTNZ", new System.Security.Cryptography.X509Certificates.X509Certificate2(HttpContext.Current.Server.MapPath("~/Certificates/MotorResolve Ltd/public_privatekey.pfx"), "GoldenTriangle") ); break; } this.XeroRepository = new XeroApi.Repository(this.XeroSession); this.LoginSuccessful = true; this.HostName = System.Environment.MachineName; this.LoginTimeStamp = DateTime.Now; return true; } catch(Exception ex) { throw ex; } }
public ActionResult Create([Bind(Include = "Contact,Date,Type,DueDate,Status")] Invoice invoice) { if (invoice.Date < DateTime.Now.AddDays(-1)) { ModelState.AddModelError("Date", "Date must be today or a future time"); } if (invoice.DueDate < invoice.Date) { ModelState.AddModelError("DueDate", "DueDate must be greater than the Start Date"); } //in real app we also need to check the existence of the contact if (ModelState.IsValid) { var XeroSession = new XeroApi.OAuth.XeroApiPrivateSession("MyApiTestSoftware", "5INHEEY7VVKTPZPBMTF9BRCDN33KGM", new X509Certificate2("C:\\OpenSSL-Win32\\bin\\public_privatekey.pfx")); var repository = new XeroApi.Repository(XeroSession); var contacts = repository.Contacts.Where(c => c.Name == "Marine Systems"); //new comment var uInvoice = new XeroApi.Model.Invoice(); var uContact = new XeroApi.Model.Contact(); DateTime today = DateTime.Now; DateTime duedate = today.AddDays(30); uContact.Name = "Marine Systems"; uInvoice.Contact = uContact; //uInvoice.Date = DateTime.Now; uInvoice.Date = invoice.Date; uInvoice.Type = "ACCREC"; //uInvoice.DueDate = duedate; uInvoice.DueDate = invoice.DueDate; uInvoice.Status = "AUTHORISED"; uInvoice.FullyPaidOnDate = today; uInvoice.AmountPaid = 230; uInvoice.LineItems = new XeroApi.Model.LineItems(); var uLineItem1 = new XeroApi.Model.LineItem(); uLineItem1.Quantity = 1; uLineItem1.Description = "Product 1"; uLineItem1.AccountCode = "200"; uLineItem1.UnitAmount = 50; uInvoice.LineItems.Add(uLineItem1); var uLineItem2 = new XeroApi.Model.LineItem(); uLineItem2.Quantity = 3; uLineItem2.Description = "Product 2"; uLineItem2.AccountCode = "200"; uLineItem2.UnitAmount = 50; uInvoice.LineItems.Add(uLineItem2); var sResults = repository.Create((XeroApi.Model.Invoice)uInvoice); var theAccount = repository.Accounts.FirstOrDefault(); //the following section is to create a payment for the invoice var invoiceNumber = sResults.InvoiceNumber; var invoiceID = sResults.InvoiceID; var NewInvoice = repository.Invoices.Where(i => i.InvoiceID == invoiceID).FirstOrDefault(); //var account = repository.Accounts.Where(a => a.Name == "MyAccount").FirstOrDefault(); var account = new XeroApi.Model.Account(); //account.ReportingCode = "ABC"; var Payment = new XeroApi.Model.Payment(); //Payment.Account = (XeroApi.Model.Account)account; Payment.Account = theAccount; Payment.Invoice = (XeroApi.Model.Invoice)NewInvoice; Payment.Invoice.InvoiceNumber = invoiceNumber.ToString(); Payment.Invoice.InvoiceID = invoiceID; Payment.Status = "AUTHORISED"; Payment.Date = DateTime.Now; Payment.Amount = 200; var paymentResults = repository.Create((XeroApi.Model.Payment)Payment); db.Invoice.Add(invoice); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(invoice)); }
public static string GetData() { //attempt login to xero now IOAuthSession session = null; try { session = new XeroApi.OAuth.XeroApiPrivateSession( "XeroAPI mini app", "9BMWMQ19COBLNVKIGZVXLDHL2095RB", new X509Certificate2(@"C:\hackfest\public_privatekey.pfx", "hackfest")); } catch (Exception ex) { //TODO notify user return ""; } Repository repository = new Repository(session); List<Payment> list = repository.Payments.ToList(); List<Contact> contacts = repository.Contacts.ToList(); //associate contact ID with contact Dictionary<Guid, Contact> contactDict = new Dictionary<Guid, Contact>(); foreach (Contact c in contacts) { contactDict.Add(c.ContactID, c); } /* //populate purchase list List<Purchase> purchases = new List<Purchase>(); foreach (Payment payment in list) { Purchase p = new Purchase(payment, contactDict[payment.Invoice.Contact.ContactID]); purchases.Add(p); }*/ // Populates the node dictionary Dictionary<String, Node> nodes = new Dictionary<String, Node>(); // A list of nodes to be displayed foreach (Payment payment in list) { Purchase p = new Purchase(payment, contactDict[payment.Invoice.Contact.ContactID]); if (!nodes.ContainsKey(p.Address)) { nodes.Add(p.Address, new Node(p)); } else { Node node = null; nodes.TryGetValue(p.Address, out node); node.addPurchase(p); } } // PurchaseHolder holder = new PurchaseHolder(purchases); NodeHolder holder = new NodeHolder(nodes); //PurchaseHolder holder = new PurchaseHolder(purchases); //finally send the JSON data string json = JsonConvert.SerializeObject(holder); return json; }