// GET: Cart/Details/5 public async Task <ActionResult> Details() { wLogUser = (LoggedUser)Session["CurrentUser"]; Cart wCart = ConvertEntityUserTOUserModel.ConvertoCartEntityTOCartModel(await new CartBL().GetCartByUser(wLogUser.wDetailUser.Id)); wCart.UserOwner = wLogUser.wDetailUser; return(View("Details", wCart)); }
public async Task <ActionResult> PaymentWithPaypal(string userId, string Cancel = null) { //getting the apiContext APIContext apiContext = PaypalConfiguration.GetAPIContext(); wLogUser = (LoggedUser)Session["CurrentUser"]; Cart wCart = null; try { //A resource representing a Payer that funds a payment Payment Method as paypal //Payer Id will be returned when payment proceeds or click to pay string payerId = Request.Params["PayerID"]; if (string.IsNullOrEmpty(payerId)) { //this section will be executed first because PayerID doesn't exist //it is returned by the create function call of the payment class // Creating a payment // baseURL is the url on which paypal sendsback the data. string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Cart/PaymentWithPayPal?"; //here we are generating guid for storing the paymentID received in session //which will be used in the payment execution var guid = Convert.ToString((new Random()).Next(100000)); //CreatePayment function gives us the payment approval url //on which payer is redirected for paypal account payment wCart = ConvertEntityUserTOUserModel.ConvertoCartEntityTOCartModel(await new CartBL().GetCartByUser(wLogUser.wDetailUser.Id)); if (wCart.DetailsCart.Count == 0) { ViewBag.ErrorMessage = "Non sono presenti articoli nel carrello"; wCart.UserOwner = wLogUser.wDetailUser; return(View("Details", wCart)); } wCart.NumOrder = new CartBL().GenerateNumOrder(); Session["NumOrder"] = wCart.NumOrder; var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid, wCart); //get links returned from paypal in response to Create function call 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; } } // saving the paymentID in the key guid Session.Add(guid, createdPayment.id); return(Redirect(paypalRedirectUrl)); } else { // This function exectues after receving all parameters for the payment var guid = Request.Params["guid"]; var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string); //If executed payment failed then we will show payment failure message to user if (executedPayment.state.ToLower() != "approved") { return(View("FailureView")); } } } catch (Exception ex) { return(View("FailureView")); } //on successful payment, show success page to user. wLogUser = (LoggedUser)Session["CurrentUser"]; string wNumOrder = Session["NumOrder"] != null ? Session["NumOrder"].ToString() : ""; //scrittura pdf di conferma acquisto Log.Info("Scrittura PDF per ordine " + wNumOrder); var appSettings = ConfigurationManager.AppSettings; string wUrlPDF = appSettings["UlrPDF"]; FileStream wPFD = ManagementDocument.CreateOrderDocument(wUrlPDF, wNumOrder, await new CartBL().GetCartByUser(wLogUser.wDetailUser.Id), ConvertEntityUserTOUserModel.ConvertoUserEntityTOUserModel(wLogUser.wDetailUser)); //invio Mail try { Log.Info("Tentativo invio mail per ordine " + wNumOrder); MailManagment.SendEmail(wPFD.Name, wLogUser.Email, wLogUser.wDetailUser.Name, wLogUser.wDetailUser.Surname); } catch (Exception ex) { Log.Error("Errore durante l'invio della mail.", ex); } try { //creo lo storico dell'ordine Log.Info("Scrittura Storico per ordine " + wNumOrder + " dell'utente " + wLogUser.Id); await new CartBL().AddHistoryBuy(wLogUser.wDetailUser.Id, wNumOrder); } catch (Exception ex) { Log.Error("Errore durante la scrittura dello storico ordine.", ex); } return(View("SuccessView")); }