using System; using System.Collections.Generic; using System.Configuration; using System.Text; using System.Web; using ECOEarth.Web.Domain; namespace ECOEarth.Web { public partial class Checkout : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Request["action"] != null && Request["action"].Equals("next")) { ProcessOrder(); } } protected void ProcessOrder() { Order order = new Order(); order.OrderCreated = DateTime.Now; order.OrderStatus = OrderStatus.Created; Customer customer = new Customer(); customer.Name = Request.Form["name"]; customer.Email = Request.Form["email"]; customer.Telephone = Request.Form["telephone"]; Address billingAddress = new Address(); billingAddress.StreetAddress = Request.Form["BillingStreetAddress"]; billingAddress.SecondLine = Request.Form["Billing2ndLine"]; billingAddress.TownCity = Request.Form["BillingTownCity"]; billingAddress.County = Request.Form["BillingCounty"]; billingAddress.Country = Request.Form["BillingCountry"]; billingAddress.Postcode = Request.Form["BillingPostcode"]; customer.BillingAddress = billingAddress; Address deliveryAddress = new Address(); deliveryAddress.StreetAddress = Request.Form["ShippingStreetAddress"]; deliveryAddress.SecondLine = Request.Form["Shipping2ndLine"]; deliveryAddress.TownCity = Request.Form["ShippingTownCity"]; deliveryAddress.County = Request.Form["ShippingCounty"]; deliveryAddress.Country = Request.Form["ShippingCountry"]; deliveryAddress.Postcode = Request.Form["ShippingPostcode"]; customer.DeliveryAddress = deliveryAddress; order.Customer = customer; OrderBasket orderBasket = Session["OrderBasket"] as OrderBasket; order.OrderBasket = orderBasket; order.ProductTotal = orderBasket.TotalProductPrice; order.ShippingTotal = orderBasket.TotalShippingPrice; order.VatTotal = orderBasket.TotalVATPrice; order.OrderTotal = order.ProductTotal + order.ShippingTotal + order.VatTotal; order.SaveAndFlush(); int orderId = order.OrderId; Session.Clear(); Session["OrderBasket"] = new OrderBasket(); Response.Redirect(BuildWorldPayUrl(order)); } public string BuildWorldPayUrl(Order order) { StringBuilder worldPayBuilder = new StringBuilder(); worldPayBuilder.Append(ConfigurationManager.AppSettings["WorldPayPostUrl"]); BuildQuerystring(worldPayBuilder, "instId", ConfigurationManager.AppSettings["WorldPayInstallationId"]); BuildQuerystring(worldPayBuilder, "cartId",order.OrderId.ToString()); BuildQuerystring(worldPayBuilder, "amount",order.OrderTotal.ToString()); BuildQuerystring(worldPayBuilder, "currency","GBP"); BuildQuerystring(worldPayBuilder, "desc", BuildOrderDescription(order.OrderBasket.OrderItems)); #region optional parameters for worldpay BuildQuerystring(worldPayBuilder, "address", order.Customer.BillingAddress.StreetAddress + "\n" + order.Customer.BillingAddress.SecondLine + "\n" + order.Customer.BillingAddress.TownCity + "\n" + order.Customer.BillingAddress.County); BuildQuerystring(worldPayBuilder, "postcode", order.Customer.BillingAddress.Postcode); BuildQuerystring(worldPayBuilder, "country", "GB"); BuildQuerystring(worldPayBuilder, "telephone", order.Customer.Telephone); BuildQuerystring(worldPayBuilder, "email", order.Customer.Email); #endregion if (Convert.ToBoolean(ConfigurationManager.AppSettings["IsTestMode"])) { BuildQuerystring(worldPayBuilder, "testMode", "100"); } return worldPayBuilder.ToString(); } private static string BuildOrderDescription(IEnumerable items) { StringBuilder orderDescription = new StringBuilder(); foreach (OrderItem oItem in items) { orderDescription.Append(oItem.Quantity.ToString()); orderDescription.Append("x "); orderDescription.Append(oItem.ProductName); orderDescription.Append(".\n"); } return orderDescription.ToString(); } private static void BuildQuerystring(StringBuilder builder, string key, string value) { builder.Append(key); builder.Append("="); builder.Append(HttpUtility.UrlEncode(value)); builder.Append("&"); } } }