public ActionResult PaymentReceipt()
        {
            string  username            = Request.Form["username"];
            string  password            = Request.Form["password"];
            int     orderNo             = Convert.ToInt32(Request.Form["payment_reference"]);
            decimal paymentAmount       = Convert.ToDecimal(Request.Form["am_payment"]);
            string  cardType            = Request.Form["nm_card_scheme"];
            string  nameOnCard          = Request.Form["nm_card_holder"];
            string  truncatedCardNumber = Request.Form["TruncatedCardNumber"];
            int     paymentStatus       = Convert.ToInt32(Request.Form["fl_success"]);

            if (username != WebConfigurationManager.AppSettings["payway_username"] || password != WebConfigurationManager.AppSettings["payway_password"])
            {
                return(new HttpUnauthorizedResult());
            }

            var order = applicationDataContext.Orders.Where(x => x.OrderNo == orderNo).Single();

            Payment payment = new Payment
            {
                Order           = order,
                PaymentAmount   = paymentAmount,
                CardType        = cardType,
                NameOnCard      = nameOnCard,
                TruncatedCardNo = truncatedCardNumber,
                PaymentStatus   = paymentStatus
            };

            applicationDataContext.AddToPayments(payment);
            order.Payments.Add(payment);
            applicationDataContext.AddLink(order, "Payments", payment);

            applicationDataContext.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        public string ProcessOrder()
        {
            context.AddToOrders(this.order);
            this.order.Sale.Orders.Add(this.order);
            context.AddLink(this.order.Sale, "Orders", this.order);

            foreach (var l in this.cart.Lines)
            {
                var orderDetail = new OrderDetail {
                    Quantity = l.Quantity
                };
                context.AddToOrderDetails(orderDetail);
                context.AddLink(this.order, "OrderDetails", orderDetail);

                orderDetail.SaleProduct = context.SaleProducts.Where(x => x.Id == l.SaleProduct.Id).First();
                orderDetail.SaleProduct.OrderDetails.Add(orderDetail);
                context.AddLink(orderDetail.SaleProduct, "OrderDetails", orderDetail);

                if (l.AssetAllocation != null)
                {
                    var assetAllocation = context.AssetAllocations.Where(x => x.Id == l.AssetAllocation.Id).Single();

                    assetAllocation.Order = this.order;
                    this.order.AssetAllocation.Add(l.AssetAllocation);
                    context.AddLink(this.order, "AssetAllocation", assetAllocation);
                }
            }

            context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

            string tokenRequest = BuildTokenRequest();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                WebConfigurationManager.AppSettings["payWayBaseUrl"] + "RequestToken");

            request.KeepAlive   = false;
            request.Method      = "POST";
            request.Timeout     = 60000;
            request.ContentType = "application/x-www-form-urlencoded; charset=" +
                                  System.Text.Encoding.UTF8.WebName;

            byte[] requestBody = System.Text.Encoding.UTF8.GetBytes(
                tokenRequest);

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(requestBody, 0, requestBody.Length);
            requestStream.Close();
            requestStream = null;

            WebResponse response = request.GetResponse();

            Stream       responseStream = response.GetResponseStream();
            StreamReader responseReader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
            string       tokenResponse  = responseReader.ReadToEnd();

            responseStream.Close();

            string[] responseParameters = tokenResponse.Split(new Char[] { '&' });
            string   token = null;

            for (int i = 0; i < responseParameters.Length; i++)
            {
                string   responseParameter = responseParameters[i];
                string[] paramNameValue    = responseParameter.Split(new Char[] { '=' }, 2);
                if ("token".Equals(paramNameValue[0]))
                {
                    token = paramNameValue[1];
                }
                else if ("error".Equals(paramNameValue[0]))
                {
                    throw new Exception(paramNameValue[1]);
                }
            }

            string handOffUrl = WebConfigurationManager.AppSettings["payWayBaseUrl"] + "MakePayment";

            handOffUrl += "?biller_code=" +
                          HttpUtility.UrlEncode(WebConfigurationManager.AppSettings["billerCode"]) +
                          "&token=" + HttpUtility.UrlEncode(token);

            this.cart.Clear();
            return(handOffUrl);
        }