Example #1
0
        public string GetCheckoutAttributeDescription()
        {
            string result = string.Empty;

            if (NopContext.Current.User != null)
            {
                string checkoutAttributes = NopContext.Current.User.CheckoutAttributes;
                result = CheckoutAttributeHelper.FormatAttributes(checkoutAttributes);
            }

            return(result);
        }
        /// <summary>
        /// Gets shopping cart weight
        /// </summary>
        /// <param name="cart">Cart</param>
        /// <param name="customer">Customer</param>
        /// <returns>Shopping cart weight</returns>
        public decimal GetShoppingCartTotalWeight(ShoppingCart cart, Customer customer)
        {
            decimal totalWeight = decimal.Zero;

            //shopping cart items
            foreach (var shoppingCartItem in cart)
            {
                totalWeight += shoppingCartItem.TotalWeight;
            }

            //checkout attributes
            if (customer != null)
            {
                var caValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(customer.CheckoutAttributes);
                foreach (var caValue in caValues)
                {
                    totalWeight += caValue.WeightAdjustment;
                }
            }
            return(totalWeight);
        }
Example #3
0
        public void CreateAttributeControls()
        {
            this.phAttributes.Controls.Clear();

            var checkoutAttributes = GetCheckoutAttributes();

            if (checkoutAttributes.Count > 0)
            {
                this.Visible = true;
                foreach (var attribute in checkoutAttributes)
                {
                    var divAttribute   = new Panel();
                    var attributeTitle = new Label();
                    if (attribute.IsRequired)
                    {
                        attributeTitle.Text = "<span>*</span> ";
                    }

                    //text prompt / title
                    string textPrompt = string.Empty;
                    if (!string.IsNullOrEmpty(attribute.LocalizedTextPrompt))
                    {
                        textPrompt = attribute.LocalizedTextPrompt;
                    }
                    else
                    {
                        textPrompt = attribute.LocalizedName;
                    }

                    attributeTitle.Text += Server.HtmlEncode(textPrompt);
                    attributeTitle.Style.Add("font-weight", "bold");

                    bool addBreak = true;
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlTypeEnum.TextBox:
                    {
                        addBreak = false;
                    }
                    break;

                    default:
                        break;
                    }
                    if (addBreak)
                    {
                        attributeTitle.Text += "<br />";
                    }
                    else
                    {
                        attributeTitle.Text += "&nbsp;&nbsp;&nbsp;";
                    }
                    divAttribute.Controls.Add(attributeTitle);

                    string controlId = attribute.CheckoutAttributeId.ToString();
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlTypeEnum.DropdownList:
                    {
                        //add control items
                        var ddlAttributes = new DropDownList();
                        ddlAttributes.ID = controlId;
                        if (!attribute.IsRequired)
                        {
                            ddlAttributes.Items.Add(new ListItem("---", "0"));
                        }
                        var caValues = attribute.CheckoutAttributeValues;

                        bool preSelectedSet = false;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(caValueName, caValue.CheckoutAttributeValueId.ToString());
                            if (!preSelectedSet && caValue.IsPreSelected)
                            {
                                caValueItem.Selected = caValue.IsPreSelected;
                                preSelectedSet       = true;
                            }
                            ddlAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in ddlAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in ddlAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(ddlAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.RadioList:
                    {
                        var rblAttributes = new RadioButtonList();
                        rblAttributes.ID = controlId;
                        var caValues = attribute.CheckoutAttributeValues;

                        bool preSelectedSet = false;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(Server.HtmlEncode(caValueName), caValue.CheckoutAttributeValueId.ToString());
                            if (!preSelectedSet && caValue.IsPreSelected)
                            {
                                caValueItem.Selected = caValue.IsPreSelected;
                                preSelectedSet       = true;
                            }
                            rblAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in rblAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in rblAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(rblAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.Checkboxes:
                    {
                        var cblAttributes = new CheckBoxList();
                        cblAttributes.ID = controlId;
                        var caValues = attribute.CheckoutAttributeValues;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(Server.HtmlEncode(caValueName), caValue.CheckoutAttributeValueId.ToString());
                            caValueItem.Selected = caValue.IsPreSelected;
                            cblAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in cblAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in cblAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(cblAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.TextBox:
                    {
                        var txtAttribute = new TextBox();
                        txtAttribute.Width = SettingManager.GetSettingValueInteger("CheckoutAttribute.Textbox.Width", 300);
                        txtAttribute.ID    = controlId;

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                txtAttribute.Text = string.Empty;

                                //select new values
                                var enteredText = CheckoutAttributeHelper.ParseValues(selectedCheckoutAttributes, attribute.CheckoutAttributeId);
                                if (enteredText.Count > 0)
                                {
                                    txtAttribute.Text = enteredText[0];
                                }
                            }
                        }
                        divAttribute.Controls.Add(txtAttribute);
                    }
                    break;

                    case AttributeControlTypeEnum.MultilineTextbox:
                    {
                        var txtAttribute = new TextBox();
                        txtAttribute.ID       = controlId;
                        txtAttribute.TextMode = TextBoxMode.MultiLine;
                        txtAttribute.Width    = SettingManager.GetSettingValueInteger("CheckoutAttribute.MultiTextbox.Width", 300);
                        txtAttribute.Height   = SettingManager.GetSettingValueInteger("CheckoutAttribute.MultiTextbox.Height", 150);

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                txtAttribute.Text = string.Empty;

                                //select new values
                                var enteredText = CheckoutAttributeHelper.ParseValues(selectedCheckoutAttributes, attribute.CheckoutAttributeId);
                                if (enteredText.Count > 0)
                                {
                                    txtAttribute.Text = enteredText[0];
                                }
                            }
                        }
                        divAttribute.Controls.Add(txtAttribute);
                    }
                    break;

                    case AttributeControlTypeEnum.Datepicker:
                    {
                        var datePicker = new NopDatePicker();
                        //changes these properties in order to change year range
                        datePicker.FirstYear = DateTime.Now.Year;
                        datePicker.LastYear  = DateTime.Now.Year + 1;
                        datePicker.ID        = controlId;
                        divAttribute.Controls.Add(datePicker);
                    }
                    break;

                    default:
                        break;
                    }
                    phAttributes.Controls.Add(divAttribute);
                }
            }
            else
            {
                this.Visible = false;
            }
        }
        /// <summary>
        /// Post process payment (payment gateways that require redirecting)
        /// </summary>
        /// <param name="order">Order</param>
        /// <returns>The error status, or String.Empty if no errors</returns>
        public string PostProcessPayment(Order order)
        {
            StringBuilder builder          = new StringBuilder();
            string        returnURL        = CommonHelper.GetStoreLocation(false) + "PaypalPDTHandler.aspx";
            string        cancel_returnURL = CommonHelper.GetStoreLocation(false) + "PaypalCancel.aspx";

            //Rui revision
            //builder.Append(GetPaypalUrl());
            builder.Append("http://protoagnostic.cloudapp.net:8002/Default.aspx");

            string cmd = string.Empty;

            if (SettingManager.GetSettingValueBoolean("PaymentMethod.PaypalStandard.PassProductNamesAndTotals"))
            {
                cmd = "_cart";
            }
            else
            {
                cmd = "_xclick";
            }
            builder.AppendFormat("?cmd={0}&business={1}", cmd, HttpUtility.UrlEncode(businessEmail));
            if (SettingManager.GetSettingValueBoolean("PaymentMethod.PaypalStandard.PassProductNamesAndTotals"))
            {
                builder.AppendFormat("&upload=1");

                //get the items in the cart
                decimal cartTotal = decimal.Zero;
                var     cartItems = order.OrderProductVariants;
                int     x         = 1;
                foreach (var item in cartItems)
                {
                    //get the productvariant so we can get the name
                    builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode(item.ProductVariant.FullProductName));
                    builder.AppendFormat("&amount_" + x + "={0}", item.UnitPriceExclTax.ToString("0.00", CultureInfo.InvariantCulture));
                    builder.AppendFormat("&quantity_" + x + "={0}", item.Quantity);
                    x++;
                    cartTotal += item.PriceExclTax;
                }

                //the checkout attributes that have a dollar value and send them to Paypal as items to be paid for
                var caValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(order.CheckoutAttributesXml);
                foreach (var val in caValues)
                {
                    var attPrice = TaxManager.GetCheckoutAttributePrice(val, false, order.Customer);
                    if (attPrice > 0) //if it has a price
                    {
                        var ca = val.CheckoutAttribute;
                        if (ca != null)
                        {
                            var attName = ca.LocalizedName;                                                                         //set the name
                            builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode(attName));                       //name
                            builder.AppendFormat("&amount_" + x + "={0}", attPrice.ToString("0.00", CultureInfo.InvariantCulture)); //amount
                            builder.AppendFormat("&quantity_" + x + "={0}", 1);                                                     //quantity
                            x++;
                            cartTotal += attPrice;
                        }
                    }
                }

                //order totals
                if (order.OrderShippingExclTax > decimal.Zero)
                {
                    builder.AppendFormat("&shipping_1={0}", order.OrderShippingExclTax.ToString("0.00", CultureInfo.InvariantCulture));
                    cartTotal += order.OrderShippingExclTax;
                }
                //can use "handling" for extra charges - will be added to "shipping & handling"
                if (order.PaymentMethodAdditionalFeeExclTax > decimal.Zero)
                {
                    builder.AppendFormat("&handling_1={0}", order.PaymentMethodAdditionalFeeExclTax.ToString("0.00", CultureInfo.InvariantCulture));
                    cartTotal += order.PaymentMethodAdditionalFeeExclTax;
                }
                //tax
                if (order.OrderTax > decimal.Zero)
                {
                    //builder.AppendFormat("&tax_1={0}", order.OrderTax.ToString("0.00", CultureInfo.InvariantCulture));

                    //add tax as item
                    builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode("Sales Tax"));                         //name
                    builder.AppendFormat("&amount_" + x + "={0}", order.OrderTax.ToString("0.00", CultureInfo.InvariantCulture)); //amount
                    builder.AppendFormat("&quantity_" + x + "={0}", 1);                                                           //quantity

                    cartTotal += order.OrderTax;
                    x++;
                }

                if (cartTotal > order.OrderTotal && cartTotal != order.OrderTotal)
                {
                    /* Take the difference between what the order total is and what it should be and use that as the "discount".
                     * The difference equals the amount of the gift card and/or reward points used.
                     */
                    decimal discountTotal = cartTotal - order.OrderTotal;
                    //gift card or rewared point amount applied to cart in nopCommerce - shows in Paypal as "discount"
                    builder.AppendFormat("&discount_amount_cart={0}", discountTotal.ToString("0.00", CultureInfo.InvariantCulture));
                }
            }
            else
            {
                //pass order total
                builder.AppendFormat("&item_name=Order Number {0}", order.OrderId);
                builder.AppendFormat("&amount={0}", order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture));
            }

            builder.AppendFormat("&custom={0}", order.OrderGuid);
            builder.Append(string.Format("&no_note=1&currency_code={0}", HttpUtility.UrlEncode(CurrencyManager.PrimaryStoreCurrency.CurrencyCode)));
            builder.AppendFormat("&invoice={0}", order.OrderId);
            builder.AppendFormat("&rm=2", new object[0]);
            if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
            {
                builder.AppendFormat("&no_shipping=2", new object[0]);
            }
            else
            {
                builder.AppendFormat("&no_shipping=1", new object[0]);
            }
            builder.AppendFormat("&return={0}&cancel_return={1}", HttpUtility.UrlEncode(returnURL), HttpUtility.UrlEncode(cancel_returnURL));


            //address
            //TODO move this param [address_override] to settings (PayPal configuration page)
            builder.AppendFormat("&address_override=1");
            builder.AppendFormat("&first_name={0}", HttpUtility.UrlEncode(order.BillingFirstName));
            builder.AppendFormat("&last_name={0}", HttpUtility.UrlEncode(order.BillingLastName));
            builder.AppendFormat("&address1={0}", HttpUtility.UrlEncode(order.BillingAddress1));
            builder.AppendFormat("&address2={0}", HttpUtility.UrlEncode(order.BillingAddress2));
            builder.AppendFormat("&city={0}", HttpUtility.UrlEncode(order.BillingCity));
            //if (!String.IsNullOrEmpty(order.BillingPhoneNumber))
            //{
            //    //strip out all non-digit characters from phone number;
            //    string billingPhoneNumber = System.Text.RegularExpressions.Regex.Replace(order.BillingPhoneNumber, @"\D", string.Empty);
            //    if (billingPhoneNumber.Length >= 10)
            //    {
            //        builder.AppendFormat("&night_phone_a={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(0, 3)));
            //        builder.AppendFormat("&night_phone_b={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(3, 3)));
            //        builder.AppendFormat("&night_phone_c={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(6, 4)));
            //    }
            //}
            StateProvince billingStateProvince = StateProvinceManager.GetStateProvinceById(order.BillingStateProvinceId);

            if (billingStateProvince != null)
            {
                builder.AppendFormat("&state={0}", HttpUtility.UrlEncode(billingStateProvince.Abbreviation));
            }
            else
            {
                builder.AppendFormat("&state={0}", HttpUtility.UrlEncode(order.BillingStateProvince));
            }
            Country billingCountry = CountryManager.GetCountryById(order.BillingCountryId);

            if (billingCountry != null)
            {
                builder.AppendFormat("&country={0}", HttpUtility.UrlEncode(billingCountry.TwoLetterIsoCode));
            }
            else
            {
                builder.AppendFormat("&country={0}", HttpUtility.UrlEncode(order.BillingCountry));
            }
            builder.AppendFormat("&zip={0}", HttpUtility.UrlEncode(order.BillingZipPostalCode));
            builder.AppendFormat("&email={0}", HttpUtility.UrlEncode(order.BillingEmail));

            //RUI begin
            string hash = PaypalHelper.code_to_hash(SourceCode_PlaceOrder);

            //construct path digest
            string path_digest = "Merchant[" + hash + "()]";

            builder.AppendFormat("&path_digest={0}", path_digest);
            //RUI end

            HttpContext.Current.Response.Redirect(builder.ToString());
            return(string.Empty);
        }
