public void Can_Create_Invoice_Subscription()
        {
            // Arrange
            var product = Chargify.GetProductList().Values.DefaultIfEmpty(null).FirstOrDefault(p => p.PriceInCents > 0 && p.RequireCreditCard == false);

            if (product == null)
            {
                Assert.Inconclusive("No product to test"); return;
            }
            var referenceID = Guid.NewGuid().ToString();
            var newCustomer = new CustomerAttributes(Faker.Name.First(), Faker.Name.Last(), Faker.Internet.Email(), Faker.Phone.Number(), Faker.Company.Name(), referenceID);

            // Act
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, PaymentCollectionMethod.Invoice);

            // Assert
            //Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Phone == newCustomer.Phone);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceID);
            Assert.IsTrue(newSubscription.ProductPriceInCents == product.PriceInCents);
            Assert.IsTrue(newSubscription.ProductPrice == product.Price);
            Assert.IsTrue(newSubscription.PaymentCollectionMethod == PaymentCollectionMethod.Invoice);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Can_Reactivate_With_Trial()
        {
            // Arrange
            var trialingProduct     = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0);
            var referenceId         = Guid.NewGuid().ToString();
            var expMonth            = DateTime.Now.AddMonths(1).Month;
            var expYear             = DateTime.Now.AddMonths(12).Year;
            var newCustomer         = new CustomerAttributes(Faker.Name.First(), Faker.Name.Last(), Faker.Internet.Email(), Faker.Phone.Number(), Faker.Company.Name(), referenceId);
            var newPaymentInfo      = GetTestPaymentMethod(newCustomer);
            var createdSubscription = Chargify.CreateSubscription(trialingProduct.Handle, newCustomer, newPaymentInfo);

            Assert.IsNotNull(createdSubscription);
            var deletedSubscription = Chargify.DeleteSubscription(createdSubscription.SubscriptionID, "Delete for test Subscription_Can_Reactivate_With_Trial");

            Assert.IsNotNull(deletedSubscription);
            var foundSubscription = Chargify.Find <Subscription>(createdSubscription.SubscriptionID);

            Assert.IsTrue(foundSubscription.State == SubscriptionState.Canceled, "Expected cancelled subscription on a trial product");

            // Act
            var result = Chargify.ReactivateSubscription(foundSubscription.SubscriptionID, true, null, null);

            // Assert
            Assert.IsNotNull(result);
#if !NUNIT
            Assert.IsInstanceOfType(result, typeof(ISubscription));
