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); }
private List <Transaction> GetListTransactionFromOfferta(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); OFFERTA_SPEDIZIONE spedizione = offerta.OFFERTA_SPEDIZIONE.FirstOrDefault(); if (spedizione != null) { itemList.items.Add(new Item() { name = string.Concat(App_GlobalResources.Language.Shipment, " per annuncio: ", 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); 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 = offerta.NOTE; transazione.invoice_number = guid + "|&%" + offerta.ANNUNCIO.TOKEN.ToString(); transazione.amount = amount; transazione.item_list = itemList; lista.Add(transazione); return(lista); }