using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net.Mail; using System.Text; public partial class Checkout : WebBlockPage { private OrderInfo OrderInfo { get { if (Session["OrderInfo"] == null) Session["OrderInfo"] = new OrderInfo(); return (OrderInfo)Session["OrderInfo"]; } } protected override void OnInit(EventArgs e) { if (ShoppingCart.GrandTotal == 0) Response.Redirect("Cart.aspx"); SecurePage = true; base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Session["OrderID"] = null; Session["BillingAddressID"] = null; Utilities.PopulateStateList(ddlBillState); Utilities.PopulateCountryList(ddlBillCountry); ddlBillCountry.SelectedValue = "USA"; // set payment types string []paytypes = Configuration.Get("Payment.AllowedPaymentTypes").Split(','); foreach (string s in paytypes) { switch (s) { case "Credit Card": Payment1.ShowCreditCardForm = true; break; case "PO Number": Payment1.ShowPurchaseOrderForm = true; break; default: break; } } if (CustomerInfo != null) { LoadCustomerData(); } } } private void LoadCustomerData() { // LOAD BILLING ddlUseExistingBilling.Items.Clear(); DataTable dt = Database.Addresses_GetByCustomerID(CustomerInfo.CustomerID, true); if (dt.Rows.Count > 0) { ddlUseExistingBilling.Items.Add(new ListItem("Use a New Billing Address", "")); foreach (DataRow dr in dt.Rows) { string AddressSnippet = (string)dr["Address"] + ", " + (string)dr["City"] + ", " + (string)dr["State"] + ", " + (string)dr["ZipCode"]; AddressSnippet = AddressSnippet.Substring(0, Math.Min(40, AddressSnippet.Length)) + "..."; ddlUseExistingBilling.Items.Add(new ListItem(AddressSnippet, dr["AddressID"].ToString())); } } if (OrderInfo.BillingAddressID > 0) { DataRow dr = Database.Addresses_GetByAddressID(OrderInfo.BillingAddressID); txtBillFirstName.Text = (string)dr["FirstName"]; txtBillLastName.Text = (string)dr["LastName"]; txtBillAddress.Text = (string)dr["Address"]; txtBillCity.Text = (string)dr["City"]; ddlBillState.SelectedValue = (string)dr["State"]; ddlBillCountry.SelectedValue = (string)dr["Country"]; txtBillZipCode.Text = (string)dr["ZipCode"]; txtBillPhone.Text = (string)dr["Phone"]; } else { // pre-load if there is only one existing address if (ddlUseExistingBilling.Items.Count == 2) { ddlUseExistingBilling.SelectedValue = ddlUseExistingBilling.Items[1].Value; ddlUseExistingBilling_SelectedIndexChanged(null, null); } } } void Login1_SuccessfulLogin(object sender, EventArgs e) { LoadCustomerData(); } protected void ddlUseExistingBilling_SelectedIndexChanged(object sender, EventArgs e) { if (ddlUseExistingBilling.SelectedValue == "") { txtBillFirstName.Text = ""; txtBillLastName.Text = ""; txtBillAddress.Text = ""; txtBillCity.Text = ""; ddlBillState.SelectedValue = ""; ddlBillCountry.SelectedValue = "USA"; txtBillZipCode.Text = ""; txtBillPhone.Text = ""; txtBillFirstName.Enabled = true; txtBillLastName.Enabled = true; txtBillAddress.Enabled = true; txtBillCity.Enabled = true; ddlBillState.Enabled = true; ddlBillCountry.Enabled = true; txtBillZipCode.Enabled = true; txtBillPhone.Enabled = true; } else { DataRow dr = Database.Addresses_GetByAddressID(Int32.Parse(ddlUseExistingBilling.SelectedValue)); txtBillFirstName.Text = (string)dr["FirstName"]; txtBillLastName.Text = (string)dr["LastName"]; txtBillAddress.Text = (string)dr["Address"]; txtBillCity.Text = (string)dr["City"]; ddlBillState.SelectedValue = (string)dr["State"]; ddlBillCountry.SelectedValue = (string)dr["Country"]; txtBillZipCode.Text = (string)dr["ZipCode"]; txtBillPhone.Text = (string)dr["Phone"]; chkSameAsShipping.Checked = false; txtBillFirstName.Enabled = false; txtBillLastName.Enabled = false; txtBillAddress.Enabled = false; txtBillCity.Enabled = false; ddlBillState.Enabled = false; ddlBillCountry.Enabled = false; txtBillZipCode.Enabled = false; txtBillPhone.Enabled = false; } } protected void chkSameAsShipping_CheckedChanged(object sender, EventArgs e) { txtBillFirstName.Enabled = true; txtBillLastName.Enabled = true; txtBillAddress.Enabled = true; txtBillCity.Enabled = true; ddlBillState.Enabled = true; ddlBillCountry.Enabled = true; txtBillZipCode.Enabled = true; txtBillPhone.Enabled = true; if (chkSameAsShipping.Checked) { DataRow dr = Database.Addresses_GetByAddressID(OrderInfo.ShippingAddressID); txtBillFirstName.Text = (string)dr["FirstName"]; txtBillLastName.Text = (string)dr["LastName"]; txtBillAddress.Text = (string)dr["Address"]; txtBillCity.Text = (string)dr["City"]; ddlBillState.SelectedValue = (string)dr["State"]; ddlBillCountry.SelectedValue = (string)dr["Country"]; txtBillZipCode.Text = (string)dr["ZipCode"]; if (ddlUseExistingBilling.Visible && ddlUseExistingBilling.Items.Count > 0) ddlUseExistingBilling.SelectedValue = ""; } } protected void btnSubmit_Click(object sender, ImageClickEventArgs e) { Page.Validate("valCheckout"); if (Page.IsValid) { #region billing address management if (ddlUseExistingBilling.SelectedValue != "") OrderInfo.BillingAddressID = Int32.Parse(ddlUseExistingBilling.SelectedValue); else if (OrderInfo.BillingAddressID > 0) { Database.Addresses_Update(OrderInfo.BillingAddressID, CustomerInfo.CustomerID, true, txtBillFirstName.Text, txtBillLastName.Text, txtBillAddress.Text, txtBillCity.Text, ddlBillState.SelectedValue, ddlBillCountry.SelectedValue, txtBillZipCode.Text, txtBillPhone.Text, ""); } else { // add a new one OrderInfo.BillingAddressID = Database.Addresses_Add(CustomerInfo.CustomerID, true, txtBillFirstName.Text, txtBillLastName.Text, txtBillAddress.Text, txtBillCity.Text, ddlBillState.SelectedValue, ddlBillCountry.SelectedValue, txtBillZipCode.Text, txtBillPhone.Text, ""); } #endregion // save payment information Payment1.SavePaymentInformation(); // check if user wants to sign up for mailing list if(chkMailingList.Checked) { ConstantContact cc = new ConstantContact(); cc.AddContact(3, CustomerInfo.EmailAddress, txtBillFirstName.Text, txtBillLastName.Text, txtBillAddress.Text, "", txtBillCity.Text, ddlBillState.SelectedValue, ddlBillState.SelectedItem.Text, txtBillZipCode.Text, txtBillPhone.Text, ""); } // move onto the confirmation page Response.Redirect("ConfirmOrder.aspx"); } } }