#endif
            Assert.IsTrue(result.State != foundSubscription.State);
            Assert.IsTrue(result.State == SubscriptionState.Trialing);
        }
        public void Can_Create_Invoice_Subscription_For_Existing_Customer()
        {
            // Arrange
            var       customers      = Chargify.GetCustomerList().Keys;
            var       referenceValue = customers.FirstOrDefault(systemID => !string.IsNullOrWhiteSpace(systemID));
            ICustomer customer       = Chargify.LoadCustomer(referenceValue);
            var       products       = Chargify.GetProductList().Values;
            var       product        = products.FirstOrDefault(p => p.PriceInCents > 0 && !p.RequireCreditCard);

            Assert.IsNotNull(product, "No valid product was found.");

            // Act
            var result = Chargify.CreateSubscription(product.Handle, customer.ChargifyID, PaymentCollectionMethod.Invoice);

            // Assert
            //Assert.IsInstanceOfType(result, typeof(Subscription));
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Customer);
            Assert.IsNull(result.PaymentProfile);
            Assert.IsTrue(result.SubscriptionID > int.MinValue);
            Assert.IsTrue(result.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(result.Customer.FirstName == customer.FirstName);
            Assert.IsTrue(result.Customer.LastName == customer.LastName);
            Assert.IsTrue(result.Customer.Email == customer.Email);
            Assert.IsTrue(result.Customer.Phone == customer.Phone);
            Assert.IsTrue(result.Customer.Organization == customer.Organization);
            Assert.IsTrue(result.Customer.SystemID == customer.SystemID);
            Assert.IsTrue(result.ProductPriceInCents == product.PriceInCents);
            Assert.IsTrue(result.ProductPrice == product.Price);
            Assert.IsTrue(result.PaymentCollectionMethod == PaymentCollectionMethod.Invoice);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(result.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Reactivation()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceId    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes(Faker.Name.First(), Faker.Name.Last(), Faker.Internet.Email(), Faker.Phone.Number(), Faker.Company.Name(), referenceId);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            // Act
            var newSubscription    = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo);
            var result             = Chargify.DeleteSubscription(newSubscription.SubscriptionID, "testing");
            var foundSubscription  = Chargify.Find <Subscription>(newSubscription.SubscriptionID);
            var resultSubscription = Chargify.ReactivateSubscription(foundSubscription.SubscriptionID);

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
            Assert.IsInstanceOfType(foundSubscription, typeof(Subscription));
            Assert.IsInstanceOfType(resultSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsTrue(result);
            Assert.IsNotNull(foundSubscription);
            Assert.IsTrue(foundSubscription.State == SubscriptionState.Canceled);
            Assert.IsNotNull(newSubscription);
            Assert.IsTrue(resultSubscription.State == SubscriptionState.Active);
        }
Exemple #5
0
        public void Subscription_Create()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceId    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes(Faker.Name.First(), Faker.Name.Last(), Faker.Internet.Email(), Faker.Phone.Number(), Faker.Company.Name(), referenceId);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            // Act
            string data = string.Empty;

            Chargify.LogRequest = (requestMethod, address, postedData) => {
                data = postedData;
            };
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo);

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.AreEqual(newCustomer.FirstName, newSubscription.Customer.FirstName);
            Assert.AreEqual(newCustomer.LastName, newSubscription.Customer.LastName);
            Assert.AreEqual(newCustomer.Email, newSubscription.Customer.Email);
            Assert.AreEqual(newCustomer.Phone, newSubscription.Customer.Phone);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceId);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(newSubscription.ProductPriceInCents == product.PriceInCents);
            Assert.IsTrue(newSubscription.ProductPrice == product.Price);
            Assert.AreEqual(product.TrialInterval > 0 ? SubscriptionState.Trialing : SubscriptionState.Active, newSubscription.State);
            if (Chargify.UseJSON)
            {
                Assert.AreEqual(true, data.IsJSON());
                Assert.AreEqual(false, data.IsXml());
            }
            else
            {
                Assert.AreEqual(true, data.IsXml());
                Assert.AreEqual(false, data.IsJSON());
            }

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Reactivation()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceID    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "123-456-7890", "Chargify", referenceID);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            // Act
            var newSubscription    = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo);
            var result             = Chargify.DeleteSubscription(newSubscription.SubscriptionID, "testing");
            var foundSubscription  = Chargify.Find <Subscription>(newSubscription.SubscriptionID);
            var resultSubscription = Chargify.ReactivateSubscription(foundSubscription.SubscriptionID);

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
            Assert.IsInstanceOfType(foundSubscription, typeof(Subscription));
            Assert.IsInstanceOfType(resultSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsTrue(result);
            Assert.IsNotNull(foundSubscription);
            Assert.IsTrue(foundSubscription.State == SubscriptionState.Canceled);
            Assert.IsNotNull(newSubscription);
            Assert.IsTrue(resultSubscription.State == SubscriptionState.Active);
        }
        public void Subscription_Can_Reactivate_Without_Trial()
        {
            // Arrange
            var trialingProduct     = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0);
            var referenceID         = Guid.NewGuid().ToString();
            var expMonth            = DateTime.Now.AddMonths(1).Month;
            var expYear             = DateTime.Now.AddMonths(12).Year;
            var newCustomer         = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "123-456-7890", "Chargify", referenceID);
            var newPaymentInfo      = GetTestPaymentMethod(newCustomer);
            var createdSubscription = Chargify.CreateSubscription(trialingProduct.Handle, newCustomer, newPaymentInfo);

            Assert.IsNotNull(createdSubscription);
            var deletedSubscription = Chargify.DeleteSubscription(createdSubscription.SubscriptionID, "Delete for test Subscription_Can_Reactivate_With_Trial");

            Assert.IsNotNull(deletedSubscription);
            var foundSubscription = Chargify.Find <Subscription>(createdSubscription.SubscriptionID);

            Assert.IsTrue(foundSubscription.State == SubscriptionState.Canceled, "Expected cancelled subscription on a trial product");

            // Act
            var result = Chargify.ReactivateSubscription(foundSubscription.SubscriptionID, false);

            // Assert
            Assert.IsNotNull(result);
#if !NUNIT
            Assert.IsInstanceOfType(result, typeof(ISubscription));
