private List <Transaction> GetListTransaction(AzionePayPal azione, string guid) { using (DatabaseContext db = new DatabaseContext()) { switch (azione) { case AzionePayPal.Acquisto: AcquistoViewModel acquisto = Session["PayPalCompra"] as AcquistoViewModel; //AnnuncioModel annuncio = Session["PayPalAnnuncio"] as AnnuncioModel; AnnuncioModel annuncio = new AnnuncioModel((Session["PayPalAnnuncio"] as AnnuncioModel).TOKEN, db); return(GetListTransactionFromAcquisto(acquisto, annuncio, guid)); case AzionePayPal.Offerta: //OffertaModel offerta = Session["PayPalOfferta"] as OffertaModel; int idOfferta = (Session["PayPalOfferta"] as OffertaModel).ID; var model = db.OFFERTA.SingleOrDefault(m => m.ID == idOfferta); OffertaModel offerta = new OffertaModel(model); return(GetListTransactionFromOfferta(offerta, guid)); case AzionePayPal.OffertaOK: AcquistoViewModel acquistoOfferta = Session["PayPalCompra"] as AcquistoViewModel; //AnnuncioModel annuncioOfferta = Session["PayPalAnnuncio"] as AnnuncioModel; AnnuncioModel annuncioOfferta = new AnnuncioModel((Session["PayPalAnnuncio"] as AnnuncioModel).TOKEN, db); return(GetListTransactionFromAcquisto(acquistoOfferta, annuncioOfferta, guid)); } } return(null); }
public ActionResult Index(string token, string azione = "compra") { try { Guid tokenDecriptato = getTokenDecodificato(token); using (DatabaseContext db = new DatabaseContext()) { db.Database.Connection.Open(); AnnuncioModel model = new AnnuncioModel(); AnnuncioViewModel viewModel = model.GetViewModel(db, tokenDecriptato); viewModel.Azione = azione; if (TempData["esito"] != null) { ViewBag.Esito = TempData["esito"]; } else if (TempData["errore"] != null) { ModelState.AddModelError("", TempData["errore"] as string); } return(View(viewModel)); } } catch (Exception ex) { //Elmah.ErrorSignal.FromCurrentContext().Raise(ex); LoggatoreModel.Errore(ex); } return(View()); }
private bool SaveOffertaCompleta(PayPalIndexViewModel paypal, Payment payment) { AcquistoViewModel viewModel = Session["PayPalCompra"] as AcquistoViewModel; AnnuncioModel model = Session["PayPalAnnuncio"] as AnnuncioModel; using (DatabaseContext db = new DatabaseContext()) { // AGGIUNGERE TRANSAZIONE NELLA TABELLA LOG_PAGAMENTO LOG_PAGAMENTO log = new LOG_PAGAMENTO(); ANNUNCIO annuncio = db.ANNUNCIO.SingleOrDefault(m => m.TOKEN.ToString() == viewModel.Token && m.SESSIONE_COMPRATORE == model.SESSIONE_COMPRATORE); log.ID_ANNUNCIO = annuncio.ID; log.ID_COMPRATORE = (Session["utente"] as PersonaModel).Persona.ID; log.SESSIONE_COMPRATORE = paypal.Guid; log.ID_PAGAMENTO = payment.id; //log.ID_PAGAMENTO = "TEST"; log.ISTITUTO_PAGAMENTO = "PAYPAL"; log.NOME_ANNUNCIO = "Pagamento spedizione per " + annuncio.NOME; log.DATA_INSERIMENTO = DateTime.Now; db.LOG_PAGAMENTO.Add(log); db.SaveChanges(); int idPayPal = SavePayPal(db, payment); using (DbContextTransaction transaction = db.Database.BeginTransaction()) { viewModel.PagamentoFatto = true; AnnuncioModel annuncioModel = new AnnuncioModel(annuncio); Models.Enumerators.VerificaAcquisto verifica = annuncioModel.Acquisto(db, viewModel, true); if (verifica == Models.Enumerators.VerificaAcquisto.Ok) { OFFERTA offerta = db.OFFERTA.SingleOrDefault(m => m.ID == paypal.Id); if (model.CompletaAcquistoOfferta(db, offerta, idPayPal)) { transaction.Commit(); this.RefreshPunteggioUtente(db); Models.ViewModels.Email.PagamentoOffertaViewModel pagamentoOfferta = new Models.ViewModels.Email.PagamentoOffertaViewModel(); pagamentoOfferta.NominativoDestinatario = offerta.ANNUNCIO.PERSONA.NOME + " " + offerta.ANNUNCIO.PERSONA.COGNOME; pagamentoOfferta.NomeAnnuncio = offerta.ANNUNCIO.NOME; pagamentoOfferta.Moneta = offerta.PUNTI; pagamentoOfferta.SoldiSpedizione = offerta.SOLDI; pagamentoOfferta.Baratti = offerta.OFFERTA_BARATTO.Select(m => m.ANNUNCIO.NOME).ToList(); this.SendNotifica((Session["utente"] as PersonaModel).Persona, offerta.ANNUNCIO.PERSONA, TipoNotifica.OffertaPagata, ControllerContext, "pagamentoOfferta", pagamentoOfferta); System.Web.Routing.RouteValueDictionary data = new System.Web.Routing.RouteValueDictionary(new { token = viewModel.Token }); _UrlFinePagamento = Url.Action("Index", "Annuncio", data, this.Request.Url.Scheme, this.Request.Url.Host); return(true); } // altrimenti acquisto fallito } } } return(false); }
private void Load(DatabaseContext db, PERSONA persona) { this.Nome = persona.NOME; this.Cognome = persona.COGNOME; this.Foto = persona.PERSONA_FOTO.Select(m => new FotoModel(m.ALLEGATO)).ToList(); this.listaAcquisti = new List <AnnuncioViewModel>(); this.listaVendite = new List <AnnuncioViewModel>(); this.listaDesideri = new List <AnnuncioViewModel>(); // far vedere top n acquisti con link var query = db.ANNUNCIO.Where(item => item.ID_COMPRATORE == persona.ID && item.TRANSAZIONE_ANNUNCIO.Count(m => m.STATO == (int)StatoPagamento.ATTIVO || m.STATO == (int)StatoPagamento.ACCETTATO) > 0 && (item.STATO == (int)StatoVendita.VENDUTO || item.STATO == (int)StatoVendita.BARATTATO) && (item.ID_OGGETTO != null || item.ID_SERVIZIO != null)); List <ANNUNCIO> lista = query .OrderByDescending(item => item.DATA_INSERIMENTO) .Take(4).ToList(); foreach (ANNUNCIO m in lista) { AnnuncioModel annuncioModel = new AnnuncioModel(); this.listaAcquisti.Add(annuncioModel.GetViewModel(db, m)); } // far vedere vendite recenti con link var queryVendite = db.ANNUNCIO.Where(item => item.ID_PERSONA == persona.ID && (item.STATO != (int)StatoVendita.ELIMINATO && item.STATO != (int)StatoVendita.BARATTATO && item.STATO != (int)StatoVendita.VENDUTO) && (item.ID_OGGETTO != null || item.ID_SERVIZIO != null)); List <ANNUNCIO> listaVendite = queryVendite .OrderByDescending(item => item.DATA_INSERIMENTO) .Take(4).ToList(); foreach (ANNUNCIO m in listaVendite) { AnnuncioModel annuncioModel = new AnnuncioModel(); this.listaVendite.Add(annuncioModel.GetViewModel(db, m)); } // far vedere top n desideri con link List <ANNUNCIO_DESIDERATO> listaDesideri = db.ANNUNCIO_DESIDERATO .Where(item => item.ID_PERSONA == persona.ID && (item.ANNUNCIO.STATO == (int)StatoVendita.INATTIVO || item.ANNUNCIO.STATO == (int)StatoVendita.ATTIVO) && (item.ANNUNCIO.DATA_FINE == null || item.ANNUNCIO.DATA_FINE >= DateTime.Now)) .OrderByDescending(item => item.ANNUNCIO.DATA_INSERIMENTO) .Take(4) .ToList(); listaDesideri.ForEach(m => this.listaDesideri.Add( new AnnuncioViewModel(db, m.ANNUNCIO) ) ); }
private bool CanPagamento(AzionePayPal azione) { switch (azione) { case AzionePayPal.Acquisto: using (DatabaseContext db = new DatabaseContext()) { AnnuncioModel annuncio = Session["PayPalAnnuncio"] as AnnuncioModel; ANNUNCIO model = db.ANNUNCIO.SingleOrDefault(m => m.ID == annuncio.ID && m.SESSIONE_COMPRATORE == annuncio.SESSIONE_COMPRATORE); if (model != null) { annuncio = new AnnuncioModel(model); return(true); } } break; case AzionePayPal.Offerta: using (DatabaseContext db = new DatabaseContext()) { OffertaModel offerta = Session["PayPalOfferta"] as OffertaModel; OFFERTA model = db.OFFERTA.SingleOrDefault(m => m.ID == offerta.ID && m.SESSIONE_COMPRATORE == offerta.SESSIONE_COMPRATORE); if (model != null) { //offerta = new OffertaModel(model); return(true); } } break; case AzionePayPal.OffertaOK: using (DatabaseContext db = new DatabaseContext()) { AnnuncioModel annuncio = Session["PayPalAnnuncio"] as AnnuncioModel; ANNUNCIO model = db.ANNUNCIO.SingleOrDefault(m => m.ID == annuncio.ID && m.SESSIONE_COMPRATORE == annuncio.SESSIONE_COMPRATORE); if (model != null) { annuncio = new AnnuncioModel(model); return(true); } } break; } //AnnullaPagamento(azione); return(false); }
public ActionResult Index(int pagina = 1) { List <AnnuncioViewModel> vendite = new List <AnnuncioViewModel>(); try { using (DatabaseContext db = new DatabaseContext()) { int utente = ((PersonaModel)Session["utente"]).Persona.ID; var query = db.ANNUNCIO.Where(item => item.ID_PERSONA == utente && (item.STATO != (int)StatoVendita.ELIMINATO && item.STATO != (int)StatoVendita.BARATTATO && item.STATO != (int)StatoVendita.VENDUTO) && (item.ID_OGGETTO != null || item.ID_SERVIZIO != null)); int numeroElementi = Convert.ToInt32(WebConfigurationManager.AppSettings["numeroElementi"]); ViewData["TotalePagine"] = (int)Math.Ceiling((decimal)query.Count() / (decimal)numeroElementi); ViewData["Pagina"] = pagina; pagina -= 1; List <ANNUNCIO> listaAnnunci = query .OrderByDescending(item => item.DATA_INSERIMENTO) .Skip(pagina * numeroElementi) .Take(numeroElementi) .ToList(); foreach (ANNUNCIO annuncio in listaAnnunci) { AnnuncioModel annuncioModel = new AnnuncioModel(); // escludo il singolo annuncio in caso di errore try { vendite.Add(annuncioModel.GetViewModel(db, annuncio)); } catch (Exception eccezione) { Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); } } if (vendite.Count > 0) { RefreshPunteggioUtente(db); } } } catch (Exception ex) { //Elmah.ErrorSignal.FromCurrentContext().Raise(ex); LoggatoreModel.Errore(ex); } return(View(vendite)); }
public PayPalIndexViewModel(AcquistoViewModel viewModel, AnnuncioModel annuncio) { Token = viewModel.Token; if (viewModel.TipoScambio == TipoScambio.Spedizione && viewModel.TipoCarta != TipoCartaCredito.PayPal) { MetodoPagamento carta = new MetodoPagamento(); carta.TipoCarta = viewModel.TipoCarta.ToString(); carta.Numero = viewModel.NumeroCarta; carta.Cvv2 = ((int)viewModel.Cvv2).ToString(); carta.Nome = viewModel.NomeTitolareCarta; carta.Cognome = viewModel.CognomeTitolareCarta; carta.MeseScadenza = (int)viewModel.MeseScadenzaCarta; carta.AnnoScadenza = (int)viewModel.AnnoScadenzaCarta; } }
public ActionResult Conclusi(int pagina = 1) { List <AnnuncioViewModel> acquistiConclusi = new List <AnnuncioViewModel>(); try { using (DatabaseContext db = new DatabaseContext()) { int utente = ((PersonaModel)Session["utente"]).Persona.ID; var query = db.ANNUNCIO.Where(item => item.ID_PERSONA != utente && item.ID_COMPRATORE == utente && (item.STATO == (int)StatoVendita.VENDUTO || item.STATO == (int)StatoVendita.ELIMINATO || item.STATO == (int)StatoVendita.BARATTATO) && (item.ID_OGGETTO != null || item.ID_SERVIZIO != null)); int numeroElementi = Convert.ToInt32(WebConfigurationManager.AppSettings["numeroElementi"]); ViewData["TotalePagine"] = (int)Math.Ceiling((decimal)query.Count() / (decimal)numeroElementi); ViewData["Pagina"] = pagina; pagina -= 1; string randomString = Utility.RandomString(3); List <ANNUNCIO> lista = query .OrderByDescending(item => item.DATA_VENDITA) .Skip(pagina * numeroElementi) .Take(numeroElementi).ToList(); foreach (ANNUNCIO m in lista) { AnnuncioModel annuncioModel = new AnnuncioModel(); AnnuncioViewModel viewModel = annuncioModel.GetViewModel(db, m); OFFERTA offerta = m.OFFERTA.SingleOrDefault(o => o.STATO == (int)StatoOfferta.ACCETTATA); if (offerta != null) { viewModel.Offerta = new OffertaViewModel(db, offerta); } acquistiConclusi.Add(viewModel); } if (acquistiConclusi.Count > 0) { RefreshPunteggioUtente(db); } } } catch (Exception eccezione) { //Elmah.ErrorSignal.FromCurrentContext().Raise(ex); LoggatoreModel.Errore(eccezione); } return(View(acquistiConclusi)); }
private bool SaveAcquisto(PayPalIndexViewModel paypal, Payment payment) { AcquistoViewModel viewModel = Session["PayPalCompra"] as AcquistoViewModel; AnnuncioModel model = Session["PayPalAnnuncio"] as AnnuncioModel; using (DatabaseContext db = new DatabaseContext()) { // AGGIUNGERE TRANSAZIONE NELLA TABELLA LOG_PAGAMENTO LOG_PAGAMENTO log = new LOG_PAGAMENTO(); ANNUNCIO annuncio = db.ANNUNCIO.SingleOrDefault(m => m.TOKEN.ToString() == viewModel.Token && m.SESSIONE_COMPRATORE == model.SESSIONE_COMPRATORE); log.ID_ANNUNCIO = annuncio.ID; log.ID_COMPRATORE = (Session["utente"] as PersonaModel).Persona.ID; log.SESSIONE_COMPRATORE = paypal.Guid; log.ID_PAGAMENTO = payment.id; //log.ID_PAGAMENTO = "TEST"; log.ISTITUTO_PAGAMENTO = "PAYPAL"; log.NOME_ANNUNCIO = annuncio.NOME; log.DATA_INSERIMENTO = DateTime.Now; db.LOG_PAGAMENTO.Add(log); db.SaveChanges(); int idPaypal = SavePayPal(db, payment); using (DbContextTransaction transaction = db.Database.BeginTransaction()) { viewModel.PagamentoFatto = true; AnnuncioModel annuncioModel = new AnnuncioModel(annuncio); Models.Enumerators.VerificaAcquisto verifica = annuncioModel.Acquisto(db, viewModel); if (verifica == Models.Enumerators.VerificaAcquisto.Ok) { if (model.CompletaAcquisto(db, viewModel, idPaypal)) { transaction.Commit(); this.RefreshPunteggioUtente(db); this.SendNotifica(annuncioModel.PERSONA, annuncioModel.PERSONA1, TipoNotifica.AnnuncioAcquistato, ControllerContext, "annuncioAcquistato", annuncioModel); this.SendNotifica(annuncioModel.PERSONA1, annuncioModel.PERSONA, TipoNotifica.AnnuncioVenduto, ControllerContext, "annuncioVenduto", annuncioModel); System.Web.Routing.RouteValueDictionary data = new System.Web.Routing.RouteValueDictionary(new { token = viewModel.Token }); _UrlFinePagamento = Url.Action("Index", "Annuncio", data, this.Request.Url.Scheme, this.Request.Url.Host); return(true); } // altrimenti acquisto fallito } } } return(false); }
private bool annullaVenditaSuDatabase(DatabaseContext db, string token) { Guid tokenDecriptato = getTokenDecodificato(token); AnnuncioModel model = new AnnuncioModel(tokenDecriptato, db); return(model.Elimina(db)); //int idUtente = (Session["utente"] as PersonaModel).Persona.ID; //ANNUNCIO model = db.ANNUNCIO.Where(v => v.TOKEN == tokenDecriptato && v.ID_PERSONA == idUtente && v.STATO != (int)StatoVendita.BARATTATO // && v.STATO != (int)StatoVendita.ELIMINATO && v.STATO != (int)StatoVendita.VENDUTO).SingleOrDefault(); //if (model != null) //{ // model.STATO = (int)StatoVendita.ELIMINATO; // model.DATA_MODIFICA = DateTime.Now; // if (db.SaveChanges() > 0) // { // OffertaModel.AnnullaOfferteEffettuate(db, model.ID); // OffertaModel.AnnullaOfferteRicevute(db, model.ID); // return true; // } //} //return false; }
private List <Transaction> GetListTransactionFromAcquisto(AcquistoViewModel viewModel, AnnuncioModel annuncio, string guid) { List <Transaction> lista = new List <Transaction>(); //similar to credit card create itemlist and add item objects to it var itemList = new ItemList() { items = new List <Item>() }; TIPO_VALUTA tipoValuta = (HttpContext.Application["tipoValuta"] as List <TIPO_VALUTA>).SingleOrDefault(m => m.SIMBOLO == NumberFormatInfo.CurrentInfo.CurrencySymbol); if (annuncio.TIPO_PAGAMENTO != (int)TipoPagamento.HAPPY) { if (annuncio.SOLDI != null && annuncio.SOLDI > 0) { itemList.items.Add(new Item() { name = annuncio.NOME, currency = tipoValuta.CODICE, price = ConvertDecimalToString(annuncio.SOLDI), quantity = "1", sku = "sku" }); //decimal percentualeAnnuncio = Convert.ToDecimal(System.Configuration.ConfigurationManager.AppSettings["annuncioMonetaRealePercentuale"]); //decimal commissioneAnnuncio = (((decimal)annuncio.SOLDI / 100) * percentualeAnnuncio); decimal commissioneAnnuncio = (((decimal)annuncio.SOLDI / 100) * annuncio.COMMISSIONE.PERCENTUALE); itemList.items.Add(new Item() { name = App_GlobalResources.Language.PayPalAd, currency = tipoValuta.CODICE, price = ConvertDecimalToString(commissioneAnnuncio), quantity = "1", sku = "sku" }); } } // se presente spedizione ANNUNCIO_TIPO_SCAMBIO tipoScambio = annuncio.ANNUNCIO_TIPO_SCAMBIO.FirstOrDefault(m => m.TIPO_SCAMBIO == (int)TipoScambio.Spedizione); if (annuncio.ID_OGGETTO != null && tipoScambio != null && viewModel.TipoScambio != TipoScambio.AMano) { ANNUNCIO_TIPO_SCAMBIO_SPEDIZIONE spedizione = tipoScambio.ANNUNCIO_TIPO_SCAMBIO_SPEDIZIONE.FirstOrDefault(); if (spedizione != null) { itemList.items.Add(new Item() { name = string.Concat(App_GlobalResources.Language.Shipment, " ", annuncio.NOME), currency = tipoValuta.CODICE, price = ConvertDecimalToString(spedizione.SOLDI), //price = ConvertDecimalToString(new Decimal(1006.5)), // prova fissa verifica conversione quantity = "1", sku = "sku" }); //decimal percentualeSpedizione = Convert.ToDecimal(System.Configuration.ConfigurationManager.AppSettings["spedizionePercentuale"]); //decimal commissioneSpedizione = (((decimal)spedizione.SOLDI / 100) * percentualeSpedizione); decimal commissioneSpedizione = (((decimal)spedizione.SOLDI / 100) * spedizione.COMMISSIONE.PERCENTUALE); itemList.items.Add(new Item() { name = App_GlobalResources.Language.PayPalShipment, currency = tipoValuta.CODICE, price = ConvertDecimalToString(commissioneSpedizione), quantity = "1", sku = "sku" }); } } decimal subtotal = itemList.items.Sum(m => ConvertStringToDecimal(m.price)); // similar as we did for credit card, do here and create details object var details = new Details() { tax = "0", shipping = "0", subtotal = ConvertDecimalToString(subtotal) }; // similar as we did for credit card, do here and create amount object var amount = new Amount() { currency = tipoValuta.CODICE, total = ConvertDecimalToString(subtotal), // Total must be equal to sum of shipping, tax and subtotal. details = details }; Transaction transazione = new Transaction(); transazione.description = annuncio.NOTE_AGGIUNTIVE; transazione.invoice_number = guid + "|&%" + annuncio.TOKEN.ToString(); transazione.amount = amount; transazione.item_list = itemList; lista.Add(transazione); return(lista); }
public OffertaModel(OFFERTA model) { OffertaOriginale = model; AnnuncioModel = new AnnuncioModel(model.ANNUNCIO); CopyAttributes <OFFERTA>(model); }
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 InviaOfferta(OffertaViewModel viewModel) { AnnuncioModel model = new AnnuncioModel(); string messaggio = ErrorResource.BidAd; if (ModelState.IsValid) { if (!Utility.IsUtenteAttivo(1, TempData)) { Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(ErrorResource.UserEnabled)); } using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { if (viewModel.Save(db, ref messaggio)) { // aggiungere nella lista dei desideri string tokenDecodificato = HttpContext.Server.UrlDecode(viewModel.Annuncio.Token); Guid tokenGuid = Guid.Parse(tokenDecodificato); PersonaModel utente = (PersonaModel)HttpContext.Session["utente"]; AnnuncioViewModel annuncio = null; addDesiderio(db, tokenGuid, utente.Persona.ID, ref annuncio); // salvare transazione transaction.Commit(); this.RefreshPunteggioUtente(db); // invia e-mail al venditore if (annuncio.Venditore.Persona != null) { this.SendNotifica(utente.Persona, annuncio.Venditore.Persona, TipoNotifica.OffertaRicevuta, ControllerContext, "offerta", viewModel); } return(Json(new { Messaggio = Language.JsonSendBid })); } transaction.Rollback(); } catch (Exception eccezione) { //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); transaction.Rollback(); LoggatoreModel.Errore(eccezione); Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(eccezione.Message)); } } } } else { var errori = ModelState.Where(m => m.Value.Errors.Count > 0) .Select(m => m.Value.Errors.Select(n => n.ErrorMessage)).SelectMany(m => m).ToList(); if (errori != null && errori.Count > 0) { messaggio = string.Join("<br />", errori); } } // acquisto generico Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(messaggio)); }
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))); } }