Exemple #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());
        }
Exemple #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));
        }
Exemple #3
0
        public ActionResult Checkout(NewPurchaseModel model, [ModelBinder(typeof(DataTablesRequestModelBinder))] DataTablesRequestModel dtRequestModel)
        {
            var user = Membership.GetUser().GetUserEntity();

            if (Session["cart"] as List <Models.Purchase> == null)
            {
                Session["cart"] = new List <Models.Purchase>();
            }

            model.Cart = ((List <Models.Purchase>)Session["cart"]);

            if (Request.HttpMethod == "POST")
            {
                if (ModelState.IsValid)
                {
                    if (model.Cart.Count > 0)
                    {
                        var transaction = new Transaction(IsolationLevel.ReadCommitted, "purchase transfer");
                        try
                        {
                            // authorize and capture purchase
                            CustomerGateway cg;
                            var             customer = EnsureProfile(out cg);

                            var order = new Order(customer.ProfileID, model.CreditCard.AuthorizeId, "")
                            {
                                Amount        = model.Cart.Sum(x => x.Price),
                                Description   = model.PurchaseNotes,
                                InvoiceNumber =
                                    DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture)
                            };

                            var response = (GatewayResponse)cg.AuthorizeAndCapture(order);
                            if (!response.Approved)
                            {
                                throw new Exception(response.Message);
                            }

                            // set up all the transactions
                            foreach (var purchase in model.Cart)
                            {
                                var toDevice    = purchase.Device;
                                var newPurchase = new PurchaseHistoryEntity
                                {
                                    DeviceId       = purchase.DeviceId,
                                    LocationId     = toDevice.LocationId,
                                    UserId         = user.UserId,
                                    PurchaseTime   = DateTime.UtcNow,
                                    ScansPurchased = purchase.Quantity,
                                    AmountPaid     = purchase.Price,
                                    PurchaseNotes  = model.PurchaseNotes,
                                    TransactionId  = response.TransactionID
                                };
                                transaction.Add(newPurchase);
                                newPurchase.Save();

                                toDevice.ScansAvailable += purchase.Quantity;
                                transaction.Add(toDevice);
                                toDevice.Save();
                            }

                            transaction.Commit();

                            model.Cart.Clear();

                            OperationController.Update();

                            return(RedirectToAction("List"));
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            ModelState.AddModelError("", Purchase.CheckoutError);
                            Log.Error(Purchase.CheckoutError, ex);
                        }
                        finally
                        {
                            transaction.Dispose();
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", Purchase.NoItems);
                }

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

            model.Cards = user.UserCreditCards.AsQueryable();

            var result = View(model);

            if (dtRequestModel == null)
            {
                return(result);
            }

            return(Query(result, dtRequestModel));
        }
Exemple #4
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));
        }
Exemple #5
0
        public ActionResult List(int?locationId, int?organizationId, PurchaseHistoryModel model, [ModelBinder(typeof(DataTablesRequestModelBinder))] DataTablesRequestModel dtRequestModel)
        {
            if (Request.HttpMethod == "POST" && ModelState.IsValid)
            {
                var user = Membership.GetUser().GetUserEntity();

                var transaction = new Transaction(IsolationLevel.ReadCommitted, "purchase transfer");
                try
                {
                    var fromDevice = model.FromDevice;
                    var toDevice   = model.ToDevice;

                    var from = new PurchaseHistoryEntity
                    {
                        DeviceId       = model.FromDeviceId,
                        LocationId     = fromDevice.LocationId,
                        UserId         = user.UserId,
                        PurchaseTime   = DateTime.UtcNow,
                        ScansPurchased = -model.Quantity,
                        AmountPaid     = 0,
                        TransactionId  = string.Empty,
                        PurchaseNotes  = String.Format(Purchase.TransferFrom, SharedRes.Formats.Device.FormatWith(fromDevice), SharedRes.Formats.Device.FormatWith(toDevice))
                    };
                    transaction.Add(from);
                    from.Save();

                    var to = new PurchaseHistoryEntity
                    {
                        DeviceId       = model.ToDeviceId,
                        LocationId     = toDevice.LocationId,
                        UserId         = user.UserId,
                        PurchaseTime   = DateTime.UtcNow,
                        ScansPurchased = model.Quantity,
                        AmountPaid     = 0,
                        TransactionId  = string.Empty,
                        PurchaseNotes  = String.Format(Purchase.TransferFrom, SharedRes.Formats.Device.FormatWith(fromDevice), SharedRes.Formats.Device.FormatWith(toDevice))
                    };
                    transaction.Add(to);
                    to.Save();

                    transaction.Add(fromDevice);
                    fromDevice.ScansAvailable -= model.Quantity;
                    fromDevice.Save();

                    transaction.Add(toDevice);
                    toDevice.ScansAvailable += model.Quantity;
                    toDevice.Save();

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

            if (!organizationId.HasValue)
            {
                if (!locationId.HasValue)
                {
                    model.Puchases = new LinqMetaData().PurchaseHistory.WithPermissions();
                }
                else
                {
                    var location = new LocationEntity(locationId.Value);
                    if (location.IsNew)
                    {
                        throw new HttpException(404, SharedRes.Error.NotFound_Location);
                    }

                    if (!Permissions.UserHasPermission("View", location))
                    {
                        throw new HttpException(401, SharedRes.Error.Unauthorized_Location);
                    }

                    model.Puchases = new LinqMetaData().PurchaseHistory.Where(x => x.LocationId == locationId.Value);
                }
            }
            else
            {
                var organization = new OrganizationEntity(organizationId.Value);
                if (organization.IsNew)
                {
                    throw new HttpException(404, SharedRes.Error.NotFound_Organization);
                }

                if (!locationId.HasValue)
                {
                    if (!Permissions.UserHasPermission("View", organization))
                    {
                        throw new HttpException(401, SharedRes.Error.Unauthorized_Organization);
                    }

                    model.Puchases =
                        new LinqMetaData().PurchaseHistory.Where(x => x.Location.OrganizationId == organizationId);
                }
                else
                {
                    // do the same thing as above but check if the location is assigned to the organization
                    var location = new LocationEntity(locationId.Value);
                    if (location.IsNew && location.OrganizationId == organizationId)
                    {
                        throw new HttpException(404, SharedRes.Error.NotFound_Location);
                    }

                    if (!Permissions.UserHasPermission("View", location))
                    {
                        throw new HttpException(401, SharedRes.Error.Unauthorized_Location);
                    }

                    model.Puchases = new LinqMetaData().PurchaseHistory.Where(x => x.LocationId == locationId.Value);
                }
            }

            var result = View(model);

            if (dtRequestModel == null)
            {
                return(result);
            }

            return(Query(result, dtRequestModel));
        }