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 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 SaveOfferta(Payment payment)
        {
            OffertaModel offerta = Session["PayPalOfferta"] as OffertaModel;

            using (DatabaseContext db = new DatabaseContext())
            {
                db.Database.Connection.Open();
                // AGGIUNGERE TRANSAZIONE NELLA TABELLA DEI LOG
                LOG_PAGAMENTO log   = new LOG_PAGAMENTO();
                OFFERTA       model = db.OFFERTA.Include(m => m.PERSONA)
                                      .Include(m => m.ANNUNCIO.ANNUNCIO_TIPO_SCAMBIO)
                                      .SingleOrDefault(m => m.ID == offerta.ID);
                log.ID_ANNUNCIO        = model.ID_ANNUNCIO;
                log.ID_COMPRATORE      = model.ID_PERSONA;
                log.ID_PAGAMENTO       = model.ID.ToString();
                log.ISTITUTO_PAGAMENTO = "PAYPAL";
                log.NOME_ANNUNCIO      = "Offerta per l'annuncio " + model.ANNUNCIO.NOME;
                log.DATA_INSERIMENTO   = DateTime.Now;
                db.LOG_PAGAMENTO.Add(log);
                db.SaveChanges();
                // cambia stato spedizione in pagata
                OFFERTA_SPEDIZIONE spedizione = model.OFFERTA_SPEDIZIONE.FirstOrDefault();
                spedizione.DATA_MODIFICA = DateTime.Now;
                spedizione.STATO         = (int)StatoSpedizione.PAGATA;
                db.OFFERTA_SPEDIZIONE.Attach(spedizione);
                db.Entry(spedizione).State = EntityState.Modified;
                db.SaveChanges();
                int idPayPal = SavePayPal(db, payment);
                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var    utente    = (Session["utente"] as PersonaModel);
                        string messaggio = string.Empty;
                        // ripopolo classe offerta per calcoli nella funzione ACCETTA
                        offerta.OffertaOriginale = model;
                        offerta.ANNUNCIO.ANNUNCIO_TIPO_SCAMBIO = db.ANNUNCIO_TIPO_SCAMBIO
                                                                 .Include(m => m.ANNUNCIO_TIPO_SCAMBIO_SPEDIZIONE)
                                                                 .Where(m => m.ID_ANNUNCIO == offerta.ID_ANNUNCIO).ToList();
                        offerta.AnnuncioModel.ANNUNCIO_TIPO_SCAMBIO = offerta.ANNUNCIO.ANNUNCIO_TIPO_SCAMBIO;

                        //if (offerta.Accetta(db, utente.Persona, utente.Credito, ref messaggio)) => non passa compratore nell'offerta
                        if (offerta.Accetta(db, utente.Persona, idPayPal, ref messaggio))
                        {
                            transaction.Commit();
                            Models.ViewModels.Email.PagamentoOffertaViewModel offertaAccettata = new Models.ViewModels.Email.PagamentoOffertaViewModel();
                            offertaAccettata.NominativoDestinatario = offerta.PERSONA.NOME + " " + offerta.PERSONA.COGNOME;
                            offertaAccettata.NomeAnnuncio           = offerta.ANNUNCIO.NOME;
                            offertaAccettata.Moneta          = offerta.PUNTI;
                            offertaAccettata.SoldiSpedizione = offerta.SOLDI;
                            offertaAccettata.Baratti         = offerta.OFFERTA_BARATTO.Select(m => m.ANNUNCIO.NOME).ToList();
                            this.SendNotifica(utente.Persona, offerta.PERSONA, TipoNotifica.OffertaAccettata, ControllerContext, "offertaAccettata", offertaAccettata);
                            //System.Web.Routing.RouteValueDictionary data = new System.Web.Routing.RouteValueDictionary(new { token = offerta.ID });
                            _UrlFinePagamento = Url.Action("OfferteRicevute", "Vendite", null, this.Request.Url.Scheme, this.Request.Url.Host);
                            //return (db.SaveChanges() > 0);
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        LoggatoreModel.Errore(ex);
                        throw ex;
                    }
                }
            }
            return(false);
        }