public ActionResult Compra(AcquistoViewModel viewModel) { AnnuncioModel model = null; using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { Guid token = getTokenDecodificato(viewModel.Token); model = new AnnuncioModel(token, db); if (model.TOKEN == null) { throw new System.Web.HttpException(404, ExceptionMessage.AdNotFound); } if (ModelState.IsValid) { if (!Utility.IsUtenteAttivo(1, TempData)) { ModelState.AddModelError("", ErrorResource.UserEnabled); } else { Models.Enumerators.VerificaAcquisto verifica = model.Acquisto(db, viewModel); if (verifica == Models.Enumerators.VerificaAcquisto.Ok) { if (model.CompletaAcquisto(db, viewModel)) { TempData["Esito"] = Language.JsonBuyAd; TempData["pagamentoEffettuato"] = true; transaction.Commit(); this.RefreshPunteggioUtente(db); this.SendNotifica(model.PERSONA, model.PERSONA1, TipoNotifica.AnnuncioAcquistato, ControllerContext, "annuncioAcquistato", model); this.SendNotifica(model.PERSONA1, model.PERSONA, TipoNotifica.AnnuncioVenduto, ControllerContext, "annuncioVenduto", model); return(RedirectToAction("Index", "Annuncio", new { token = viewModel.Token })); } // altrimenti pagamento fallito! } else if (verifica == Models.Enumerators.VerificaAcquisto.VerificaCartaCredito) { string actionPagamento = "Payment"; if (viewModel.TipoCarta != TipoCartaCredito.PayPal) { actionPagamento = "PaymentWithCreditCard"; } transaction.Commit(); Session["PayPalCompra"] = viewModel; Session["PayPalAnnuncio"] = model; return(RedirectToAction(actionPagamento, "PayPal", new { Id = model.ID, Token = viewModel.Token, Azione = AzionePayPal.Acquisto })); } else { Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(string.Format("Messaggio {0} per l'errore {1}", ErrorResource.AdBuyFailed, verifica.ToString()))); ModelState.AddModelError("", ErrorResource.AdBuyFailed); } } } //transaction.Rollback(); } catch (System.Web.HttpException eccezione) { //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); LoggatoreModel.Errore(eccezione); throw new System.Web.HttpException(404, eccezione.Message); } catch (Exception eccezione) { ModelState.AddModelError("", eccezione.Message); //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); LoggatoreModel.Errore(eccezione); } finally { if (db.Database.CurrentTransaction != null) { transaction.Rollback(); } } viewModel.Annuncio = model.GetViewModel(db); viewModel.Annuncio.Azione = "compra"; } } ViewData["acquistoViewModel"] = viewModel; return(View("Index", viewModel.Annuncio)); }
public ActionResult Index(string token, int pagina = 1) { AnnuncioModel model = null; using (DatabaseContext db = new DatabaseContext()) { using (System.Data.Entity.DbContextTransaction transaction = db.Database.BeginTransaction()) { try { int id = Utility.DecodeToInt(token); OFFERTA offerta = db.OFFERTA.SingleOrDefault(m => m.ID == id); if (offerta == null) { throw new HttpException(404, ExceptionMessage.AdNotFound); } model = new AnnuncioModel(offerta.ANNUNCIO); if (model.TOKEN == null) { throw new HttpException(404, ExceptionMessage.AdNotFound); } AcquistoViewModel viewModel = new AcquistoViewModel(offerta); if (ModelState.IsValid) { if (!Utility.IsUtenteAttivo(1, TempData)) { ModelState.AddModelError("", ErrorResource.UserEnabled); } else { Models.Enumerators.VerificaAcquisto verifica = model.Acquisto(db, viewModel, true); if (verifica == Models.Enumerators.VerificaAcquisto.Ok) { // PENSO CHE QUESTA ACTION SIA USATA SOLO PER OFFERTA E QUINDI CAMBIO CHIAMATA //if (model.CompletaAcquisto(db, viewModel)) if (model.CompletaAcquistoOfferta(db, offerta)) { TempData["Esito"] = Language.JsonBuyAd; TempData["pagamentoEffettuato"] = true; transaction.Commit(); this.RefreshPunteggioUtente(db); return(RedirectToAction("Index", "Annuncio", new { token = viewModel.Token })); } // altrimenti pagamento fallito! } else if (verifica == Models.Enumerators.VerificaAcquisto.VerificaCartaCredito) { string actionPagamento = "Payment"; if (viewModel.TipoCarta != TipoCartaCredito.PayPal) { actionPagamento = "PaymentWithCreditCard"; } transaction.Commit(); Session["PayPalCompra"] = viewModel; Session["PayPalAnnuncio"] = model; return(RedirectToAction(actionPagamento, "PayPal", new { Id = offerta.ID, Token = viewModel.Token, Azione = AzionePayPal.OffertaOK })); } else { Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(string.Format("Messaggio {0} per l'errore {1}", ErrorResource.AdBuyFailed, verifica.ToString()))); ModelState.AddModelError("", ErrorResource.AdBuyFailed); } } } //transaction.Rollback(); } catch (HttpException eccezione) { //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); LoggatoreModel.Errore(eccezione); throw new HttpException(404, eccezione.Message); } catch (Exception eccezione) { ModelState.AddModelError("", eccezione.Message); //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); LoggatoreModel.Errore(eccezione); } finally { if (db.Database.CurrentTransaction != null) { transaction.Rollback(); } } } int utente = ((PersonaModel)Session["utente"]).Persona.ID; return(View(GetListaOfferte(db, utente, pagina))); } }