예제 #1
0
        public ActionResult PaymentWithPaypal()
        {
            //getting the apiContext as earlier
            APIContext apiContext = Configuration.GetAPIContext();

            try
            {
                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.

                    // So we have provided URL of this controller only

                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?";

                    //guid we are generating for storing the paymentID received in session

                    //after calling the create function and it is 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 acccount payment

                    var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);

                    //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 section is executed when we have received all the payments parameters

                    // from the previous call to the function Create

                    // Executing a payment

                    var guid = Request.Params["guid"];

                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                    if (executedPayment.state.ToLower() != "approved")
                    {
                        Session["cartError"] = "Your Payment Cannot be Processed. Please Try Again";
                        return(RedirectToAction("Cart", "Store"));
                    }
                }
            }
            catch (Exception ex)
            {
                Session["cartError"] = "Your Payment Cannot be Processed. Please Try Again";
                Logger.Log("Error" + ex.Message);
                return(RedirectToAction("Cart", "Store"));
            }

            // If we reach here, the payment was successful.
            // Creating a Order for the User
            User currentUser = ModelHelpers.GetCurrentUser(db);

            Models.Order order = new Models.Order();
            order.Id         = db.Orders.Count() + 8;
            order.Tax        = 0;
            order.TotalPrice = 0;
            foreach (nItem item in (List <nItem>)Session["cart"])
            {
                item.Ite.Quantity = item.Quantity;
                order.Items.Add(item.Ite);
                order.Tax        += item.Ite.Price * item.Ite.Quantity * 0.13M;
                order.TotalPrice += item.Ite.Price * item.Ite.Quantity;
                // Modifying the current Items' Quantities
                db.Items.Where(i => i.Id == item.Ite.Id).First().Quantity -= item.Quantity;
            }
            order.TotalPrice += order.Tax;
            currentUser.Orders.Add(order);

            // Lest we forgetti, Save the Spaghetti
            db.SaveChanges();
            // Empty the current cart.
            Session["cart"] = new List <nItem>();

            return(View("SuccessView", currentUser));
        }
예제 #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Check if user already exists with that email
                User currentUser = ModelHelpers.GetCurrentUser(db);

                // If a user is not found
                if (currentUser == null)
                {
                    PasswordHasher hash = new PasswordHasher();

                    User newUser = db.Users.Create();

                    newUser.UserName  = model.Username;
                    newUser.Email     = model.Email;
                    newUser.Password  = hash.HashPassword(model.Password);
                    newUser.FirstName = model.FirstName;
                    newUser.LastName  = model.LastName;
                    if (model.Street != null)
                    {
                        newUser.Address = model.Street;
                    }
                    if (model.Country != null)
                    {
                        newUser.Country = model.Country;
                    }
                    if (model.City != null)
                    {
                        newUser.City = model.City;
                    }
                    if (model.Province != null)
                    {
                        newUser.StateProv = model.Province;
                    }
                    if (model.PostalCode != null)
                    {
                        newUser.PostalCode = model.PostalCode;
                    }
                    if (model.PhoneNumber != null)
                    {
                        newUser.PhoneNumber = model.PhoneNumber;
                    }
                    db.Users.Add(newUser);
                    db.SaveChanges();
                    ModelHelpers.CreateUserStoreIfNotExisting(db, newUser);
                    ModelHelpers.CreateUserPaymentMethodIfNotExisting(db, newUser);

                    // MVC will create a User seperate from the Digistore's User.
                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email, DigistoreUserId = newUser.Id
                    };
                    var result = await UserManager.CreateAsync(user, newUser.Password);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToAction("Index", "Home"));
                    }
                    AddErrors(result);
                }
                else
                {
                    ViewBag.EmailInUse = "That e-mail is already in use!";
                    return(View(model));
                }
            }
            // If we got this far, something failed, redisplay form.
            return(View(model));
        }