public ActionResult Payment(PagamentoViewModel model)
        {
            model.WebSite    = Request.UrlReferrer.Host;
            model.UrlRequest = Request.UrlReferrer.OriginalString;
            try
            {
                // verifico se non è un test locale
                if (Session["testPaymentFromLocalHost"] != null)
                {
                    model.WebSite    = "http://www.gratisforgratis.com";
                    model.UrlRequest = "/Home/Index";
                    model.Token      = System.Configuration.ConfigurationManager.AppSettings["test"];
                    model.Test       = 1;
                }

                using (DatabaseContext db = new DatabaseContext())
                {
                    // verifica autenticità del portale web tramite dominio e token
                    ATTIVITA sito = db.ATTIVITA.FirstOrDefault(m => m.DOMINIO == model.WebSite && m.TOKEN == new Guid(model.Token));
                    if (sito != null)
                    {
                        SalvataggioPagamentoViewModel salvataggioPagamento = new SalvataggioPagamentoViewModel(model);

                        salvataggioPagamento.PortaleWebID = sito.ID_CONTO_CORRENTE;
                        // verifica se l'utente è registrato
                        PERSONA utentePagato = db.PERSONA.FirstOrDefault(m => m.PERSONA_EMAIL.SingleOrDefault(item => item.TIPO == (int)TipoEmail.Registrazione).EMAIL == model.EmailReceivent);
                        if (utentePagato != null)
                        {
                            salvataggioPagamento.UtentePagatoID = utentePagato.ID_CONTO_CORRENTE;
                            Session["pagamento"] = salvataggioPagamento;
                        }
                        else
                        {
                            ModelState.AddModelError("Error", Language.ErrorNotUser);
                            if (!String.IsNullOrEmpty(model.ReturnUrlForFailed))
                            {
                                return(Redirect(model.ReturnUrlForFailed));
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Error", Language.ErrorNotPartner);
                        if (!String.IsNullOrEmpty(model.ReturnUrlForFailed))
                        {
                            return(Redirect(model.ReturnUrlForFailed));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                ModelState.AddModelError("Error", ex.Message);
            }

            return(View(model));
        }
        public ActionResult SavePayment()
        {
            try
            {
                using (DatabaseContext db = new DatabaseContext())
                {
                    SalvataggioPagamentoViewModel datiPagamento = (SalvataggioPagamentoViewModel)Session["pagamento"];
                    if (!ModelState.IsValid)
                    {
                        return(View("Payment", datiPagamento));
                    }

                    // se esiste una sessione pagamento aperta e la mail ricevente dei soldi corrisponde a quella della login,
                    // allora blocco la login e scatta l'errore

                    if (Session["pagamento"] != null && datiPagamento.EmailReceivent.Equals(((PersonaModel)Session["utente"]).Email.FirstOrDefault(item => item.TIPO == (int)TipoEmail.Registrazione)))
                    {
                        ModelState.AddModelError("Error", string.Format(App_GlobalResources.Language.ErrorPaymentUser, datiPagamento.EmailReceivent));
                        return(View("Payment", datiPagamento));
                    }

                    // effettuare il salvataggio
                    TRANSAZIONE pagamento = new TRANSAZIONE();
                    pagamento.NOME = datiPagamento.DescriptionPayment;
                    //pagamento.ID_ATTIVITA = datiPagamento.PortaleWebID;
                    pagamento.SOLDI = (int)datiPagamento.TotalPrice;
                    pagamento.TIPO  = datiPagamento.TypePayment;
                    pagamento.ID_CONTO_DESTINATARIO = (datiPagamento.PortaleWebID != null)? ((ATTIVITA)Session["utente"]).ID_CONTO_CORRENTE : ((PersonaModel)Session["utente"]).Persona.ID_CONTO_CORRENTE;
                    pagamento.ID_CONTO_MITTENTE     = datiPagamento.UtentePagatoID;
                    pagamento.TEST = datiPagamento.Test;

                    db.TRANSAZIONE.Add(pagamento);
                    db.SaveChanges();
                    // impostare invio email pagamento effettuato
                    EmailModel email = new EmailModel(ControllerContext);
                    email.To.Add(new System.Net.Mail.MailAddress(datiPagamento.EmailReceivent, pagamento.CONTO_CORRENTE1.PERSONA.Select(item => item.NOME + ' ' + item.COGNOME).SingleOrDefault()));
                    email.Subject   = string.Format(Email.PaymentFromPartnersSubject, pagamento.NOME, (Session["utente"] as PersonaModel).Persona.NOME + ' ' + (Session["utente"] as PersonaModel).Persona.COGNOME, datiPagamento.Nominativo) + " - " + WebConfigurationManager.AppSettings["nomeSito"];
                    email.Body      = "PagamentoDaPartners";
                    email.DatiEmail = new SchedaPagamentoViewModel()
                    {
                        Nome       = pagamento.NOME,
                        Compratore = (Session["utente"] as PersonaModel).Persona.NOME + ' ' + (Session["utente"] as PersonaModel).Persona.COGNOME,
                        Venditore  = pagamento.CONTO_CORRENTE1.PERSONA.Select(item => item.NOME + ' ' + item.COGNOME).SingleOrDefault(),
                        Punti      = (int)pagamento.PUNTI,
                        Soldi      = (int)pagamento.SOLDI,
                        Data       = pagamento.DATA_INSERIMENTO,
                        Portale    = datiPagamento.Nominativo,
                    };
                    new EmailController().SendEmail(email);
                    RemoveSessionPayment();
                    if (String.IsNullOrEmpty(datiPagamento.ReturnUrlForSuccess))
                    {
                        return(Redirect(datiPagamento.UrlRequest));
                    }
                    else
                    {
                        return(Redirect(datiPagamento.ReturnUrlForSuccess));
                    }
                }
            }
            catch (Exception ex)
            {
                //Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                LoggatoreModel.Errore(ex);
                ModelState.AddModelError("Error", ex);
            }

            return(View("Payment", new PagamentoViewModel((PagamentoAbstractModel)Session["pagamento"])));
        }