#endif
            Assert.IsTrue(result.State != foundSubscription.State);
            Assert.IsTrue(result.State == SubscriptionState.Active);
        }
        public void Subscription_Create_WithTwoComponents()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceID    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "Chargify", referenceID);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            Dictionary <int, string> componentsToUse = new Dictionary <int, string>()
            {
                { 998, "5" },
                { 6776, "1" }
            };

            // Act
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo, componentsToUse);
            var subComponents   = Chargify.GetComponentsForSubscription(newSubscription.SubscriptionID);
            var usedComponents  = from c in subComponents
                                  where componentsToUse.ContainsKey(c.Value.ComponentID)
                                  select c;

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceID);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(usedComponents.Count() == 2);
            Assert.IsTrue(usedComponents.FirstOrDefault().Value.AllocatedQuantity == 5);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Create_WithTwoComponents()
        {
            // Arrange
            var product         = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceId     = Guid.NewGuid().ToString();
            var newCustomer     = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "Chargify", referenceId);
            var newPaymentInfo  = GetTestPaymentMethod(newCustomer);
            var components      = Chargify.GetComponentsForProductFamily(product.ProductFamily.ID);
            var componentsToUse = components.Take(2).ToDictionary(v => v.Key, v => "1");

            // Act
            Assert.IsNotNull(product, "Product couldn't be found");
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo, componentsToUse);
            var subComponents   = Chargify.GetComponentsForSubscription(newSubscription.SubscriptionID);
            var usedComponents  = from c in subComponents
                                  where componentsToUse.ContainsKey(c.Value.ComponentID)
                                  select c;

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceId);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(usedComponents.Count() == componentsToUse.Count);
            foreach (var component in usedComponents)
            {
                Assert.IsTrue(componentsToUse.ContainsKey(component.Key));
                //Assert.AreEqual(decimal.Parse(componentsToUse[component.Key]), component.Value.AllocatedQuantity);
            }

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Create_WithComponent()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var productFamily  = Chargify.GetProductFamilyList().Values.FirstOrDefault();
            var referenceId    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "Chargify", referenceId);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);
            var component      = Chargify.GetComponentsForProductFamily(productFamily.ID).FirstOrDefault(d => d.Value.Kind == ComponentType.Quantity_Based_Component && d.Value.Prices.Any(p => p.UnitPrice > 0m)).Value;

            Assert.IsNotNull(component, "Couldn't find any usable component.");

            // Act
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo, component.ID, 5);
            var subComponents   = Chargify.GetComponentsForSubscription(newSubscription.SubscriptionID);
            var usedComponents  = from c in subComponents
                                  where c.Value.ComponentID == component.ID
                                  select c;

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceId);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(usedComponents.Count() == 1);
            Assert.IsTrue(usedComponents.FirstOrDefault().Value.AllocatedQuantity == 5);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Create_With_SpecialChars()
        {
            var product     = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceId = Guid.NewGuid().ToString();
            var expMonth    = DateTime.Now.AddMonths(1).Month;
            var expYear     = DateTime.Now.AddMonths(12).Year;
            var newCustomer = new CustomerAttributes("Scott!", "Pilgrim@", "*****@*****.**", "+1 (123) 456-7890", "@Chargify#$%^&@", referenceId);

            newCustomer.ShippingAddress = @"123 Main St.*()-=_+`~";
            newCustomer.ShippingCity    = @"Kingston{}[]|;':";
            newCustomer.ShippingState   = @"ON<>,.?/";
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            // Act
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo);

            // Assert