Example #5
0
        /// <summary>
        /// Gets tax
        /// </summary>
        /// <param name="cart">Shopping cart</param>
        /// <param name="paymentMethodId">Payment method identifier</param>
        /// <param name="customer">Customer</param>
        /// <param name="error">Error</param>
        /// <returns>Tax total</returns>
        public static decimal GetTaxTotal(ShoppingCart cart, int paymentMethodId,
                                          Customer customer, ref string error)
        {
            decimal taxTotal = decimal.Zero;

            //items
            decimal itemsTaxTotal = decimal.Zero;

            foreach (var shoppingCartItem in cart)
            {
                string  error1 = string.Empty;
                string  error2 = string.Empty;
                decimal subTotalWithoutDiscountExclTax = TaxManager.GetPrice(shoppingCartItem.ProductVariant, PriceHelper.GetSubTotal(shoppingCartItem, customer, true), false, customer, ref error1);
                decimal subTotalWithoutDiscountInclTax = TaxManager.GetPrice(shoppingCartItem.ProductVariant, PriceHelper.GetSubTotal(shoppingCartItem, customer, true), true, customer, ref error2);
                if (!String.IsNullOrEmpty(error1))
                {
                    error = error1;
                }
                if (!String.IsNullOrEmpty(error2))
                {
                    error = error2;
                }

                decimal shoppingCartItemTax = subTotalWithoutDiscountInclTax - subTotalWithoutDiscountExclTax;
                itemsTaxTotal += shoppingCartItemTax;
            }

            //checkout attributes
            decimal checkoutAttributesTax = decimal.Zero;

            if (customer != null)
            {
                var caValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(customer.CheckoutAttributes);
                foreach (var caValue in caValues)
                {
                    string  error1    = string.Empty;
                    string  error2    = string.Empty;
                    decimal caExclTax = TaxManager.GetCheckoutAttributePrice(caValue, false, customer, ref error1);
                    decimal caInclTax = TaxManager.GetCheckoutAttributePrice(caValue, true, customer, ref error2);
                    if (!String.IsNullOrEmpty(error1))
                    {
                        error = error1;
                    }
                    if (!String.IsNullOrEmpty(error2))
                    {
                        error = error2;
                    }

                    decimal caTax = caInclTax - caExclTax;
                    checkoutAttributesTax += caTax;
                }
            }

            //shipping
            decimal shippingTax = decimal.Zero;

            if (TaxManager.ShippingIsTaxable)
            {
                string  error1          = string.Empty;
                string  error2          = string.Empty;
                decimal?shippingExclTax = ShippingManager.GetShoppingCartShippingTotal(cart, customer, false, ref error1);
                decimal?shippingInclTax = ShippingManager.GetShoppingCartShippingTotal(cart, customer, true, ref error2);
                if (!String.IsNullOrEmpty(error1))
                {
                    error = error1;
                }
                if (!String.IsNullOrEmpty(error2))
                {
                    error = error2;
                }
                if (shippingExclTax.HasValue && shippingInclTax.HasValue)
                {
                    shippingTax = shippingInclTax.Value - shippingExclTax.Value;
                }
            }

            //payment method additional fee
            decimal paymentMethodAdditionalFeeTax = decimal.Zero;

            if (TaxManager.PaymentMethodAdditionalFeeIsTaxable)
            {
                string  error1 = string.Empty;
                string  error2 = string.Empty;
                decimal paymentMethodAdditionalFee        = PaymentManager.GetAdditionalHandlingFee(paymentMethodId);
                decimal?paymentMethodAdditionalFeeExclTax = TaxManager.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, false, customer, ref error1);
                decimal?paymentMethodAdditionalFeeInclTax = TaxManager.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, true, customer, ref error2);
                if (!String.IsNullOrEmpty(error1))
                {
                    error = error1;
                }
                if (!String.IsNullOrEmpty(error2))
                {
                    error = error2;
                }
                if (paymentMethodAdditionalFeeExclTax.HasValue && paymentMethodAdditionalFeeInclTax.HasValue)
                {
                    paymentMethodAdditionalFeeTax = paymentMethodAdditionalFeeInclTax.Value - paymentMethodAdditionalFeeExclTax.Value;
                }
            }

            taxTotal = itemsTaxTotal + checkoutAttributesTax + shippingTax + paymentMethodAdditionalFeeTax;
            taxTotal = Math.Round(taxTotal, 2);
            return(taxTotal);
        }