public void GetPayments()
        {
            List<Payment> xeroPaymentsToCommit;
            List<Payment> xeroPaymentsToCreate = new List<Payment>();
            List<Payment> xeroPaymentsToUpdate = new List<Payment>();

            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                var dateOfLastUpdate = (from xm in context.XERO_PAYMENTs
                                        select xm.UpdateDate).Max();

                if (!(dateOfLastUpdate == null))
                {
                    xeroPaymentsToCommit = xeroCoreApi.Payments
                    .ModifiedSince(dateOfLastUpdate.GetValueOrDefault().ToUniversalTime())
                    .Find()
                    .ToList();
                }
                else
                {
                    xeroPaymentsToCommit = xeroCoreApi.Payments
                    .Find()
                    .ToList();
                }

                if ((!(xeroPaymentsToCommit == null)) && (xeroPaymentsToCommit.Count > 0))
                {

                    foreach (var payment in xeroPaymentsToCommit)
                    {
                        var paymentDatabaseRecord = (from xm in context.XERO_PAYMENTs
                                                     where xm.InvoiceNumber == payment.Invoice.Number
                                                     select xm).FirstOrDefault();

                        if (paymentDatabaseRecord == null)
                        {
                            xeroPaymentsToCreate.Add(payment);
                        }
                        else
                        {
                            xeroPaymentsToUpdate.Add(payment);
                        }
                    }
                }
            }

            if (xeroPaymentsToCreate.Count > 0)
            {
                CreatePaymentsInDatabase(xeroPaymentsToCreate);
            }

            if (xeroPaymentsToUpdate.Count > 0)
            {
                UpdatePaymentsInDatabase(xeroPaymentsToUpdate);
            }
        }
        private void CreatePaymentsInDatabase(List<Payment> paymentsToUpdate)
        {
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                List<XERO_PAYMENT> xeroPayments = new List<XERO_PAYMENT>();
                foreach (var payment in paymentsToUpdate)
                {
                    XERO_PAYMENT xeroPayment = new XERO_PAYMENT();
                    xeroPayment.AccountCode = payment.Account.Code;
                    xeroPayment.Amount = payment.Amount;
                    xeroPayment.CurrencyRate = payment.CurrencyRate;
                    xeroPayment.Date = payment.Date;
                    xeroPayment.InvoiceNumber = payment.Invoice.Number;
                    xeroPayment.IsReconciled = payment.IsReconciled;
                    xeroPayment.PaymentStatus = payment.Status.ToString();
                    xeroPayment.PaymentType = payment.Type.ToString();
                    xeroPayment.Reference = payment.Reference;
                    xeroPayment.XeroId = payment.Id.ToString();
                    xeroPayment.UpdateDate = payment.UpdatedDateUtc.GetValueOrDefault();

                    xeroPayments.Add(xeroPayment);
                }

                context.XERO_PAYMENTs.InsertAllOnSubmit(xeroPayments);
                context.SubmitChanges();
            }
        }
        private void UpdatePaymentsInDatabase(List<Payment> paymentsToUpdate)
        {
            using (DataClassesDataContext context = new DataClassesDataContext())
            {
                List<XERO_PAYMENT> xeroPayments = new List<XERO_PAYMENT>();
                foreach (var payment in paymentsToUpdate)
                {
                    var originalPayment = (from xm in context.XERO_PAYMENTs
                                           where xm.XeroId == payment.Id.ToString()
                                           select xm).FirstOrDefault();

                    originalPayment.AccountCode = payment.Account.Code;
                    originalPayment.Amount = payment.Amount;
                    originalPayment.CurrencyRate = payment.CurrencyRate;
                    originalPayment.Date = payment.Date;
                    originalPayment.InvoiceNumber = payment.Invoice.Number;
                    originalPayment.IsReconciled = payment.IsReconciled;
                    originalPayment.PaymentStatus = payment.Status.ToString();
                    originalPayment.PaymentType = payment.Type.ToString();
                    originalPayment.Reference = payment.Reference;
                    originalPayment.XeroId = payment.Id.ToString();
                    originalPayment.UpdateDate = payment.UpdatedDateUtc.GetValueOrDefault();

                    xeroPayments.Add(originalPayment);
                }

                context.SubmitChanges();
            }
        }