A class for building requests to manipulate Address records in the vault.
상속: Braintree.Request
예제 #1
0
        public void Find_FindsAddress()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;

            var addressRequest = new AddressRequest
            {
                FirstName = "Michael",
                LastName = "Angelo",
                Company = "Angelo Co.",
                StreetAddress = "1 E Main St",
                ExtendedAddress = "Apt 3",
                Locality = "Chicago",
                Region = "IL",
                PostalCode = "60622",
                CountryName = "United States of America"
            };

            Address createdAddress = gateway.Address.Create(customer.Id, addressRequest).Target;
            Address address = gateway.Address.Find(customer.Id, createdAddress.Id);

            Assert.AreEqual("Michael", address.FirstName);
            Assert.AreEqual("Angelo", address.LastName);
            Assert.AreEqual("Angelo Co.", address.Company);
            Assert.AreEqual("1 E Main St", address.StreetAddress);
            Assert.AreEqual("Apt 3", address.ExtendedAddress);
            Assert.AreEqual("Chicago", address.Locality);
            Assert.AreEqual("IL", address.Region);
            Assert.AreEqual("60622", address.PostalCode);
            Assert.AreEqual("United States of America", address.CountryName);
        }
        public async Task AddAddressAsync(string email, string firstName, string lastName, string company, string streetAddress, string extendedAddress, string locality, string region, string postalCode, string countryName)
        {
            Customer customer = await GetCustomerAsync(email);

            Braintree.AddressRequest newAddress = new Braintree.AddressRequest
            {
                FirstName       = firstName,
                LastName        = lastName,
                Company         = company,
                CountryName     = countryName,
                PostalCode      = postalCode,
                ExtendedAddress = extendedAddress,
                Locality        = locality,
                Region          = region,
                StreetAddress   = streetAddress
            };

            await gateway.Address.CreateAsync(customer.Id, newAddress);
        }
예제 #3
0
        public void AddAddress(string email, string firstName, string lastName, string company, string streetAddress, string extendedAddress, string locality, string region, string postalCode, string countryName)
        {
            Customer c = GetCustomer(email);

            Braintree.AddressRequest newAddress = new Braintree.AddressRequest
            {
                FirstName       = firstName,
                LastName        = lastName,
                Company         = company,
                CountryName     = countryName,
                PostalCode      = postalCode,
                ExtendedAddress = extendedAddress,
                Locality        = locality,
                Region          = region,
                StreetAddress   = streetAddress
            };

            gateway.Address.Create(c.Id, newAddress);
        }
예제 #4
0
        public void Create_CreatesAddressForGivenCustomerId()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;

            var addressRequest = new AddressRequest
            {
                FirstName = "Michael",
                LastName = "Angelo",
                Company = "Angelo Co.",
                StreetAddress = "1 E Main St",
                ExtendedAddress = "Apt 3",
                Locality = "Chicago",
                Region = "IL",
                PostalCode = "60622",
                CountryCodeAlpha2 = "US",
                CountryCodeAlpha3 = "USA",
                CountryCodeNumeric = "840",
                CountryName = "United States of America"
            };

            Address address = gateway.Address.Create(customer.Id, addressRequest).Target;

            Assert.AreEqual("Michael", address.FirstName);
            Assert.AreEqual("Angelo", address.LastName);
            Assert.AreEqual("Angelo Co.", address.Company);
            Assert.AreEqual("1 E Main St", address.StreetAddress);
            Assert.AreEqual("Apt 3", address.ExtendedAddress);
            Assert.AreEqual("Chicago", address.Locality);
            Assert.AreEqual("IL", address.Region);
            Assert.AreEqual("60622", address.PostalCode);
            Assert.AreEqual("US", address.CountryCodeAlpha2);
            Assert.AreEqual("USA", address.CountryCodeAlpha3);
            Assert.AreEqual("840", address.CountryCodeNumeric);
            Assert.AreEqual("United States of America", address.CountryName);
            Assert.IsNotNull(address.CreatedAt);
            Assert.IsNotNull(address.UpdatedAt);
        }
