public void Exercise_16_Add_Discount_And_Customer_To_Segment()
        {
            var discountResource = new Mozu.Api.Resources.Commerce.Catalog.Admin.DiscountResource(_apiContext);
            var customerSegmentResouce = new Mozu.Api.Resources.Commerce.Customer.CustomerSegmentResource(_apiContext);
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            var discount = (discountResource.GetDiscountsAsync(filter: "Content.Name eq '10% Off Scarves'").Result).Items[0];

            var customerSegment = (customerSegmentResouce.GetSegmentsAsync(filter:"Name eq 'High Volume Customer'").Result).Items[0];
            var segmentToAdd = new Mozu.Api.Contracts.ProductAdmin.CustomerSegment()
            {
                Id = customerSegment.Id
            };

            if (!(discount.Conditions.CustomerSegments.Exists(x => x.Id == segmentToAdd.Id)))
            {
                discount.Conditions.CustomerSegments.Add(segmentToAdd);
                var updatedDiscount = discountResource.UpdateDiscountAsync(discount, (int)discount.Id).Result;
            }

            var customerAccountIds = new List<int>();

            var customerAccount = (customerAccountResource.GetAccountsAsync(filter:"FirstName eq 'Malcolm'").Result).Items[0];

            customerAccountIds.Add(customerAccount.Id);

            if(!(customerAccount.Segments.Exists(x => x.Id == customerSegment.Id)))
            {
                customerSegmentResouce.AddSegmentAccountsAsync(customerAccountIds, customerSegment.Id).Wait();
            }
        }
        public void Exercise_16_Add_Discount_And_Customer_To_Segment()
        {
            var discountResource        = new Mozu.Api.Resources.Commerce.Catalog.Admin.DiscountResource(_apiContext);
            var customerSegmentResouce  = new Mozu.Api.Resources.Commerce.Customer.CustomerSegmentResource(_apiContext);
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            var discount = (discountResource.GetDiscountsAsync(filter: "Content.Name eq '10% Off Scarves'").Result).Items[0];

            var customerSegment = (customerSegmentResouce.GetSegmentsAsync(filter: "Name eq 'High Volume Customer'").Result).Items[0];
            var segmentToAdd    = new Mozu.Api.Contracts.ProductAdmin.CustomerSegment()
            {
                Id = customerSegment.Id
            };

            if (!(discount.Conditions.CustomerSegments.Exists(x => x.Id == segmentToAdd.Id)))
            {
                discount.Conditions.CustomerSegments.Add(segmentToAdd);
                var updatedDiscount = discountResource.UpdateDiscountAsync(discount, (int)discount.Id).Result;
            }

            var customerAccountIds = new List <int>();

            var customerAccount = (customerAccountResource.GetAccountsAsync(filter: "FirstName eq 'Malcolm'").Result).Items[0];

            customerAccountIds.Add(customerAccount.Id);

            if (!(customerAccount.Segments.Exists(x => x.Id == customerSegment.Id)))
            {
                customerSegmentResouce.AddSegmentAccountsAsync(customerAccountIds, customerSegment.Id).Wait();
            }
        }
        public void Exercise_13_1_Get_Customers()
        {
            //Create a Customer Account resource
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            //Retrieve an Account by id
            var account = customerAccountResource.GetAccountAsync(1001).Result;

            //Write the Account email
            System.Diagnostics.Debug.WriteLine("Account Email[{0}]: {1}", account.Id, account.EmailAddress);

            //You can also filter the Accounts Get call by email
            var accountByEmail = customerAccountResource.GetAccountsAsync(filter: "EmailAddress eq '*****@*****.**'").Result;

            //write account email
            System.Diagnostics.Debug.WriteLine("Account Email[{0}]: {1}", account.EmailAddress, account.Id);

            //Now, create a Customer Contact resource
            var customerContactResource   = new Mozu.Api.Resources.Commerce.Customer.Accounts.CustomerContactResource(_apiContext);
            var customerContactCollection = new Mozu.Api.Contracts.Customer.CustomerContactCollection();

            if (accountByEmail.TotalCount > 0)
            {
                customerContactCollection = customerContactResource.GetAccountContactsAsync(accountByEmail.Items[0].Id).Result;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("No contact information -- Customer does not exist");
            }

            if (customerContactCollection.TotalCount > 0)
            {
                foreach (var contact in customerContactCollection.Items)
                {
                    System.Diagnostics.Debug.WriteLine("Name:");
                    System.Diagnostics.Debug.WriteLine(contact.FirstName);
                    System.Diagnostics.Debug.WriteLine(contact.MiddleNameOrInitial);
                    System.Diagnostics.Debug.WriteLine(contact.LastNameOrSurname);
                    System.Diagnostics.Debug.WriteLine("Address:");
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address1);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address2);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address3);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address4);
                    System.Diagnostics.Debug.WriteLine(contact.Address.CityOrTown);
                    System.Diagnostics.Debug.WriteLine(contact.Address.StateOrProvince);
                    System.Diagnostics.Debug.WriteLine(contact.Address.PostalOrZipCode);
                    System.Diagnostics.Debug.WriteLine(contact.Address.CountryCode);
                    System.Diagnostics.Debug.WriteLine(String.Format("Is a validated address? {0}", contact.Address.IsValidated));
                }
            }

            //Create a Customer Credit resource
            var creditResource = new Mozu.Api.Resources.Commerce.Customer.CreditResource(_apiContext);

            //Get credits by customer account id
            var customerCredits = creditResource.GetCreditsAsync(filter: "CustomerId eq '1001'").Result;

            foreach (var customerCredit in customerCredits.Items)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Customer Credit[{0}]: Code({1})Balance ({2})", customerCredit.CustomerId, customerCredit.Code, customerCredit.CurrentBalance));
            }
        }
        public void Exercise_14_2_Auth_Capture_Order_Payment()
        {
            var orderNumber = 11;

            //Create an Order resource. This resource is used to get, create, update orders
            var orderResource   = new Mozu.Api.Resources.Commerce.OrderResource(_apiContext);
            var paymentResource = new Mozu.Api.Resources.Commerce.Orders.PaymentResource(_apiContext);

            var existingOrder = (orderResource.GetOrdersAsync(filter: "OrderNumber eq '" + orderNumber + "'").Result).Items[0];

            Mozu.Api.Contracts.CommerceRuntime.Payments.Payment authorizedPayment = null;
            Mozu.Api.Contracts.CommerceRuntime.Payments.Payment pendingPayment    = null;

            #region Add BillingInfo from Customer Object
            var customerResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);
            var customerAccount  = customerResource.GetAccountAsync(1002).Result;

            var contactInfo = new Mozu.Api.Contracts.Core.Contact();

            foreach (var contact in customerAccount.Contacts)
            {
                foreach (var type in contact.Types)
                {
                    if (type.IsPrimary)
                    {
                        contactInfo.Address = contact.Address;
                        contactInfo.CompanyOrOrganization = contact.CompanyOrOrganization;
                        contactInfo.Email               = contact.Email;
                        contactInfo.FirstName           = contact.FirstName;
                        contactInfo.LastNameOrSurname   = contact.LastNameOrSurname;
                        contactInfo.MiddleNameOrInitial = contact.MiddleNameOrInitial;
                        contactInfo.PhoneNumbers        = contact.PhoneNumbers;
                    }
                }
            }

            var billingInfo = new Mozu.Api.Contracts.CommerceRuntime.Payments.BillingInfo()
            {
                BillingContact = contactInfo,
                IsSameBillingShippingAddress = true,
                PaymentType = "Check",
            };
            #endregion

            var action = new Mozu.Api.Contracts.CommerceRuntime.Payments.PaymentAction()
            {
                Amount                   = existingOrder.Total,
                CurrencyCode             = "USD",
                InteractionDate          = DateTime.Now,
                NewBillingInfo           = billingInfo,
                ActionName               = "CreatePayment",
                ReferenceSourcePaymentId = null,
                CheckNumber              = "1234"
            };

            try
            {
                authorizedPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Authorized");
                pendingPayment    = existingOrder.Payments.FirstOrDefault(d => d.Status == "Pending");
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            if (authorizedPayment != null)
            {
                action.ActionName = "CapturePayment";
                var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, authorizedPayment.Id).Result;
            }
            else if (pendingPayment != null)
            {
                action.ActionName = "CapturePayment";
                var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, pendingPayment.Id).Result;
            }
            else
            {
                var authPayment = paymentResource.CreatePaymentActionAsync(action, existingOrder.Id).Result;
            }
        }
        public void Exercise_14_2_Auth_Capture_Order_Payment()
        {
            var orderNumber = 12;

            //Create an Order resource. This resource is used to get, create, update orders
            var orderResource = new Mozu.Api.Resources.Commerce.OrderResource(_apiContext);
            var paymentResource = new Mozu.Api.Resources.Commerce.Orders.PaymentResource(_apiContext);

            var existingOrder = (orderResource.GetOrdersAsync(filter: "OrderNumber eq '" + orderNumber + "'").Result).Items[0];
            Mozu.Api.Contracts.CommerceRuntime.Payments.Payment authorizedPayment = null;
            Mozu.Api.Contracts.CommerceRuntime.Payments.Payment pendingPayment = null;

            #region Add BillingInfo from Customer Object
            var customerResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);
            var customerAccount = customerResource.GetAccountAsync(1002).Result;

            var contactInfo = new Mozu.Api.Contracts.Core.Contact();

            foreach (var contact in customerAccount.Contacts)
            {
                foreach (var type in contact.Types)
                {
                    if (type.IsPrimary)
                    {
                        contactInfo.Address = contact.Address;
                        contactInfo.CompanyOrOrganization = contact.CompanyOrOrganization;
                        contactInfo.Email = contact.Email;
                        contactInfo.FirstName = contact.FirstName;
                        contactInfo.LastNameOrSurname = contact.LastNameOrSurname;
                        contactInfo.MiddleNameOrInitial = contact.MiddleNameOrInitial;
                        contactInfo.PhoneNumbers = contact.PhoneNumbers;
                    }
                }
            }

            var billingInfo = new Mozu.Api.Contracts.CommerceRuntime.Payments.BillingInfo()
            {
                BillingContact = contactInfo,
                IsSameBillingShippingAddress = true,
                PaymentType = "Check",
            };
            #endregion

            var action = new Mozu.Api.Contracts.CommerceRuntime.Payments.PaymentAction()
            {
                    Amount = existingOrder.Total,
                    CurrencyCode = "USD",
                    InteractionDate = DateTime.Now,
                    NewBillingInfo = billingInfo,
                    ActionName = "CreatePayment",
                    ReferenceSourcePaymentId = null,
                    CheckNumber = "1234"
            };

            try
            {
                authorizedPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Authorized");
                pendingPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Pending");
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            if(authorizedPayment != null)
            {
                action.ActionName = "CapturePayment";
                var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, authorizedPayment.Id).Result;
            }
            else if(pendingPayment != null)
            {
                action.ActionName = "CapturePayment";
                var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, pendingPayment.Id).Result;
            }
            else
            {
                var authPayment = paymentResource.CreatePaymentActionAsync(action, existingOrder.Id).Result;

            }
        }
        public void Exercise_13_1_Get_Customers()
        {
            //Create a Customer Account resource
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            //Retrieve an Account by id
            var account = customerAccountResource.GetAccountAsync(1001).Result;

            //Write the Account email
            System.Diagnostics.Debug.WriteLine("Account Email[{0}]: {1}", account.Id, account.EmailAddress);

            //You can also filter the Accounts Get call by email
            var accountByEmail = customerAccountResource.GetAccountsAsync(filter: "EmailAddress eq '*****@*****.**'").Result;

            //write account email
            System.Diagnostics.Debug.WriteLine("Account Email[{0}]: {1}", account.EmailAddress, account.Id);

            //Now, create a Customer Contact resource
            var customerContactResource = new Mozu.Api.Resources.Commerce.Customer.Accounts.CustomerContactResource(_apiContext);
            var customerContactCollection = new Mozu.Api.Contracts.Customer.CustomerContactCollection();
            if (accountByEmail.TotalCount > 0)
            {
                customerContactCollection = customerContactResource.GetAccountContactsAsync(accountByEmail.Items[0].Id).Result;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("No contact information -- Customer does not exist");
            }

            if (customerContactCollection.TotalCount > 0)
            {
                foreach (var contact in customerContactCollection.Items)
                {
                    System.Diagnostics.Debug.WriteLine("Name:");
                    System.Diagnostics.Debug.WriteLine(contact.FirstName);
                    System.Diagnostics.Debug.WriteLine(contact.MiddleNameOrInitial);
                    System.Diagnostics.Debug.WriteLine(contact.LastNameOrSurname);
                    System.Diagnostics.Debug.WriteLine("Address:");
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address1);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address2);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address3);
                    System.Diagnostics.Debug.WriteLine(contact.Address.Address4);
                    System.Diagnostics.Debug.WriteLine(contact.Address.CityOrTown);
                    System.Diagnostics.Debug.WriteLine(contact.Address.StateOrProvince);
                    System.Diagnostics.Debug.WriteLine(contact.Address.PostalOrZipCode);
                    System.Diagnostics.Debug.WriteLine(contact.Address.CountryCode);
                    System.Diagnostics.Debug.WriteLine(String.Format("Is a validated address? {0}", contact.Address.IsValidated));
                }
            }

            //Create a Customer Credit resource
            var creditResource = new Mozu.Api.Resources.Commerce.Customer.CreditResource(_apiContext);

            //Get credits by customer account id
            var customerCredits = creditResource.GetCreditsAsync(filter: "CustomerId eq '1001'").Result;

            foreach (var customerCredit in customerCredits.Items)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Customer Credit[{0}]: Code({1})Balance ({2})", customerCredit.CustomerId, customerCredit.Code, customerCredit.CurrentBalance));
            }
        }
        public void Exercise_13_2_Add_New_Customer()
        {
            //Create a Customer Account resource
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            var existingAcct = customerAccountResource.GetAccountsAsync(filter: "EmailAddress eq '*****@*****.**'").Result;

            if (existingAcct == null || existingAcct.TotalCount == 0)
            {
                //Create a new Account Info and Authorization Info contract
                var customerAccountAndAuthInfo = new Mozu.Api.Contracts.Customer.CustomerAccountAndAuthInfo()
                {
                    Account = new Mozu.Api.Contracts.Customer.CustomerAccount()
                    {
                        AcceptsMarketing = false,
                        CompanyOrOrganization = "Serenity Corp.",
                        EmailAddress = "*****@*****.**",
                        ExternalId = "A0001",
                        FirstName = "Malcolm",
                        LastName = "Reynolds",
                        IsActive = true,
                        IsAnonymous = false,
                        LocaleCode = "en-US",
                        TaxExempt = false,
                        IsLocked = false,
                        UserName = "******",
                    },
                    Password = "******",
                    IsImport = true
                };

                var newAccount = customerAccountResource.AddAccountAndLoginAsync(customerAccountAndAuthInfo).Result;

                var contactMal = new Mozu.Api.Contracts.Customer.CustomerContact()
                {
                    Email = "*****@*****.**",
                    FirstName = "Malcolm",
                    LastNameOrSurname = "Reynolds",
                    Label = "Capt.",
                    PhoneNumbers = new Mozu.Api.Contracts.Core.Phone()
                    {
                        Mobile = "555-555-0001"
                    },
                    Address = new Mozu.Api.Contracts.Core.Address()
                    {
                        Address1 = "03-K64 Firefly Transport",
                        AddressType = "Residential",
                        CityOrTown = "Austin",
                        CountryCode = "US",
                        PostalOrZipCode = "78759",
                        StateOrProvince = "TX"
                    },
                    Types = new System.Collections.Generic.List<Mozu.Api.Contracts.Customer.ContactType>()
                    {
                        new Mozu.Api.Contracts.Customer.ContactType()
                        {
                            IsPrimary = true,
                                Name = "Billing"
                        }
                    }
                };

                var contactInara = new Mozu.Api.Contracts.Customer.CustomerContact()
                {
                    Email = "*****@*****.**",
                    FirstName = "Inara",
                    LastNameOrSurname = "Serra",
                    Label = "Ms.",
                    PhoneNumbers = new Mozu.Api.Contracts.Core.Phone()
                    {
                        Mobile = "555-555-0002"
                    },
                    Address = new Mozu.Api.Contracts.Core.Address()
                    {
                        Address1 = "03-K64 Firefly Transport -- Shuttle",
                        AddressType = "Residential",
                        CityOrTown = "Austin",
                        CountryCode = "US",
                        PostalOrZipCode = "78759",
                        StateOrProvince = "TX"
                    },
                    Types = new System.Collections.Generic.List<Mozu.Api.Contracts.Customer.ContactType>()
                    {
                        new Mozu.Api.Contracts.Customer.ContactType()
                        {
                            IsPrimary = false,
                                Name = "Billing"
                        }
                    }
                };

                //Create a Customer Contact resource
                var contactResource = new Mozu.Api.Resources.Commerce.Customer.Accounts.CustomerContactResource(_apiContext);

                //Add new contact
                var newContactMal = contactResource.AddAccountContactAsync(contactMal, newAccount.CustomerAccount.Id).Result;

                //Add additional contact
                var newContactInara = contactResource.AddAccountContactAsync(contactInara, newAccount.CustomerAccount.Id).Result;
            }
                //Create a Customer Credit resource
                var creditResource = new Mozu.Api.Resources.Commerce.Customer.CreditResource(_apiContext);

                //Create a Credit object
                var credit = new Mozu.Api.Contracts.Customer.Credit.Credit()
                {
                    ActivationDate = DateTime.Now,
                    Code = Guid.NewGuid().ToString("N"),
                    CreditType = "GiftCard",
                    CurrencyCode = "USD",
                    CurrentBalance = 1000,
                    CustomerId = 1002,
                    InitialBalance = 1000
                };

                //Add credit
                var newCredit = creditResource.AddCreditAsync(credit).Result;
        }
        public void Exercise_13_2_Add_New_Customer()
        {
            //Create a Customer Account resource
            var customerAccountResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext);

            var existingAcct = customerAccountResource.GetAccountsAsync(filter: "EmailAddress eq '*****@*****.**'").Result;

            if (existingAcct == null || existingAcct.TotalCount == 0)
            {
                //Create a new Account Info and Authorization Info contract
                var customerAccountAndAuthInfo = new Mozu.Api.Contracts.Customer.CustomerAccountAndAuthInfo()
                {
                    Account = new Mozu.Api.Contracts.Customer.CustomerAccount()
                    {
                        AcceptsMarketing      = false,
                        CompanyOrOrganization = "Serenity Corp.",
                        EmailAddress          = "*****@*****.**",
                        ExternalId            = "A0001",
                        FirstName             = "Malcolm",
                        LastName    = "Reynolds",
                        IsActive    = true,
                        IsAnonymous = false,
                        LocaleCode  = "en-US",
                        TaxExempt   = false,
                        IsLocked    = false,
                        UserName    = "******",
                    },
                    Password = "******",
                    IsImport = true
                };

                var newAccount = customerAccountResource.AddAccountAndLoginAsync(customerAccountAndAuthInfo).Result;

                var contactMal = new Mozu.Api.Contracts.Customer.CustomerContact()
                {
                    Email             = "*****@*****.**",
                    FirstName         = "Malcolm",
                    LastNameOrSurname = "Reynolds",
                    Label             = "Capt.",
                    PhoneNumbers      = new Mozu.Api.Contracts.Core.Phone()
                    {
                        Mobile = "555-555-0001"
                    },
                    Address = new Mozu.Api.Contracts.Core.Address()
                    {
                        Address1        = "03-K64 Firefly Transport",
                        AddressType     = "Residential",
                        CityOrTown      = "Austin",
                        CountryCode     = "US",
                        PostalOrZipCode = "78759",
                        StateOrProvince = "TX"
                    },
                    Types = new System.Collections.Generic.List <Mozu.Api.Contracts.Customer.ContactType>()
                    {
                        new Mozu.Api.Contracts.Customer.ContactType()
                        {
                            IsPrimary = true,
                            Name      = "Billing"
                        }
                    }
                };

                var contactInara = new Mozu.Api.Contracts.Customer.CustomerContact()
                {
                    Email             = "*****@*****.**",
                    FirstName         = "Inara",
                    LastNameOrSurname = "Serra",
                    Label             = "Ms.",
                    PhoneNumbers      = new Mozu.Api.Contracts.Core.Phone()
                    {
                        Mobile = "555-555-0002"
                    },
                    Address = new Mozu.Api.Contracts.Core.Address()
                    {
                        Address1        = "03-K64 Firefly Transport -- Shuttle",
                        AddressType     = "Residential",
                        CityOrTown      = "Austin",
                        CountryCode     = "US",
                        PostalOrZipCode = "78759",
                        StateOrProvince = "TX"
                    },
                    Types = new System.Collections.Generic.List <Mozu.Api.Contracts.Customer.ContactType>()
                    {
                        new Mozu.Api.Contracts.Customer.ContactType()
                        {
                            IsPrimary = false,
                            Name      = "Billing"
                        }
                    }
                };

                //Create a Customer Contact resource
                var contactResource = new Mozu.Api.Resources.Commerce.Customer.Accounts.CustomerContactResource(_apiContext);

                //Add new contact
                var newContactMal = contactResource.AddAccountContactAsync(contactMal, newAccount.CustomerAccount.Id).Result;

                //Add additional contact
                var newContactInara = contactResource.AddAccountContactAsync(contactInara, newAccount.CustomerAccount.Id).Result;
            }
            //Create a Customer Credit resource
            var creditResource = new Mozu.Api.Resources.Commerce.Customer.CreditResource(_apiContext);

            //Create a Credit object
            var credit = new Mozu.Api.Contracts.Customer.Credit.Credit()
            {
                ActivationDate = DateTime.Now,
                Code           = Guid.NewGuid().ToString("N"),
                CreditType     = "GiftCard",
                CurrencyCode   = "USD",
                CurrentBalance = 1000,
                CustomerId     = 1002,
                InitialBalance = 1000
            };

            //Add credit
            var newCredit = creditResource.AddCreditAsync(credit).Result;
        }