Пример #1
0
        /// <summary>
        /// This is called in the special condition where a card is already added to Authorize.Net CIM but apparently not in the list of cards.
        /// This is not expected to happen usually, but could during testing or if we have to manually add cards.
        /// </summary>
        /// <returns></returns>
        public ActionResult ForceResync()
        {
            var transaction = new Transaction(IsolationLevel.ReadCommitted, "sync cards");

            try
            {
                CustomerGateway cg;
                var             customer = EnsureProfile(out cg);
                foreach (var cardProfile in customer.PaymentProfiles)
                {
                    var creditCard = new CreditCardEntity
                    {
                        AuthorizeId   = cardProfile.ProfileID,
                        FirstName     = cardProfile.BillingAddress.First,
                        LastName      = cardProfile.BillingAddress.Last,
                        AccountNumber = cardProfile.CardNumber.Replace("X", ""),
                        Address       = cardProfile.BillingAddress.Street
                    };
                    transaction.Add(creditCard);
                    creditCard.Save();

                    var userCard = new UserCreditCardEntity
                    {
                        UserId       = Membership.GetUser().GetUserEntity().UserId,
                        CreditCardId = creditCard.CreditCardId
                    };
                    transaction.Add(userCard);
                    userCard.Save();
                }

                transaction.Commit();
            }
            catch (Exception exc)
            {
                transaction.Rollback();
                ModelState.AddModelError("", Purchase.AddCard_Error);
                Log.Error(Purchase.SyncError, exc);
            }
            finally
            {
                transaction.Dispose();
            }
            return(new EmptyResult());
        }
Пример #2
0
        public ActionResult AddCard(AddCard model)
        {
            if (ModelState.IsValid)
            {
                var transaction = new Transaction(IsolationLevel.ReadCommitted, "add card");
                try
                {
                    CustomerGateway cg;
                    var             customer = EnsureProfile(out cg);

                    var addr = new AuthorizeNet.Address
                    {
                        First   = model.FirstName,
                        Last    = model.LastName,
                        Street  = model.AddressLine1 + Environment.NewLine + model.AddressLine2,
                        State   = model.State,
                        Country = model.Country,
                        City    = model.City,
                        Zip     = model.Zip
                    };

                    // save the customer profile for the currently logged on user
                    var creditCard = new CreditCardEntity()
                    {
                        FirstName     = model.FirstName,
                        LastName      = model.LastName,
                        AccountNumber = model.CardNumber.Substring(model.CardNumber.Length - 4, 4),
                        Address       = model.AddressLine1
                    };

                    creditCard.AuthorizeId = cg.AddCreditCard(
                        customer.ProfileID,
                        model.CardNumber,
                        model.CardMonth,
                        model.CardYear,
                        model.SecurityCode,
                        addr);
                    transaction.Add(creditCard);
                    creditCard.Save();

                    var userCard = new UserCreditCardEntity
                    {
                        UserId       = Membership.GetUser().GetUserEntity().UserId,
                        CreditCardId = creditCard.CreditCardId
                    };
                    transaction.Add(userCard);
                    userCard.Save();

                    transaction.Commit();

                    return(new EmptyResult());
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    // try to get all profiles from authorize.net
                    if (ex.Message.Contains("duplicate"))
                    {
                        ForceResync();
                    }
                    else
                    {
                        ModelState.AddModelError("", Purchase.AddCard_Error);
                    }
                    Log.Error(Purchase.AddCard_Error, ex);
                }
                finally
                {
                    transaction.Dispose();
                }
            }

            Response.StatusCode             = 417;
            Response.TrySkipIisCustomErrors = true;

            return(PartialView(model));
        }