예제 #5
0
        public void Create_ReturnsAnErrorResult_ForInconsistentCountry()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;
            AddressRequest request = new AddressRequest()
            {
                CountryName = "United States of America",
                CountryCodeAlpha2 = "BZ"
            };

            Result<Address> createResult = gateway.Address.Create(customer.Id, request);
            Assert.IsFalse(createResult.IsSuccess());
            Assert.AreEqual(
                ValidationErrorCode.ADDRESS_INCONSISTENT_COUNTRY,
                createResult.Errors.ForObject("Address").OnField("Base")[0].Code
            );
        }
예제 #6
0
        public void Create_ReturnsAnErrorResult()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;
            AddressRequest request = new AddressRequest() { CountryName = "United States of Hammer" };

            Result<Address> createResult = gateway.Address.Create(customer.Id, request);
            Assert.IsFalse(createResult.IsSuccess());
            Dictionary<String, String> parameters = createResult.Parameters;
            Assert.AreEqual("integration_merchant_id", parameters["merchant_id"]);
            Assert.AreEqual(customer.Id, parameters["customer_id"]);
            Assert.AreEqual("United States of Hammer", parameters["address[country_name]"]);
        }
예제 #7
0
        public void Delete_DeletesTheAddress()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;

            var addressRequest = new AddressRequest
            {
                StreetAddress = "1 E Main St",
                ExtendedAddress = "Apt 3",
            };

            Address createdAddress = gateway.Address.Create(customer.Id, addressRequest).Target;
            Assert.AreEqual(createdAddress.Id, gateway.Address.Find(customer.Id, createdAddress.Id).Id);
            gateway.Address.Delete(customer.Id, createdAddress.Id);
            try
            {
                gateway.Address.Find(customer.Id, createdAddress.Id);
                Assert.Fail("Expected NotFoundException.");
            }
            catch (NotFoundException)
            {
                // expected
            }
        }
예제 #8
0
        public void Update_UpdatesAddress_WithInconsistenCounty()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;

            var addressCreateRequest = new AddressRequest
            {
                FirstName = "Dave",
                LastName = "Inchy",
                Company = "Leon Ardo Co.",
                StreetAddress = "1 E State St",
                ExtendedAddress = "Apt 4",
                Locality = "Boston",
                Region = "MA",
                PostalCode = "11111",
                CountryName = "Canada",
                CountryCodeAlpha2 = "CA",
                CountryCodeAlpha3 = "CAN",
                CountryCodeNumeric = "124"
            };

            Address originalAddress = gateway.Address.Create(customer.Id, addressCreateRequest).Target;

            var addressUpdateRequest = new AddressRequest
            {
                FirstName = "Michael",
                LastName = "Angelo",
                Company = "Angelo Co.",
                StreetAddress = "1 E Main St",
                ExtendedAddress = "Apt 3",
                Locality = "Chicago",
                Region = "IL",
                PostalCode = "60622",
                CountryName = "United States of America",
                CountryCodeAlpha3 = "MEX"
            };

            Result<Address> result = gateway.Address.Update(customer.Id, originalAddress.Id, addressUpdateRequest);

            Assert.AreEqual(
                ValidationErrorCode.ADDRESS_INCONSISTENT_COUNTRY,
                result.Errors.ForObject("Address").OnField("Base")[0].Code
            );
        }
