Example #1
0
        /// <summary>
        /// Defines that the order is ready for pick up by the courrier
        /// </summary>
        /// <param name="id"></param>
        private async void OrderPutReady(int id)
        {
            var order = IncOrderList.FirstOrDefault(x => x.ID == id);

            if (order != null)
            {
                order = await _orderRepo.SetOrderAsReadyForPickup(id);

                await _orderRepo.SaveChangesAsync();

                BindData();
            }
        }
        public ActionResult Charge(string stripeEmail, string stripeToken)
        {
            // TODO Make less dependent on Stripe

            // Gets the total price
            var totalPrice   = GetTotalPriceFromCookieCart();
            int priceInCents = (int)(totalPrice * 100);

            // Gets the information about the customers from Stripe
            var customers = new StripeCustomerService();
            var customer  = customers.Create(new StripeCustomerCreateOptions
            {
                Email       = stripeEmail,
                SourceToken = stripeToken
            });

            // Gets the information about the charges made from Stripe
            var charges = new StripeChargeService();
            var charge  = charges.Create(new StripeChargeCreateOptions
            {
                Amount      = priceInCents,//charge in cents
                Description = "Sample Charge",
                Currency    = "eur",
                CustomerId  = customer.Id
            });

            if (charge.Paid)
            {
                // TODO ERROR

                // Gets the deliveryOption from the cookie
                Delivery deliveryOption = Newtonsoft.Json.JsonConvert.DeserializeObject <Delivery>(Request.Cookies[ConstVal.cookieDeliverOptionName].Value);

                IncomingOrder order;

                if (deliveryOption.OtherAddress)
                {
                    // If the client wants to use another address

                    // Get the client ID
                    int clientID = int.Parse(RudycommerceLib.Security.Encryption.DecryptString(Request.Cookies[ConstVal.cookieClientIDName].Value));

                    // Pass the inserted deliveryOption to the incoming order
                    order = new IncomingOrder(deliveryOption)
                    {
                        ClientID = clientID
                    };
                }
                else
                {
                    // If the client wants to use his home address for delivery

                    // Gets the client
                    var client = _client;

                    // Adds the client with his address to the incoming order
                    order = new IncomingOrder(client)
                    {
                        ClientID = client.ID
                    };
                }

                // Gives a status code 0 ( = Ordered, but not yet picked)
                order.StatusCode = 0;

                // Adds a paymentComplete, paymentOption and totalprice
                order.PaymentComplete = true;
                order.PaymentOption   = charge.Source.Card.Brand;
                order.TotalPrice      = totalPrice;

                // Gets the products from the cart cookie and adds them as order lines)
                var cart = GetCartFromCookie();
                foreach (var item in cart.ProductList)
                {
                    order.IncomingOrderLines.Add(new IncomingOrderLines
                    {
                        ProductID        = item.ID,
                        ProductQuantity  = item.Quantity,
                        ProductUnitPrice = item.Price / 100
                    });
                }

                // Creates the order and saves it
                _incOrderRepo.Add(order);
                _incOrderRepo.SaveChangesAsync();

                try
                {
                    string productsString = "";
                    foreach (var prod in cart.ProductList)
                    {
                        productsString += prod.Quantity.ToString() + " x " + prod.Name + "\r\n";
                    }

                    string title   = Resources.Checkout.OrderEmailTitle;
                    string content = string.Format(Resources.Checkout.OrderEmailContent, _client.FullName, productsString, deliveryOption.StreetAndNumber,
                                                   deliveryOption.MailBox, deliveryOption.PostalCode, deliveryOption.City);

                    GmailNotifier gmail = new GmailNotifier();
                    gmail.Notify(new System.Net.Mail.MailAddress(_client.Email), title, content);
                }
                catch (EmailSentFailed)
                {
                }
                catch (Exception)
                {
                    throw;
                }

                // Redirects to the Thank you page
                return(RedirectToAction("OrderFinished", "Orders", null));
            }
            else
            {
                // If the payment failed, send back to the payment page
                return(Payment());
            }
        }