public void CalculateTax(Cart cart) { // TODO: this doesn't take into account discounts decimal taxAmount = 0; if (cart.OrderInfo.TaxZoneGuid == Guid.Empty) { GeoCountry country = new GeoCountry(cart.OrderInfo.BillingCountry); GeoZone taxZone = GeoZone.GetByCode(country.Guid, cart.OrderInfo.BillingState); if (taxZone != null) { cart.OrderInfo.TaxZoneGuid = taxZone.Guid; } } if (cart.OrderInfo.TaxZoneGuid != Guid.Empty) { Collection <TaxRate> taxRates = TaxRate.GetTaxRates(this.SiteGuid, cart.OrderInfo.TaxZoneGuid); if (taxRates.Count > 0) { foreach (CartOffer offer in cart.CartOffers) { offer.Tax = 0; foreach (TaxRate taxRate in taxRates) { if (offer.TaxClassGuid == taxRate.TaxClassGuid) { offer.Tax += (taxRate.Rate * (offer.OfferPrice * offer.Quantity)); offer.Save(); taxAmount += offer.Tax; //break; } } } } } cart.TaxTotal = Math.Round(taxAmount, this.RoundingDecimalPlaces, this.RoundingMode); if (cart.TaxTotal < 0) { cart.TaxTotal = 0; } cart.Save(); }
//public decimal CalculateItemTax( // Guid siteGuid, // Guid taxZoneGuid, // Guid taxClassGuid, // decimal itemPrice, // int quantity) //{ // decimal taxAmount = 0; // if (taxZoneGuid == Guid.Empty) { return taxAmount; } // if (taxClassGuid == Guid.Empty) { return taxAmount; } // if (itemPrice == 0) { return taxAmount; } // Collection<TaxRate> taxRates = TaxRate.GetTaxRates(siteGuid, taxZoneGuid); // if (taxRates.Count > 0) // { // foreach (TaxRate taxRate in taxRates) // { // if (taxClassGuid == taxRate.TaxClassGuid) // { // taxAmount += (taxRate.Rate * (itemPrice * quantity)); // break; // } // } // } // return taxAmount; //} public void CalculateShipping(Cart cart) { decimal shippingAmount = 0; foreach (CartOffer offer in cart.CartOffers) { Collection <OfferProduct> offerProducts = OfferProduct.GetbyOffer(offer.OfferGuid); if (offerProducts != null) { foreach (OfferProduct offerProduct in offerProducts) { Product product = new Product(offerProduct.ProductGuid); if (product != null) { shippingAmount += (product.ShippingAmount * offer.Quantity); } } } } cart.ShippingTotal = Math.Round(shippingAmount, this.RoundingDecimalPlaces, this.RoundingMode); cart.Save(); }
private void LoadSettings() { store = StoreHelper.GetStore(); if (store == null) { return; } commerceConfig = SiteUtils.GetCommerceConfig(); currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code); if (Request.IsAuthenticated) { siteUser = SiteUtils.GetCurrentSiteUser(); } if (StoreHelper.UserHasCartCookie(store.Guid)) { cart = StoreHelper.GetCart(); if (cart != null) { cartOffers = cart.GetOffers(); canCheckoutWithoutAuthentication = store.CanCheckoutWithoutAuthentication(cart); if ((cart.LastModified < DateTime.UtcNow.AddDays(-1)) && (cart.DiscountCodesCsv.Length > 0)) { StoreHelper.EnsureValidDiscounts(store, cart); } if ((cart.UserGuid == Guid.Empty)&&(siteUser != null)) { cart.UserGuid = siteUser.UserGuid; cart.Save(); } cart.RefreshTotals(); } } ConfigureCheckoutButtons(); AddClassToBody("webstore webstorecheckout"); }
private void LoadSettings() { PageId = WebUtils.ParseInt32FromQueryString("pageid", -1); ModuleId = WebUtils.ParseInt32FromQueryString("mid", -1); payPalGetExpressCheckoutLogGuid = WebUtils.ParseGuidFromQueryString("plog", payPalGetExpressCheckoutLogGuid); if (payPalGetExpressCheckoutLogGuid == Guid.Empty) { Response.Redirect(SiteUtils.GetCurrentPageUrl()); } checkoutDetailsLog = new PayPalLog(payPalGetExpressCheckoutLogGuid); if (checkoutDetailsLog.RowGuid == Guid.Empty) { Response.Redirect(SiteUtils.GetCurrentPageUrl()); } cart = (Cart)SerializationHelper.DeserializeFromString(typeof(Cart), checkoutDetailsLog.SerializedObject); if (cart == null) { Response.Redirect(SiteUtils.GetCurrentPageUrl()); } cart.DeSerializeCartOffers(); cart.RefreshTotals(); if ((cart.LastModified < DateTime.UtcNow.AddDays(-1)) && (cart.DiscountCodesCsv.Length > 0)) { StoreHelper.EnsureValidDiscounts(store, cart); } siteUser = SiteUtils.GetCurrentSiteUser(); //if (siteUser == null) //{ // Response.Redirect(SiteUtils.GetCurrentPageUrl()); //} if ((siteUser != null)&&(cart.UserGuid == Guid.Empty)) { // user wasn't logged in when express checkout was called cart.UserGuid = siteUser.UserGuid; cart.Save(); //if (checkoutDetailsLog.UserGuid == Guid.Empty) //{ // // we need to make sure we have the user in the log and serialized cart // checkoutDetailsLog.UserGuid = siteUser.UserGuid; // cart.SerializeCartOffers(); // checkoutDetailsLog.SerializedObject = SerializationHelper.SerializeToSoap(cart); // checkoutDetailsLog.Save(); //} } if ((siteUser != null)&&(cart.UserGuid != siteUser.UserGuid)) { Response.Redirect(SiteUtils.GetCurrentPageUrl()); } if (ModuleId == -1) { ModuleId = StoreHelper.FindStoreModuleId(CurrentPage); } store = StoreHelper.GetStore(); commerceConfig = SiteUtils.GetCommerceConfig(); currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code); if (siteUser != null) { pnlRequireLogin.Visible = false; } else { btnMakePayment.Visible = false; } AddClassToBody("webstore webstoreexpresscheckout"); }
private static Cart CreateClerkCartAndSetCookie(Store store, string cartKey) { if (store == null) return null; if (store.Guid == Guid.Empty) return null; Cart cart = new Cart(); cart.StoreGuid = store.Guid; cart.CreatedFromIP = SiteUtils.GetIP4Address(); if ( (HttpContext.Current != null) && (HttpContext.Current.Request.IsAuthenticated) ) { SiteUser siteUser = SiteUtils.GetCurrentSiteUser(); cart.ClerkGuid = siteUser.UserGuid; } cart.Save(); CookieHelper.SetPersistentCookie(cartKey, cart.CartGuid.ToString()); HttpContext.Current.Items[cartKey] = cart; return cart; }
private static Cart CreateCartAndSetCookie(Store store) { if (store == null) return null; if (store.Guid == Guid.Empty) return null; string cartKey = "cart" + store.Guid.ToString(); Cart cart = new Cart(); cart.StoreGuid = store.Guid; cart.CreatedFromIP = SiteUtils.GetIP4Address(); if ( (HttpContext.Current != null) && (HttpContext.Current.Request.IsAuthenticated) ) { SiteUser siteUser = SiteUtils.GetCurrentSiteUser(); cart.UserGuid = siteUser.UserGuid; cart.LoadExistingUserCartIfExists(); } cart.Save(); SetCartCookie(store.Guid, cart.CartGuid); HttpContext.Current.Items[cartKey] = cart; return cart; }
public static bool ValidateAndApplyDiscounts(Store store, Cart cart, out string errorMessage) { if (cart == null) { errorMessage = WebStoreResources.DiscountInvalidCartError; return false; } if (store == null) { errorMessage = WebStoreResources.DiscountInvalidCartError; return false; } errorMessage = string.Empty; List<Discount> discountList = Discount.GetValidDiscounts(store.ModuleGuid, cart, cart.DiscountCodesCsv); cart.Discount = 0; cart.RefreshTotals(); cart.DiscountCodesCsv = string.Empty; if (cart.SubTotal <= 0) { cart.Save(); return false; } string comma = string.Empty; bool appliedDiscount = false; foreach (Discount d in discountList) { cart.DiscountCodesCsv += comma + d.DiscountCode; comma = ","; if (d.AbsoluteDiscount > 0) { cart.Discount += d.AbsoluteDiscount; appliedDiscount = true; } if (d.PercentageDiscount > 0) { cart.Discount += (cart.SubTotal * d.PercentageDiscount); appliedDiscount = true; } } cart.RefreshTotals(); if (!appliedDiscount) { errorMessage = WebStoreResources.DiscountInvalidCartError; } return appliedDiscount; }
//public static Cart GetCart(Guid storeGuid) //{ // if (HttpContext.Current != null) // { // string cartKey = "cart" + storeGuid.ToString(); // if (HttpContext.Current.Items[cartKey] != null) // { // return (Cart)HttpContext.Current.Items[cartKey]; // } // else // { // if (UserHasCartCookie(storeGuid)) // { // string cartCookie = GetCartCookie(storeGuid); // if (cartCookie.Length == 36) // { // Guid cartGuid = new Guid(cartCookie); // Cart cart = new Cart(cartGuid); // if (!cart.Exists) // { // return CreateCartAndSetCookie(storeGuid); // } // HttpContext.Current.Items[cartKey] = cart; // return cart; // } // else // { // // cookie is invalid // return CreateCartAndSetCookie(storeGuid); // } // } // else // { // // TODO: handle use case where user adds to cart on 1 machine // // then comes back to site on another machine and has no cart cookie // // look for a cart that has the userguid, // // if found set cookie for that cart // // new cart // return CreateCartAndSetCookie(storeGuid); // } // } // } // return null; //} public static void InitializeOrderInfo(Cart cart, SiteUser siteUser) { if (cart.OrderInfo.CustomerLastName.Length == 0) { if(siteUser.LastName.Length > 0) { cart.OrderInfo.CustomerLastName = siteUser.LastName; } else { cart.OrderInfo.CustomerLastName = siteUser.Name; } cart.OrderInfo.CustomerFirstName = siteUser.FirstName; } cart.OrderInfo.CustomerTelephoneDay = siteUser.PhoneNumber; cart.OrderInfo.CustomerEmail = siteUser.Email; cart.OrderInfo.Save(); cart.UserGuid = siteUser.UserGuid; cart.Save(); }
private static void VerifyCartUser(Cart cart) { if (HttpContext.Current == null) { return; } if (HttpContext.Current.Request == null) { return; } if (cart == null) { return; } if (cart.UserGuid == Guid.Empty) { return; } if (HttpContext.Current.Request.IsAuthenticated) { SiteUser currentUser = SiteUtils.GetCurrentSiteUser(); if (currentUser == null) { cart.ClearCustomerData(); cart.UserGuid = Guid.Empty; cart.OrderInfo.Save(); cart.Save(); } else { if (currentUser.UserGuid != cart.UserGuid) { cart.ClearCustomerData(); cart.OrderInfo.CustomerEmail = currentUser.Email; cart.OrderInfo.CustomerFirstName = currentUser.FirstName; cart.OrderInfo.CustomerLastName = currentUser.LastName; cart.OrderInfo.CustomerTelephoneDay = currentUser.PhoneNumber; cart.UserGuid = currentUser.UserGuid; cart.OrderInfo.Save(); cart.Save(); } } } else { cart.UserGuid = Guid.Empty; cart.Save(); } }
private void ProcessOrder( Cart cart, Store store, WorldPayPaymentResponse wpResponse, PayPalLog worldPayLog, Page page) { // process the cart into an order then // return an html order result template for use at world pay cart.DeSerializeCartOffers(); if (wpResponse.CompName.Length > 0) { cart.OrderInfo.CustomerCompany = wpResponse.CompName; } if (wpResponse.Address1.Length > 0) { cart.OrderInfo.CustomerAddressLine1 = wpResponse.Address1; } if (wpResponse.Address2.Length > 0) { cart.OrderInfo.CustomerAddressLine2 = wpResponse.Address2; } if (wpResponse.Address3.Length > 0) { cart.OrderInfo.CustomerAddressLine2 += " " + wpResponse.Address3; } if (wpResponse.Town.Length > 0) { cart.OrderInfo.CustomerCity = wpResponse.Town; } //cart.OrderInfo.DeliveryFirstName = wpResponse.Name; if( (wpResponse.Name.Length > 0) && ((cart.OrderInfo.CustomerLastName.Length == 0) || (!wpResponse.Name.Contains((cart.OrderInfo.CustomerLastName)))) ) { cart.OrderInfo.CustomerLastName = wpResponse.Name; // this is full name } if (wpResponse.Postcode.Length > 0) { cart.OrderInfo.CustomerPostalCode = wpResponse.Postcode; } if (wpResponse.Region.Length > 0) { cart.OrderInfo.CustomerState = wpResponse.Region; } if (wpResponse.Country.Length > 0) { cart.OrderInfo.CustomerCountry = wpResponse.Country; } if (wpResponse.Tel.Length > 0) { cart.OrderInfo.CustomerTelephoneDay = wpResponse.Tel; } if (wpResponse.Email.Length > 0) { cart.OrderInfo.CustomerEmail = wpResponse.Email; } cart.CopyCustomerToBilling(); cart.CopyCustomerToShipping(); //cart.TaxTotal = taxAmount; //cart.OrderTotal = grossAmount; //if (shippingAmount > 0) //{ // cart.ShippingTotal = shippingAmount; //} StoreHelper.EnsureUserForOrder(cart); cart.Save(); Order order = Order.CreateOrder( store, cart, wpResponse.TransId, wpResponse.TransId, string.Empty, wpResponse.Currency, "WorldPay", OrderStatus.OrderStatusFulfillableGuid); // grab the return url before we delete the un-needed logs string orderDetailUrl = worldPayLog.ReturnUrl; string storePageUrl = worldPayLog.RawResponse; // remove any previous logs GoogleCheckoutLog.DeleteByCart(order.OrderGuid); PayPalLog.DeleteByCart(order.OrderGuid); // create a final log that has the serialized reposnse from worldpay rather than the serialized cart worldPayLog = new PayPalLog(); worldPayLog.SiteGuid = store.SiteGuid; worldPayLog.StoreGuid = store.Guid; worldPayLog.CartGuid = order.OrderGuid; worldPayLog.UserGuid = order.UserGuid; worldPayLog.ProviderName = "WebStoreWorldPayResponseHandler"; worldPayLog.RequestType = "WorldPay"; worldPayLog.PaymentStatus = "Paid"; worldPayLog.PaymentType = "WorldPay"; worldPayLog.CartTotal = order.OrderTotal; worldPayLog.PayPalAmt = wpResponse.AuthAmount; worldPayLog.TransactionId = wpResponse.TransId; worldPayLog.CurrencyCode = wpResponse.Currency; worldPayLog.ReasonCode = wpResponse.AVS; worldPayLog.RawResponse = SerializationHelper.SerializeToString(wpResponse); worldPayLog.CreatedUtc = DateTime.UtcNow; worldPayLog.ReturnUrl = orderDetailUrl; worldPayLog.Save(); try { StoreHelper.ConfirmOrder(store, order); } catch (Exception ex) { log.Error("error sending confirmation email", ex); } // retrun the html if (config.WorldPayProduceShopperResponse) { CultureInfo currencyCulture = ResourceHelper.GetCurrencyCulture(wpResponse.Currency); string htmlTemplate = ResourceHelper.GetMessageTemplate(CultureInfo.CurrentUICulture, config.WorldPayShopperResponseTemplate); StringBuilder finalOutput = new StringBuilder(); finalOutput.Append(htmlTemplate); finalOutput.Replace("#WorldPayBannerToken", "<WPDISPLAY ITEM=banner>"); //required by worldpay finalOutput.Replace("#CustomerName", wpResponse.Name); finalOutput.Replace("#StoreName", store.Name); finalOutput.Replace("#OrderId", order.OrderGuid.ToString()); finalOutput.Replace("#StorePageLink", "<a href='" + storePageUrl + "'>" + storePageUrl + "</a>"); finalOutput.Replace("#OrderDetailLink", "<a href='" + orderDetailUrl + "'>" + orderDetailUrl + "</a>"); StringBuilder orderDetails = new StringBuilder(); DataSet dsOffers = Order.GetOrderOffersAndProducts(store.Guid, order.OrderGuid); foreach (DataRow row in dsOffers.Tables["Offers"].Rows) { string og = row["OfferGuid"].ToString(); orderDetails.Append(row["Name"].ToString() + " "); orderDetails.Append(row["Quantity"].ToString() + " @ "); orderDetails.Append(string.Format(currencyCulture, "{0:c}", Convert.ToDecimal(row["OfferPrice"]))); orderDetails.Append("<br />"); string whereClause = string.Format("OfferGuid = '{0}'", og); DataView dv = new DataView(dsOffers.Tables["Products"], whereClause, "", DataViewRowState.CurrentRows); if (dv.Count > 1) { foreach (DataRow r in dsOffers.Tables["Products"].Rows) { string pog = r["OfferGuid"].ToString(); if (og == pog) { orderDetails.Append(r["Name"].ToString() + " "); orderDetails.Append(r["Quantity"].ToString() + " <br />"); } } } } finalOutput.Replace("#OrderDetails", orderDetails.ToString()); page.Response.Write(finalOutput.ToString()); page.Response.Flush(); } }
private void LoadSettings() { store = StoreHelper.GetStore(); if (store == null) { return; } commerceConfig = SiteUtils.GetCommerceConfig(); currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code); storeCountry = new GeoCountry(siteSettings.DefaultCountryGuid); if (Request.IsAuthenticated) { siteUser = SiteUtils.GetCurrentSiteUser(); } if (StoreHelper.UserHasCartCookie(store.Guid)) { cart = StoreHelper.GetCart(); if (cart != null) { if ((cart.LastModified < DateTime.UtcNow.AddDays(-1)) && (cart.DiscountCodesCsv.Length > 0)) { StoreHelper.EnsureValidDiscounts(store, cart); } if (siteUser != null) { if (cart.UserGuid == Guid.Empty) { // take ownership of anonymous cart cart.UserGuid = siteUser.UserGuid; cart.Save(); StoreHelper.InitializeOrderInfo(cart, siteUser); } else { // cart already has a user guid but // check if it matches the current user // cart cookie could have been left behind by a previous user // on shared computers // if cart user guid doesn't match reset billing shipping info // and any other identifiers // but leave items in cart if (cart.UserGuid != siteUser.UserGuid) { cart.ResetUserInfo(); cart.UserGuid = siteUser.UserGuid; cart.Save(); StoreHelper.InitializeOrderInfo(cart, siteUser); } } } if (WebStoreConfiguration.IsDemo) { LoadDemoCustomer(); } canCheckoutWithoutAuthentication = store.CanCheckoutWithoutAuthentication(cart); // disable till I finish //canCheckoutWithoutAuthentication = false; } AddClassToBody("webstore webstoreconfirmorder"); } pnlRequireLogin.Visible = !Request.IsAuthenticated; if ((canCheckoutWithoutAuthentication)||(Request.IsAuthenticated)) { pnlOrderDetail.Visible = commerceConfig.CanProcessStandardCards || (commerceConfig.WorldPayInstallationId.Length > 0); pnlRequireLogin.Visible = true; pnlShippingTotal.Visible = false; pnlTaxTotal.Visible = false; pnlOrderTotal.Visible = false; } if ((cart != null) && (cart.SubTotal == 0) && (cart.CartOffers.Count > 0)) { // free checkout pnlOrderDetail.Visible = true; } if (pnlOrderDetail.Visible) { tblCountryList = GeoCountry.GetList(); } ConfigureCheckoutButtons(); //if (!Page.IsPostBack) //{ // if ((commerceConfig.PayPalIsEnabled) && (commerceConfig.PayPalUsePayPalStandard)) // { // if (Request.IsAuthenticated) // { // SetupPayPalStandardForm(); // } // else // { // // we need the user to be signed in before we send them to paypal if using PayPal Standard // // because we want to return them to their order summary and that requires login // // so we need to know who the user is before sending them to PayPal // litOr.Visible = false; // btnPayPal.Visible = false; // btnGoogleCheckout.Visible = false; // } // } //} //if (!Request.IsAuthenticated) //{ // pnlOrderDetail.Visible = false; // pnlRequireLogin.Visible = true; // pnlShippingTotal.Visible = false; // pnlTaxTotal.Visible = false; // pnlOrderTotal.Visible = false; // if (commerceConfig.GoogleCheckoutIsEnabled) // { // if ( // (!commerceConfig.Is503TaxExempt) // && ((cart != null) && (cart.HasDonations())) // ) // { // //btnGoogleCheckout.Visible = false; // lblGoogleMessage.Text = WebStoreResources.GoogleCheckoutDisabledForDonationsMessage; // lblGoogleMessage.Visible = true; // PaymentAcceptanceMark mark = (PaymentAcceptanceMark)pam1; // mark.SuppressGoogleCheckout = true; // mark = (PaymentAcceptanceMark)PaymentAcceptanceMark1; // mark.SuppressGoogleCheckout = true; // btnGoogleCheckout.Visible = true; // btnGoogleCheckout.Enabled = false; // } // } //} //else //{ // if ( // (!commerceConfig.Is503TaxExempt) // && ((cart != null)&& (cart.HasDonations())) // && (commerceConfig.GoogleCheckoutIsEnabled) // ) // { // btnGoogleCheckout.Visible = true; // btnGoogleCheckout.Enabled = false; // lblGoogleMessage.Text = WebStoreResources.GoogleCheckoutDisabledForDonationsMessage; // lblGoogleMessage.Visible = true; // } //} }
public void CalculateTax(Cart cart) { // TODO: this doesn't take into account discounts decimal taxAmount = 0; if(cart.OrderInfo.TaxZoneGuid == Guid.Empty) { GeoCountry country = new GeoCountry(cart.OrderInfo.BillingCountry); GeoZone taxZone = GeoZone.GetByCode(country.Guid, cart.OrderInfo.BillingState); if (taxZone != null) { cart.OrderInfo.TaxZoneGuid = taxZone.Guid; } } if (cart.OrderInfo.TaxZoneGuid != Guid.Empty) { Collection<TaxRate> taxRates = TaxRate.GetTaxRates(this.SiteGuid, cart.OrderInfo.TaxZoneGuid); if (taxRates.Count > 0) { foreach (CartOffer offer in cart.CartOffers) { offer.Tax = 0; foreach (TaxRate taxRate in taxRates) { if (offer.TaxClassGuid == taxRate.TaxClassGuid) { offer.Tax += (taxRate.Rate * (offer.OfferPrice * offer.Quantity)); offer.Save(); taxAmount += offer.Tax; //break; } } } } } cart.TaxTotal = Math.Round(taxAmount, this.RoundingDecimalPlaces, this.RoundingMode); if (cart.TaxTotal < 0) { cart.TaxTotal = 0; } cart.Save(); }
//public decimal CalculateItemTax( // Guid siteGuid, // Guid taxZoneGuid, // Guid taxClassGuid, // decimal itemPrice, // int quantity) //{ // decimal taxAmount = 0; // if (taxZoneGuid == Guid.Empty) { return taxAmount; } // if (taxClassGuid == Guid.Empty) { return taxAmount; } // if (itemPrice == 0) { return taxAmount; } // Collection<TaxRate> taxRates = TaxRate.GetTaxRates(siteGuid, taxZoneGuid); // if (taxRates.Count > 0) // { // foreach (TaxRate taxRate in taxRates) // { // if (taxClassGuid == taxRate.TaxClassGuid) // { // taxAmount += (taxRate.Rate * (itemPrice * quantity)); // break; // } // } // } // return taxAmount; //} public void CalculateShipping(Cart cart) { decimal shippingAmount = 0; foreach (CartOffer offer in cart.CartOffers) { Collection<OfferProduct> offerProducts = OfferProduct.GetbyOffer(offer.OfferGuid); if (offerProducts != null) { foreach (OfferProduct offerProduct in offerProducts) { Product product = new Product(offerProduct.ProductGuid); if (product != null) { shippingAmount += (product.ShippingAmount * offer.Quantity); } } } } cart.ShippingTotal = Math.Round(shippingAmount, this.RoundingDecimalPlaces, this.RoundingMode); cart.Save(); }