예제 #9
0
        public void Update_UpdatesAddressForGivenCustomerIdAndAddressId()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;

            var addressCreateRequest = new AddressRequest
            {
                FirstName = "Dave",
                LastName = "Inchy",
                Company = "Leon Ardo Co.",
                StreetAddress = "1 E State St",
                ExtendedAddress = "Apt 4",
                Locality = "Boston",
                Region = "MA",
                PostalCode = "11111",
                CountryName = "Canada",
                CountryCodeAlpha2 = "CA",
                CountryCodeAlpha3 = "CAN",
                CountryCodeNumeric = "124"
            };

            Address originalAddress = gateway.Address.Create(customer.Id, addressCreateRequest).Target;

            var addressUpdateRequest = new AddressRequest
            {
                FirstName = "Michael",
                LastName = "Angelo",
                Company = "Angelo Co.",
                StreetAddress = "1 E Main St",
                ExtendedAddress = "Apt 3",
                Locality = "Chicago",
                Region = "IL",
                PostalCode = "60622",
                CountryName = "United States of America",
                CountryCodeAlpha2 = "US",
                CountryCodeAlpha3 = "USA",
                CountryCodeNumeric = "840"
            };

            Address address = gateway.Address.Update(customer.Id, originalAddress.Id, addressUpdateRequest).Target;

            Assert.AreEqual("Michael", address.FirstName);
            Assert.AreEqual("Angelo", address.LastName);
            Assert.AreEqual("Angelo Co.", address.Company);
            Assert.AreEqual("1 E Main St", address.StreetAddress);
            Assert.AreEqual("Apt 3", address.ExtendedAddress);
            Assert.AreEqual("Chicago", address.Locality);
            Assert.AreEqual("IL", address.Region);
            Assert.AreEqual("60622", address.PostalCode);
            Assert.AreEqual("United States of America", address.CountryName);
            Assert.AreEqual("US", address.CountryCodeAlpha2);
            Assert.AreEqual("USA", address.CountryCodeAlpha3);
            Assert.AreEqual("840", address.CountryCodeNumeric);
        }
예제 #10
0
        public virtual Result<Address> Create(String customerId, AddressRequest request)
        {
            XmlNode addressXML = Service.Post("/customers/" + customerId + "/addresses", request);

            return new ResultImpl<Address>(new NodeWrapper(addressXML), Service);
        }
예제 #11
0
        public virtual Result <Address> Update(String customerId, String id, AddressRequest request)
        {
            XmlNode addressXML = Service.Put("/customers/" + customerId + "/addresses/" + id, request);

            return(new ResultImpl <Address>(new NodeWrapper(addressXML), Service));
        }
예제 #12
0
        public virtual async Task <Result <Address> > UpdateAsync(string customerId, string id, AddressRequest request)
        {
            XmlNode addressXML = await Service.PutAsync(Service.MerchantPath() + "/customers/" + customerId + "/addresses/" + id, request).ConfigureAwait(false);

            return(new ResultImpl <Address>(new NodeWrapper(addressXML), Gateway));
        }
예제 #13
0
        public virtual async Task <Result <Address> > CreateAsync(string customerId, AddressRequest request)
        {
            XmlNode addressXML = await Service.PostAsync(Service.MerchantPath() + "/customers/" + customerId + "/addresses", request);

            return(new ResultImpl <Address>(new NodeWrapper(addressXML), Gateway));
        }
예제 #14
0
        private AddressRequest GetBillingAddress()
        {
            var addressRequest = new AddressRequest
            {
                FirstName = _cartModel.BillingAddress.FirstName,
                LastName = _cartModel.BillingAddress.LastName,
                StreetAddress = _cartModel.BillingAddress.Address1,
                Locality = _cartModel.BillingAddress.City,
                Region = _cartModel.BillingAddress.StateProvince,
                PostalCode = _cartModel.BillingAddress.PostalCode,
                CountryCodeAlpha2 = _cartModel.BillingAddress.CountryCode
            };
            if (!string.IsNullOrWhiteSpace(_cartModel.BillingAddress.Company))
                addressRequest.Company = _cartModel.BillingAddress.Company;
            if (!string.IsNullOrWhiteSpace(_cartModel.BillingAddress.Address2))
                addressRequest.ExtendedAddress = _cartModel.BillingAddress.Address2;

            return addressRequest;
        }
예제 #15
0
        public virtual Result<Address> Update(string customerId, string id, AddressRequest request)
        {
            XmlNode addressXML = Service.Put(Service.MerchantPath() + "/customers/" + customerId + "/addresses/" + id, request);

            return new ResultImpl<Address>(new NodeWrapper(addressXML), Gateway);
        }
