Exemplo n.º 1
0
        public void GetCustomerTest()
        {
            //check login / password
            string sError = CheckLoginPassword();

            Assert.IsTrue(sError == "", sError);

            string responseString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><getCustomerProfileResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\"><messages><resultCode>Ok</resultCode><message><code>I00001</code><text>Successful.</text></message></messages><profile><merchantCustomerId /><description>UpdateCustomerTest Success</description><email>[email protected]</email><customerProfileId>24231938</customerProfileId><paymentProfiles><customerType>individual</customerType><customerPaymentProfileId>22219473</customerPaymentProfileId><payment><creditCard><cardNumber>XXXX1111</cardNumber><expirationDate>XXXX</expirationDate></creditCard></payment></paymentProfiles></profile></getCustomerProfileResponse>";

            LocalRequestObject.ResponseString = responseString;

            CustomerGateway target = new CustomerGateway(ApiLogin, TransactionKey);

            string profileID = "24231938";

            Customer expected = new Customer()
            {
                ProfileID       = "24231938",
                Email           = "*****@*****.**",
                Description     = "UpdateCustomerTest Success",
                PaymentProfiles =
                {
                    new PaymentProfile(new customerPaymentProfileMaskedType())
                    {
                        CardExpiration = "XXXX",
                        CardNumber     = "XXXX1111",
                        ProfileID      = "22219473"
                    }
                }
            };

            Customer actual = null;

            // if choose "USELOCAL", the test should pass with no exception
            // Otherwise, the test might fail for error, i.e. duplicated request.
            try
            {
                actual = target.GetCustomer(profileID);
            }
            catch (Exception e)
            {
                string s = e.Message;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual(expected.ProfileID, actual.ProfileID);
            Assert.AreEqual(expected.Email, actual.Email);
            Assert.AreEqual(expected.Description, actual.Description);
            Assert.AreEqual(expected.PaymentProfiles.Count, actual.PaymentProfiles.Count);
            Assert.AreEqual(expected.PaymentProfiles[0].CardExpiration, actual.PaymentProfiles[0].CardExpiration);
            Assert.AreEqual(expected.PaymentProfiles[0].CardNumber, actual.PaymentProfiles[0].CardNumber);
            Assert.AreEqual(expected.PaymentProfiles[0].ProfileID, actual.PaymentProfiles[0].ProfileID);
        }
Exemplo n.º 2
0
        public void StoreInVault(int peopleId, string type, string cardNumber, string expires, string cardCode,
                                 string routing, string account, bool giving)
        {
            var person      = db.LoadPersonById(peopleId);
            var paymentInfo = person.PaymentInfo();

            if (paymentInfo == null)
            {
                paymentInfo = new PaymentInfo();
                person.PaymentInfos.Add(paymentInfo);
            }

            var billToAddress = new AuthorizeNet.Address
            {
                First   = paymentInfo.FirstName ?? person.FirstName,
                Last    = paymentInfo.LastName ?? person.LastName,
                Street  = paymentInfo.Address ?? person.PrimaryAddress,
                City    = paymentInfo.City ?? person.PrimaryCity,
                State   = paymentInfo.State ?? person.PrimaryState,
                Country = paymentInfo.Country ?? person.PrimaryCountry,
                Zip     = paymentInfo.Zip ?? person.PrimaryZip,
                Phone   = paymentInfo.Phone ?? person.HomePhone ?? person.CellPhone
            };

            Customer customer;

            if (paymentInfo.AuNetCustId == null) // create a new profilein Authorize.NET CIM
            {
                // NOTE: this can throw an error if the email address already exists...
                // TODO: Authorize.net needs to release a new Nuget package, because they don't have a clean way to pass in customer ID (aka PeopleId) yet... the latest code has a parameter for this, though
                //       - we could call UpdateCustomer after the fact to do this if we wanted to
                customer    = CustomerGateway.CreateCustomer(person.EmailAddress, person.Name);
                customer.ID = peopleId.ToString();

                paymentInfo.AuNetCustId = customer.ProfileID.ToInt();
            }
            else
            {
                customer = CustomerGateway.GetCustomer(paymentInfo.AuNetCustId.ToString());
            }

            customer.BillingAddress = billToAddress;
            var isSaved = CustomerGateway.UpdateCustomer(customer);

            if (!isSaved)
            {
                throw new Exception($"UpdateCustomer failed to save for {peopleId}");
            }

            if (type == PaymentType.Ach)
            {
                SaveECheckToProfile(routing, account, paymentInfo, customer);

                paymentInfo.MaskedAccount = Util.MaskAccount(account);
                paymentInfo.Routing       = Util.Mask(new StringBuilder(routing), 2);
            }
            else if (type == PaymentType.CreditCard)
            {
                var normalizeExpires = DbUtil.NormalizeExpires(expires);
                if (normalizeExpires == null)
                {
                    throw new ArgumentException($"Can't normalize date {expires}", nameof(expires));
                }

                var expiredDate = normalizeExpires.Value;

                SaveCreditCardToProfile(cardNumber, cardCode, expiredDate, paymentInfo, customer);

                paymentInfo.MaskedCard = Util.MaskCC(cardNumber);
                paymentInfo.Expires    = expires;
            }
            else
            {
                throw new ArgumentException($"Type {type} not supported", nameof(type));
            }

            if (giving)
            {
                paymentInfo.PreferredGivingType = type;
            }
            else
            {
                paymentInfo.PreferredPaymentType = type;
            }

            db.SubmitChanges();
        }
Exemplo n.º 3
0
        public void GetCustomerTest_eCheck()
        {
            //check login / password
            string sError = CheckLoginPassword();

            Assert.IsTrue(sError == "", sError);

            string responseString1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><getCustomerProfileResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\"><messages><resultCode>Ok</resultCode><message><code>I00001</code><text>Successful.</text></message></messages><profile><description>UpdateCustomerTest Success</description><email>[email protected]</email><customerProfileId>24236276</customerProfileId><paymentProfiles><customerPaymentProfileId>24287458</customerPaymentProfileId><payment><bankAccount><accountType>checking</accountType><routingNumber>XXXX0024</routingNumber><accountNumber>XXXX3456</accountNumber><nameOnAccount>Sue Zhu</nameOnAccount></bankAccount></payment></paymentProfiles></profile></getCustomerProfileResponse>";
            string responseString  = responseString1;

            LocalRequestObject.ResponseString = responseString;

            CustomerGateway target = new CustomerGateway(ApiLogin, TransactionKey);

            string profileID = "24236276";

            Customer expected = new Customer()
            {
                ProfileID       = "24236276",
                Email           = "*****@*****.**",
                Description     = "UpdateCustomerTest Success",
                PaymentProfiles =
                {
                    new PaymentProfile(new customerPaymentProfileMaskedType())
                    {
                        eCheckBankAccount = new BankAccount()
                        {
                            accountTypeSpecified = true,
                            accountType          = BankAccountType.Checking,
                            routingNumber        = "XXXX0024",
                            accountNumber        = "XXXX3456",
                            nameOnAccount        = "Sue Zhu",
                            bankName             = "Bank of Seattle",
                            echeckType           = EcheckType.WEB,
                            echeckTypeSpecified  = true
                        }
                    }
                }
            };

            Customer actual = null;

            // if choose "USELOCAL", the test should pass with no exception
            // Otherwise, the test might fail for error, i.e. duplicated request.
            try
            {
                actual = target.GetCustomer(profileID);
            }
            catch (Exception e)
            {
                string s = e.Message;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual(expected.ProfileID, actual.ProfileID);
            Assert.AreEqual(expected.Email, actual.Email);
            Assert.AreEqual(expected.Description, actual.Description);
            Assert.AreEqual(expected.PaymentProfiles.Count, actual.PaymentProfiles.Count);
            Assert.AreEqual(expected.PaymentProfiles[0].eCheckBankAccount.accountNumber, actual.PaymentProfiles[0].eCheckBankAccount.accountNumber);
            Assert.AreEqual(expected.PaymentProfiles[0].eCheckBankAccount.routingNumber, actual.PaymentProfiles[0].eCheckBankAccount.routingNumber);
            Assert.AreEqual(expected.PaymentProfiles[0].eCheckBankAccount.accountNumber, actual.PaymentProfiles[0].eCheckBankAccount.accountNumber);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This ensures that the user profile has been created on Authorize.Net CIM.
        /// This is only called when Adding or Editing a card or finalizing a purchase.  All other viewing using the card cache table.
        /// </summary>
        /// <param name="outGateway">The customer gateway created and used to verify the customer has been created.
        /// Just returned for efficiency, there is no consequence to creating another gateway in the calling function</param>
        /// <param name="user">The current user.</param>
        /// <returns>All the customer information from CIM.</returns>
        private Customer EnsureProfile(out CustomerGateway outGateway, UserEntity user = null)
        {
            if (user == null)
            {
                user = Membership.GetUser().GetUserEntity();
            }

            // create the authorize.net gateway
            bool live;

            bool.TryParse(ConfigurationManager.AppSettings.Get("AuthorizeLive"), out live);
            var cg = new CustomerGateway(ConfigurationManager.AppSettings.Get("API_LOGIN_ID"),
                                         ConfigurationManager.AppSettings.Get("TRANSACTION_KEY"), live ? ServiceMode.Live : ServiceMode.Test);

            outGateway = cg;

            // get the users e-mail address
            var email   = user.EmailAddress;
            var setting = user.Settings.FirstOrDefault(x => x.Name == "SupportUser");

            if (setting != null)
            {
                email = setting.Value + "@" + SupportController.DOMAIN;
            }

            // create a new customer profile for the currently logged in user
            var profile = new UserSettingEntity(user.UserId, "AuthorizeId");

            if (profile.IsNew)
            {
                try
                {
                    // set up new user
                    profile.UserId = user.UserId;
                    profile.Name   = "AuthorizeId";
                    var customer = cg.CreateCustomer(email, user.Username);
                    profile.Value = customer.ProfileID;
                    profile.Save();

                    return(customer);
                }
                catch (Exception ex)
                {
                    // user exists, so just get the ID
                    if (ex.Message.Contains("duplicate"))
                    {
                        profile.UserId = user.UserId;
                        profile.Name   = "AuthorizeId";
                        var start = ex.Message.IndexOf("ID", StringComparison.InvariantCulture) + 3;
                        profile.Value = ex.Message.Substring(start,
                                                             ex.Message.IndexOf(" ", start,
                                                                                StringComparison.InvariantCulture) -
                                                             start);
                        profile.Save();
                    }
                    Log.Error(Purchase.AuthError, ex);
                }
            }

            var existingCustomer = cg.GetCustomer(profile.Value);

            if (existingCustomer.Email != email)
            {
                existingCustomer.Email = email;
                cg.UpdateCustomer(existingCustomer);
            }


            return(existingCustomer);
        }