private int SavePayPal(DatabaseContext db, Payment payment)
        {
            var         transazione = payment.transactions.FirstOrDefault();
            TIPO_VALUTA tipoValuta  = (HttpContext.Application["tipoValuta"] as List <TIPO_VALUTA>).SingleOrDefault(m => m.CODICE == transazione.amount.currency);

            PAYPAL paypal = new PAYPAL();

            paypal.KEY_PAYPAL       = payment.id;
            paypal.NUMERO_FATTURA   = transazione.invoice_number;
            paypal.NOME             = transazione.description;
            paypal.IMPORTO          = transazione.amount.total.ParseFromPayPal();
            paypal.ID_TIPO_VALUTA   = tipoValuta.ID;
            paypal.DATA_INSERIMENTO = DateTime.Now;
            paypal.STATO            = (int)Stato.ATTIVO;
            db.PAYPAL.Add(paypal);
            db.SaveChanges();
            foreach (var item in transazione.item_list.items)
            {
                PAYPAL_DETTAGLIO dettaglio = new PAYPAL_DETTAGLIO();
                dettaglio.ID_PAYPAL = paypal.ID;
                dettaglio.NOME      = item.name;
                dettaglio.IMPORTO   = item.price.ParseFromPayPal();
                dettaglio.QUANTITA  = Convert.ToInt32(item.quantity);
                dettaglio.STATO     = (int)Stato.ATTIVO;
                db.PAYPAL_DETTAGLIO.Add(dettaglio);
                db.SaveChanges();
            }
            return(paypal.ID);
        }
        private List <Transaction> GetListTransactionFromOffertaOK(OffertaModel offerta, 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 (offerta.ANNUNCIO.TIPO_PAGAMENTO != (int)TipoPagamento.HAPPY)
            {
                if (offerta.SOLDI != null && offerta.SOLDI > 0)
                {
                    itemList.items.Add(new Item()
                    {
                        name     = "Pagamento offerta per annuncio: " + offerta.ANNUNCIO.NOME,
                        currency = tipoValuta.CODICE,
                        price    = ConvertDecimalToString((decimal)offerta.SOLDI),
                        quantity = "1",
                        sku      = "sku"
                    });
                    //decimal percentualeAnnuncio = Convert.ToDecimal(System.Configuration.ConfigurationManager.AppSettings["annuncioMonetaRealePercentuale"]);
                    //decimal commissioneAnnuncio = (((decimal)offerta.SOLDI / 100) * percentualeAnnuncio);
                    //itemList.items.Add(new Item()
                    //{
                    //    name = App_GlobalResources.Language.PayPalAd,
                    //    currency = tipoValuta.CODICE,
                    //    price = ConvertDecimalToString(commissioneAnnuncio),
                    //    quantity = "1",
                    //    sku = "sku"
                    //});
                }
            }

            ANNUNCIO_TIPO_SCAMBIO tipoScambio = offerta.ANNUNCIO.ANNUNCIO_TIPO_SCAMBIO.FirstOrDefault(m => m.TIPO_SCAMBIO == (int)TipoScambio.Spedizione);

            if (offerta.ANNUNCIO.ID_OGGETTO != null && tipoScambio != null)
            {
                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, " ", offerta.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);
                    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    = offerta.NOTE;
            transazione.invoice_number = guid + "|&%" + offerta.ANNUNCIO.TOKEN.ToString();
            transazione.amount         = amount;
            transazione.item_list      = itemList;

            lista.Add(transazione);
            return(lista);
        }
        public ActionResult Test()
        {
            APIContext           apiContext = Configuration.GetAPIContext();
            PayPalIndexViewModel viewModel  = new PayPalIndexViewModel();

            viewModel.Azione = AzionePayPal.Acquisto;
            string urlCancel = GetUrlCancel(viewModel.Azione, viewModel.Token);

            viewModel.Guid = Convert.ToString((new Random()).Next(100000));
            System.Web.Routing.RouteValueDictionary data = new System.Web.Routing.RouteValueDictionary(viewModel);
            string url = Url.Action("Payment", "PayPal", data, this.Request.Url.Scheme, this.Request.Url.Host);

            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);

            itemList.items.Add(new Item()
            {
                name     = "Nome test",
                currency = tipoValuta.CODICE,
                price    = ConvertDecimalToString(11),
                quantity = "1",
                sku      = "sku"
            });

            itemList.items.Add(new Item()
            {
                name     = string.Concat(App_GlobalResources.Language.Shipment, " test"),
                currency = tipoValuta.CODICE,
                price    = ConvertDecimalToString(11),
                //price = ConvertDecimalToString(new Decimal(1006.5)), // prova fissa verifica conversione
                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    = "Test descrizione";
            transazione.invoice_number = "testami";
            transazione.amount         = amount;
            transazione.item_list      = itemList;

            lista.Add(transazione);

            var createdPayment = this.CreatePayment(apiContext, url, urlCancel, lista);

            var links = createdPayment.links.GetEnumerator();

            string paypalRedirectUrl = null;

            while (links.MoveNext())
            {
                Links lnk = links.Current;

                if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                {
                    //saving the payapalredirect URL to which user will be redirected for payment
                    paypalRedirectUrl = lnk.href;
                }
            }

            return(Redirect(paypalRedirectUrl));
        }