예제 #16
0
        public virtual Result<Address> Update(String customerId, String id, AddressRequest request)
        {
            XmlNode addressXML = Service.Put("/customers/" + customerId + "/addresses/" + id, request);

            return new Result<Address>(new NodeWrapper(addressXML), Service);
        }
예제 #17
0
        public void Create_ReturnsAnErrorResult_ForIncorrectNumeric()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;
            AddressRequest request = new AddressRequest()
            {
                CountryCodeNumeric = "000"
            };

            Result<Address> createResult = gateway.Address.Create(customer.Id, request);
            Assert.IsFalse(createResult.IsSuccess());
            Assert.AreEqual(
                ValidationErrorCode.ADDRESS_COUNTRY_CODE_NUMERIC_IS_NOT_ACCEPTED,
                createResult.Errors.ForObject("Address").OnField("CountryCodeNumeric")[0].Code
            );
        }
예제 #18
0
        public virtual Result <Address> Update(string customerId, string id, AddressRequest request)
        {
            XmlNode addressXML = Service.Put(Service.MerchantPath() + "/customers/" + customerId + "/addresses/" + id, request);

            return(new ResultImpl <Address>(new NodeWrapper(addressXML), Gateway));
        }
예제 #19
0
        public void Create_AcceptsBillingAddressId()
        {
            Customer customer = gateway.Customer.Create(new CustomerRequest()).Target;
            AddressRequest addressRequest = new AddressRequest
            {
                FirstName = "John",
                CountryName = "Chad",
                CountryCodeAlpha2 = "TD",
                CountryCodeAlpha3 = "TCD",
                CountryCodeNumeric = "148"
            };

            Address address = gateway.Address.Create(customer.Id, addressRequest).Target;

            var creditCardRequest = new CreditCardRequest
            {
                CustomerId = customer.Id,
                Number = "5105105105105100",
                ExpirationDate = "05/12",
                CVV = "123",
                CardholderName = "Michael Angelo",
                BillingAddressId = address.Id
            };

            CreditCard creditCard = gateway.CreditCard.Create(creditCardRequest).Target;

            Address billingAddress = creditCard.BillingAddress;
            Assert.AreEqual(address.Id, billingAddress.Id);
            Assert.AreEqual("Chad", billingAddress.CountryName);
            Assert.AreEqual("TD", billingAddress.CountryCodeAlpha2);
            Assert.AreEqual("TCD", billingAddress.CountryCodeAlpha3);
            Assert.AreEqual("148", billingAddress.CountryCodeNumeric);
        }