#if !NUNIT
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
#endif
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Phone == newCustomer.Phone);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceId);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_Create()
        {
            // Arrange
            var product        = Chargify.GetProductList().Values.FirstOrDefault();
            var referenceID    = Guid.NewGuid().ToString();
            var expMonth       = DateTime.Now.AddMonths(1).Month;
            var expYear        = DateTime.Now.AddMonths(12).Year;
            var newCustomer    = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "123-456-7890", "Chargify", referenceID);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);

            // Act
            var newSubscription = Chargify.CreateSubscription(product.Handle, newCustomer, newPaymentInfo);

            // Assert
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Phone == newCustomer.Phone);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceID);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(newSubscription.ProductPriceInCents == product.PriceInCents);
            Assert.IsTrue(newSubscription.ProductPrice == product.Price);

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }
        public void Subscription_ReactivateExpired()
        {
            // Arrange
            var subscription = Chargify.GetSubscriptionList().FirstOrDefault(s => s.Value.State == SubscriptionState.Expired).Value;

            if (subscription != null)
            {
                // Act
                if (subscription.BalanceInCents > 0)
                {
                    var resetResult = Chargify.ResetSubscriptionBalance(subscription.SubscriptionID);
                }
                var cancelledResult  = Chargify.DeleteSubscription(subscription.SubscriptionID, "");
                var reactivateResult = Chargify.ReactivateSubscription(subscription.SubscriptionID);

                // Assert
                Assert.IsNotNull(reactivateResult);
                Assert.IsTrue(reactivateResult.State == SubscriptionState.Active);
            }
        }
        public void Components_Create_Subscription_Multiple_Components()
        {
            // Arrange
            var product = Chargify.GetProductList().Values.FirstOrDefault();

            Assert.IsNotNull(product, "Product couldn't be found");
            var referenceId    = Guid.NewGuid().ToString();
            var newCustomer    = new CustomerAttributes("Scott", "Pilgrim", "*****@*****.**", "Chargify", referenceId);
            var newPaymentInfo = GetTestPaymentMethod(newCustomer);
            // Find components that allow for a simple allocated_quantity = 1 to work for this simple test
            var components      = Chargify.GetComponentsForProductFamily(product.ProductFamily.ID).Values.Where(c => c.Kind == ComponentType.Quantity_Based_Component || c.Kind == ComponentType.On_Off_Component);
            var componentsToUse = components.Take(2).ToList();
            var options         = new SubscriptionCreateOptions()
            {
                CustomerAttributes   = newCustomer,
                CreditCardAttributes = newPaymentInfo,
                ProductHandle        = product.Handle,
                Components           = new System.Collections.Generic.List <ComponentDetails>
                {
                    new ComponentDetails()
                    {
                        ComponentID = componentsToUse.First().ID, AllocatedQuantity = 1
                    },
                    new ComponentDetails()
                    {
                        ComponentID = componentsToUse.Last().ID, AllocatedQuantity = 1
                    }
                }
            };

            // Act
            var newSubscription = Chargify.CreateSubscription(options);
            var subComponents   = Chargify.GetComponentsForSubscription(newSubscription.SubscriptionID);
            var usedComponents  = from c in subComponents
                                  where componentsToUse.Any(x => x.ID == c.Value.ComponentID)
                                  select c;

            // Assert
            Assert.IsInstanceOfType(newSubscription, typeof(Subscription));
            Assert.IsNotNull(newSubscription);
            Assert.IsNotNull(newSubscription.Customer);
            Assert.IsNotNull(newSubscription.PaymentProfile);
            Assert.IsTrue(newSubscription.SubscriptionID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.ChargifyID > int.MinValue);
            Assert.IsTrue(newSubscription.Customer.FirstName == newCustomer.FirstName);
            Assert.IsTrue(newSubscription.Customer.LastName == newCustomer.LastName);
            Assert.IsTrue(newSubscription.Customer.Email == newCustomer.Email);
            Assert.IsTrue(newSubscription.Customer.Organization == newCustomer.Organization);
            Assert.IsTrue(newSubscription.Customer.SystemID == referenceId);
            Assert.IsTrue(newSubscription.PaymentProfile.FirstName == newPaymentInfo.FirstName);
            Assert.IsTrue(newSubscription.PaymentProfile.LastName == newPaymentInfo.LastName);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationMonth == newPaymentInfo.ExpirationMonth);
            Assert.IsTrue(newSubscription.PaymentProfile.ExpirationYear == newPaymentInfo.ExpirationYear);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress == newPaymentInfo.BillingAddress);
            //Assert.IsTrue(newSubscription.PaymentProfile.BillingAddress2 == newPaymentInfo.BillingAddress2);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCity == newPaymentInfo.BillingCity);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingCountry == newPaymentInfo.BillingCountry);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingState == newPaymentInfo.BillingState);
            Assert.IsTrue(newSubscription.PaymentProfile.BillingZip == newPaymentInfo.BillingZip);
            Assert.IsTrue(usedComponents.Count() == componentsToUse.Count);
            foreach (var component in usedComponents)
            {
                Assert.IsTrue(componentsToUse.Any(x => x.ID == component.Key));
                //Assert.AreEqual(decimal.Parse(componentsToUse[component.Key]), component.Value.AllocatedQuantity);
            }

            // Cleanup
            Assert.IsTrue(Chargify.DeleteSubscription(newSubscription.SubscriptionID, "Automatic cancel due to test"));
        }