Ejemplo n.º 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());
        }
Ejemplo n.º 2
0
        public ActionResult EditCard(int creditcardid, EditCard model)
        {
            var card = new CreditCardEntity(creditcardid);

            if (card.IsNew)
            {
                throw new HttpException(404, SharedRes.Error.NotFound_CreditCard);
            }

            if (!Permissions.UserHasPermission("Edit", card))
            {
                throw new HttpException(401, SharedRes.Error.Unauthorized_CreditCard);
            }

            if (ModelState.IsValid)
            {
                var transaction = new Transaction(IsolationLevel.ReadCommitted, "add card");
                try
                {
                    CustomerGateway cg;
                    var             customer = RoleUtils.IsUserServiceAdmin()
                                       ? EnsureProfile(out cg, card.UserCreditCards.First().User)
                                       : EnsureProfile(out cg);

                    var profile = customer.PaymentProfiles.First(x => x.ProfileID == card.AuthorizeId);

                    // update the card info
                    if (!string.IsNullOrEmpty(model.CardNumber))
                    {
                        profile.CardNumber     = model.CardNumber;
                        profile.CardCode       = model.SecurityCode;
                        profile.CardExpiration = model.CardMonth + "/" + model.CardYear;
                        card.AccountNumber     = model.CardNumber.Substring(model.CardNumber.Length - 4, 4);
                    }

                    // update the billing address
                    profile.BillingAddress = 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
                    };
                    card.FirstName = model.FirstName;
                    card.LastName  = model.LastName;
                    card.Address   = model.AddressLine1;
                    transaction.Add(card);
                    card.Save();

                    cg.UpdatePaymentProfile(customer.ProfileID, profile);

                    transaction.Commit();
                    return(new EmptyResult());
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    ModelState.AddModelError("", Purchase.EditCard_Error);
                    Log.Error(Purchase.EditCard_Error, ex);
                }
                finally
                {
                    transaction.Dispose();
                }
            }

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

            return(PartialView(model));
        }
Ejemplo n.º 3
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));
        }