예제 #20
0
        public async Task <ActionResult> Index(CheckoutModel model)
        {
            //Check if the model-state is valid -- this will catch anytime someone hacks your client-side validation
            if (ModelState.IsValid)
            {
                if (model.CustomerId == null)
                {
                    BraintreeService service = new BraintreeService(this._braintreeGateway);
                    model.CustomerId = await service.GetCustomerId(model.Email, model.Phone, model.FirstName, model.LastName);
                }
                if ((model.CardToken == "NewCard") || (model.CardToken == null))
                {
                    Braintree.CreditCardRequest card = new Braintree.CreditCardRequest();
                    card.Number          = model.CreditCardNumber;
                    card.CVV             = model.CreditCardVerificationValue;
                    card.ExpirationMonth = model.CreditCardExpirationMonth.ToString().PadLeft(2, '0');
                    card.ExpirationYear  = model.CreditCardExpirationYear.ToString();
                    card.CardholderName  = model.BillingFirstName + " " + model.BillingLastName;
                    card.CustomerId      = model.CustomerId;
                    var cardResult = await _braintreeGateway.CreditCard.CreateAsync(card);

                    model.CardToken = cardResult.Target.Token;
                }

                if ((model.BillingAddressId == "NewAddress") || (model.BillingAddressId == null))
                {
                    Braintree.AddressRequest address = new Braintree.AddressRequest();
                    address.StreetAddress = model.BillingAddressLine1 + " " + model.BillingAddressLine2;
                    address.CountryName   = model.BillingCountry;
                    address.Locality      = model.BillingCity;
                    address.PostalCode    = model.BillingPostalCode;
                    address.Region        = model.BillingState;

                    var addressResult = await _braintreeGateway.Address.CreateAsync(model.CustomerId, address);

                    model.BillingAddressId = addressResult.Target.Id;
                }
                if ((model.ShippingAddressId == "NewAddress") || (model.ShippingAddressId == null))
                {
                    if ((model.ShippingAddressLine1 == model.BillingAddressLine1) &&
                        (model.ShippingAddressLine2 == model.BillingAddressLine2) &&
                        (model.ShippingCity == model.BillingCity) &&
                        (model.ShippingPostalCode == model.BillingPostalCode) &&
                        (model.ShippingState == model.BillingState) &&
                        (model.ShippingCountry == model.BillingCountry))
                    {
                        model.ShippingAddressId = model.BillingAddressId;
                    }
                    else
                    {
                        Braintree.AddressRequest address = new Braintree.AddressRequest();
                        address.StreetAddress = model.ShippingAddressLine1 + " " + model.ShippingAddressLine2;
                        address.CountryName   = model.ShippingCountry;
                        address.Locality      = model.ShippingCity;
                        address.PostalCode    = model.ShippingPostalCode;
                        address.Region        = model.ShippingState;

                        var addressResult = await _braintreeGateway.Address.CreateAsync(model.CustomerId, address);

                        model.ShippingAddressId = addressResult.Target.Id;
                    }
                }


                Shipment s = new Shipment
                {
                    AddressLine1 = model.ShippingAddressLine1,
                    AddressLine2 = model.ShippingAddressLine2,
                    City         = model.ShippingCity,
                    State        = model.ShippingState,
                    PostalCode   = model.ShippingPostalCode,
                    Country      = model.ShippingCountry,
                    Modified     = DateTime.UtcNow,
                    Created      = DateTime.UtcNow
                };


                Purchase p = new Purchase
                {
                    SubmittedDate    = DateTime.UtcNow,
                    AspNetUserID     = User.Identity.IsAuthenticated ? db.AspNetUsers.First(x => x.UserName == User.Identity.Name).Id : null,
                    Created          = DateTime.UtcNow,
                    Modified         = DateTime.UtcNow,
                    OrderIdentifier  = Guid.NewGuid().ToString().Substring(0, 8),
                    PurchaseProducts = this.GetBasket(db).BasketProducts.Select(x => new Models.PurchaseProduct
                    {
                        ProductID    = x.ProductID,
                        Quantity     = x.Quantity,
                        Created      = DateTime.UtcNow,
                        Modified     = DateTime.UtcNow,
                        ProductName  = x.Product.Name,
                        ProductPrice = x.Product.Price,
                        Shipment     = s,
                    }).ToArray()
                };


                db.Purchases.Add(p);

                db.Baskets.Remove(this.GetBasket(db));
                db.SaveChanges();

                Braintree.TransactionRequest transaction = new Braintree.TransactionRequest();
                transaction.Amount              = this.GetBasket(db).BasketProducts.Sum(x => x.Quantity * (x.Product.Price ?? 0));
                transaction.CustomerId          = model.CustomerId;
                transaction.PaymentMethodToken  = model.CardToken;
                transaction.OrderId             = p.ID.ToString();
                transaction.PurchaseOrderNumber = p.OrderIdentifier;
                var transactionResult = await _braintreeGateway.Transaction.SaleAsync(transaction);

                await _emailService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage
                {
                    Subject     = string.Format("Your Coding Cookware Order {0}", p.OrderIdentifier),
                    Destination = model.Email,
                    Body        = CreateReceiptEmail(p)
                });

                if (!string.IsNullOrEmpty(model.Phone))
                {
                    await _smsService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage
                    {
                        Subject     = "",
                        Destination = model.Phone,
                        Body        = "You placed order " + p.OrderIdentifier
                    });
                }

                return(RedirectToAction("Index", "Receipt", new { id = p.OrderIdentifier }));
            }
            ;
            return(View());
        }