//codehint: sm-add end public virtual void AddOrderTokens(IList<Token> tokens, Order order, int languageId) { tokens.Add(new Token("Order.OrderNumber", order.GetOrderNumber())); tokens.Add(new Token("Order.CustomerFullName", string.Format("{0} {1}", order.BillingAddress.FirstName, order.BillingAddress.LastName))); tokens.Add(new Token("Order.CustomerEmail", order.BillingAddress.Email)); tokens.Add(new Token("Order.BillingFirstName", order.BillingAddress.FirstName)); tokens.Add(new Token("Order.BillingLastName", order.BillingAddress.LastName)); tokens.Add(new Token("Order.BillingPhoneNumber", order.BillingAddress.PhoneNumber)); tokens.Add(new Token("Order.BillingEmail", order.BillingAddress.Email)); tokens.Add(new Token("Order.BillingFaxNumber", order.BillingAddress.FaxNumber)); tokens.Add(new Token("Order.BillingCompany", order.BillingAddress.Company)); tokens.Add(new Token("Order.BillingAddress1", order.BillingAddress.Address1)); tokens.Add(new Token("Order.BillingAddress2", order.BillingAddress.Address2)); tokens.Add(new Token("Order.BillingCity", order.BillingAddress.City)); tokens.Add(new Token("Order.BillingStateProvince", order.BillingAddress.StateProvince != null ? order.BillingAddress.StateProvince.GetLocalized(x => x.Name) : "")); tokens.Add(new Token("Order.BillingZipPostalCode", order.BillingAddress.ZipPostalCode)); tokens.Add(new Token("Order.BillingCountry", order.BillingAddress.Country != null ? order.BillingAddress.Country.GetLocalized(x => x.Name) : "")); tokens.Add(new Token("Order.ShippingMethod", order.ShippingMethod)); tokens.Add(new Token("Order.ShippingFirstName", order.ShippingAddress != null ? order.ShippingAddress.FirstName : "")); tokens.Add(new Token("Order.ShippingLastName", order.ShippingAddress != null ? order.ShippingAddress.LastName : "")); tokens.Add(new Token("Order.ShippingPhoneNumber", order.ShippingAddress != null ? order.ShippingAddress.PhoneNumber : "")); tokens.Add(new Token("Order.ShippingEmail", order.ShippingAddress != null ? order.ShippingAddress.Email : "")); tokens.Add(new Token("Order.ShippingFaxNumber", order.ShippingAddress != null ? order.ShippingAddress.FaxNumber : "")); tokens.Add(new Token("Order.ShippingCompany", order.ShippingAddress != null ? order.ShippingAddress.Company : "")); tokens.Add(new Token("Order.ShippingAddress1", order.ShippingAddress != null ? order.ShippingAddress.Address1 : "")); tokens.Add(new Token("Order.ShippingAddress2", order.ShippingAddress != null ? order.ShippingAddress.Address2 : "")); tokens.Add(new Token("Order.ShippingCity", order.ShippingAddress != null ? order.ShippingAddress.City : "")); tokens.Add(new Token("Order.ShippingStateProvince", order.ShippingAddress != null && order.ShippingAddress.StateProvince != null ? order.ShippingAddress.StateProvince.GetLocalized(x => x.Name) : "")); tokens.Add(new Token("Order.ShippingZipPostalCode", order.ShippingAddress != null ? order.ShippingAddress.ZipPostalCode : "")); tokens.Add(new Token("Order.ShippingCountry", order.ShippingAddress != null && order.ShippingAddress.Country != null ? order.ShippingAddress.Country.GetLocalized(x => x.Name) : "")); var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName); var paymentMethodName = paymentMethod != null ? GetLocalizedValue(paymentMethod.Metadata, "FriendlyName", x => x.FriendlyName) : order.PaymentMethodSystemName; tokens.Add(new Token("Order.PaymentMethod", paymentMethodName)); tokens.Add(new Token("Order.VatNumber", order.VatNumber)); tokens.Add(new Token("Order.Product(s)", ProductListToHtmlTable(order, languageId), true)); tokens.Add(new Token("Order.CustomerComment", order.CustomerOrderComment, true)); var language = _languageService.GetLanguageById(languageId); if (language != null && !String.IsNullOrEmpty(language.LanguageCulture)) { DateTime createdOn = _dateTimeHelper.ConvertToUserTime(order.CreatedOnUtc, TimeZoneInfo.Utc, _dateTimeHelper.GetCustomerTimeZone(order.Customer)); tokens.Add(new Token("Order.CreatedOn", createdOn.ToString("D", new CultureInfo(language.LanguageCulture)))); } else { tokens.Add(new Token("Order.CreatedOn", order.CreatedOnUtc.ToString("D"))); } //TODO add a method for getting URL (use routing because it handles all SEO friendly URLs) tokens.Add(new Token("Order.OrderURLForCustomer", string.Format("{0}order/details/{1}", _webHelper.GetStoreLocation(false), order.Id), true)); tokens.Add(new Token("Order.Disclaimer", TopicToHtml("Disclaimer", languageId), true)); tokens.Add(new Token("Order.ConditionsOfUse", TopicToHtml("ConditionsOfUse", languageId), true)); //event notification _eventPublisher.EntityTokensAdded(order, tokens); }
/// <summary> /// Reduce reward points /// </summary> /// <param name="order">Order</param> /// <param name="amount">The amount. OrderTotal is used if null.</param> protected void ReduceRewardPoints(Order order, decimal? amount = null) { if (!_rewardPointsSettings.Enabled) return; if (_rewardPointsSettings.PointsForPurchases_Amount <= decimal.Zero) return; //ensure that reward points were already earned for this order before if (!order.RewardPointsWereAdded) return; //Ensure that reward points are applied only to registered users if (order.Customer == null || order.Customer.IsGuest()) return; // Truncate increases the risk of inaccuracy of rounding //int points = (int)Math.Truncate((amount ?? order.OrderTotal) / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); int points = (int)Math.Round((amount ?? order.OrderTotal) / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); if (order.RewardPointsRemaining.HasValue && order.RewardPointsRemaining.Value < points) points = order.RewardPointsRemaining.Value; if (points == 0) return; //reduce reward points order.Customer.AddRewardPointsHistoryEntry(-points, string.Format(_localizationService.GetResource("RewardPoints.Message.ReducedForOrder"), order.GetOrderNumber())); if (!order.RewardPointsRemaining.HasValue) order.RewardPointsRemaining = (int)Math.Round(order.OrderTotal / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); order.RewardPointsRemaining = Math.Max(order.RewardPointsRemaining.Value - points, 0); _orderService.UpdateOrder(order); }
/// <summary> /// Capture an order (from admin panel) /// </summary> /// <param name="order">Order</param> /// <returns>A list of errors; empty list if no errors</returns> public virtual IList<string> Capture(Order order) { if (order == null) throw new ArgumentNullException("order"); if (!CanCapture(order)) throw new SmartException("Cannot do capture for order."); var request = new CapturePaymentRequest(); CapturePaymentResult result = null; try { //old info from placing order request.Order = order; result = _paymentService.Capture(request); if (result.Success) { var paidDate = order.PaidDateUtc; if (result.NewPaymentStatus == PaymentStatus.Paid) paidDate = DateTime.UtcNow; order.CaptureTransactionId = result.CaptureTransactionId; order.CaptureTransactionResult = result.CaptureTransactionResult; order.PaymentStatus = result.NewPaymentStatus; order.PaidDateUtc = paidDate; _orderService.UpdateOrder(order); //add a note order.OrderNotes.Add(new OrderNote() { Note = T("OrderCaptured"), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); CheckOrderStatus(order); //raise event if (order.PaymentStatus == PaymentStatus.Paid) { _eventPublisher.PublishOrderPaid(order); } } } catch (Exception exc) { if (result == null) result = new CapturePaymentResult(); result.AddError(string.Format("Error: {0}. Full exception: {1}", exc.Message, exc.ToString())); } //process errors string error = ""; for (int i = 0; i < result.Errors.Count; i++) { error += string.Format("Error {0}: {1}", i, result.Errors[i]); if (i != result.Errors.Count - 1) error += ". "; } if (!String.IsNullOrEmpty(error)) { //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format("Unable to capture order. {0}", error), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //log it string logError = string.Format(T("OrderCaptureError"), order.GetOrderNumber(), error); _logger.InsertLog(LogLevel.Error, logError, logError); } return result.Errors; }
/// <summary> /// Voids order (from admin panel) /// </summary> /// <param name="order">Order</param> /// <returns>Voided order</returns> public virtual IList<string> Void(Order order) { if (order == null) throw new ArgumentNullException("order"); if (!CanVoid(order)) throw new SmartException("Cannot do void for order."); var request = new VoidPaymentRequest(); VoidPaymentResult result = null; try { request.Order = order; result = _paymentService.Void(request); if (result.Success) { //update order info order.PaymentStatus = result.NewPaymentStatus; _orderService.UpdateOrder(order); //add a note order.OrderNotes.Add(new OrderNote() { Note = T("OrderVoided"), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //check order status CheckOrderStatus(order); } } catch (Exception exc) { if (result == null) result = new VoidPaymentResult(); result.AddError(string.Format("Error: {0}. Full exception: {1}", exc.Message, exc.ToString())); } //process errors string error = ""; for (int i = 0; i < result.Errors.Count; i++) { error += string.Format("Error {0}: {1}", i, result.Errors[i]); if (i != result.Errors.Count - 1) error += ". "; } if (!String.IsNullOrEmpty(error)) { //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format(T("OrderVoidError"), error), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //log it string logError = string.Format("Error voiding order '{0}'. Error: {1}", order.GetOrderNumber(), error); _logger.InsertLog(LogLevel.Error, logError, logError); } return result.Errors; }
/// <summary> /// Award reward points /// </summary> /// <param name="order">Order</param> /// <param name="amount">The amount. OrderTotal is used if null.</param> protected void AwardRewardPoints(Order order, decimal? amount = null) { if (!_rewardPointsSettings.Enabled) return; if (_rewardPointsSettings.PointsForPurchases_Amount <= decimal.Zero) return; //Ensure that reward points are applied only to registered users if (order.Customer == null || order.Customer.IsGuest()) return; //Ensure that reward points were not added before. We should not add reward points if they were already earned for this order if (order.RewardPointsWereAdded) return; // Truncate increases the risk of inaccuracy of rounding //int points = (int)Math.Truncate((amount ?? order.OrderTotal) / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); // why are points awarded for OrderTotal? wouldn't be OrderSubtotalInclTax better? int points = (int)Math.Round((amount ?? order.OrderTotal) / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); if (points == 0) return; //add reward points order.Customer.AddRewardPointsHistoryEntry(points, string.Format(_localizationService.GetResource("RewardPoints.Message.EarnedForOrder"), order.GetOrderNumber())); order.RewardPointsWereAdded = true; _orderService.UpdateOrder(order); }
/// <summary> /// Partially refunds an order (from admin panel) /// </summary> /// <param name="order">Order</param> /// <param name="amountToRefund">Amount to refund</param> /// <returns>A list of errors; empty list if no errors</returns> public virtual IList<string> PartiallyRefund(Order order, decimal amountToRefund) { if (order == null) throw new ArgumentNullException("order"); if (!CanPartiallyRefund(order, amountToRefund)) throw new SmartException("Cannot do partial refund for order."); var request = new RefundPaymentRequest(); RefundPaymentResult result = null; try { request.Order = order; request.AmountToRefund = amountToRefund; request.IsPartialRefund = true; result = _paymentService.Refund(request); if (result.Success) { //total amount refunded decimal totalAmountRefunded = order.RefundedAmount + amountToRefund; //update order info order.RefundedAmount = totalAmountRefunded; order.PaymentStatus = result.NewPaymentStatus; _orderService.UpdateOrder(order); //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format(T("OrderPartiallyRefunded"), _priceFormatter.FormatPrice(amountToRefund, true, false)), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //check order status CheckOrderStatus(order); } } catch (Exception exc) { if (result == null) result = new RefundPaymentResult(); result.AddError(string.Format("Error: {0}. Full exception: {1}", exc.Message, exc.ToString())); } //process errors string error = ""; for (int i = 0; i < result.Errors.Count; i++) { error += string.Format("Error {0}: {1}", i, result.Errors[i]); if (i != result.Errors.Count - 1) error += ". "; } if (!String.IsNullOrEmpty(error)) { //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format(T("OrderPartiallyRefundError"), error), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //log it string logError = string.Format("Error refunding order '{0}'. Error: {1}", order.GetOrderNumber(), error); _logger.InsertLog(LogLevel.Error, logError, logError); } return result.Errors; }
/// <summary> /// Places an order /// </summary> /// <param name="processPaymentRequest">Process payment request</param> /// <returns>Place order result</returns> public virtual PlaceOrderResult PlaceOrder(ProcessPaymentRequest processPaymentRequest, Dictionary<string, string> extraData) { //think about moving functionality of processing recurring orders (after the initial order was placed) to ProcessNextRecurringPayment() method if (processPaymentRequest == null) throw new ArgumentNullException("processPaymentRequest"); if (processPaymentRequest.OrderGuid == Guid.Empty) processPaymentRequest.OrderGuid = Guid.NewGuid(); var result = new PlaceOrderResult(); var utcNow = DateTime.UtcNow; try { #region Order details (customer, totals) //Recurring orders. Load initial order Order initialOrder = _orderService.GetOrderById(processPaymentRequest.InitialOrderId); if (processPaymentRequest.IsRecurringPayment) { if (initialOrder == null) throw new ArgumentException("Initial order is not set for recurring payment"); processPaymentRequest.PaymentMethodSystemName = initialOrder.PaymentMethodSystemName; } //customer var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId); if (customer == null) throw new ArgumentException("Customer is not set"); //affilites int affiliateId = 0; var affiliate = _affiliateService.GetAffiliateById(customer.AffiliateId); if (affiliate != null && affiliate.Active && !affiliate.Deleted) affiliateId = affiliate.Id; //customer currency string customerCurrencyCode = ""; decimal customerCurrencyRate = decimal.Zero; if (!processPaymentRequest.IsRecurringPayment) { var currencyTmp = _currencyService.GetCurrencyById(customer.GetAttribute<int>(SystemCustomerAttributeNames.CurrencyId, processPaymentRequest.StoreId)); var customerCurrency = (currencyTmp != null && currencyTmp.Published) ? currencyTmp : _workContext.WorkingCurrency; customerCurrencyCode = customerCurrency.CurrencyCode; var primaryStoreCurrency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId); customerCurrencyRate = customerCurrency.Rate / primaryStoreCurrency.Rate; } else { customerCurrencyCode = initialOrder.CustomerCurrencyCode; customerCurrencyRate = initialOrder.CurrencyRate; } //customer language Language customerLanguage = null; if (!processPaymentRequest.IsRecurringPayment) { customerLanguage = _languageService.GetLanguageById(customer.GetAttribute<int>( SystemCustomerAttributeNames.LanguageId, processPaymentRequest.StoreId)); } else { customerLanguage = _languageService.GetLanguageById(initialOrder.CustomerLanguageId); } if (customerLanguage == null || !customerLanguage.Published) customerLanguage = _workContext.WorkingLanguage; //check whether customer is guest if (customer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed) throw new SmartException("Anonymous checkout is not allowed"); var storeId = _storeContext.CurrentStore.Id; //load and validate customer shopping cart IList<OrganizedShoppingCartItem> cart = null; if (!processPaymentRequest.IsRecurringPayment) { //load shopping cart cart = customer.GetCartItems(ShoppingCartType.ShoppingCart, processPaymentRequest.StoreId); if (cart.Count == 0) throw new SmartException("Cart is empty"); //validate the entire shopping cart var warnings = _shoppingCartService.GetShoppingCartWarnings(cart, customer.GetAttribute<string>(SystemCustomerAttributeNames.CheckoutAttributes), true); if (warnings.Count > 0) { var warningsSb = new StringBuilder(); foreach (string warning in warnings) { warningsSb.Append(warning); warningsSb.Append(";"); } throw new SmartException(warningsSb.ToString()); } //validate individual cart items foreach (var sci in cart) { var sciWarnings = _shoppingCartService.GetShoppingCartItemWarnings(customer, sci.Item.ShoppingCartType, sci.Item.Product, processPaymentRequest.StoreId, sci.Item.AttributesXml, sci.Item.CustomerEnteredPrice, sci.Item.Quantity, false, childItems: sci.ChildItems); if (sciWarnings.Count > 0) { var warningsSb = new StringBuilder(); foreach (string warning in sciWarnings) { warningsSb.Append(warning); warningsSb.Append(";"); } throw new SmartException(warningsSb.ToString()); } } } //min totals validation if (!processPaymentRequest.IsRecurringPayment) { bool minOrderSubtotalAmountOk = ValidateMinOrderSubtotalAmount(cart); if (!minOrderSubtotalAmountOk) { decimal minOrderSubtotalAmount = _currencyService.ConvertFromPrimaryStoreCurrency(_orderSettings.MinOrderSubtotalAmount, _workContext.WorkingCurrency); throw new SmartException(string.Format(_localizationService.GetResource("Checkout.MinOrderSubtotalAmount"), _priceFormatter.FormatPrice(minOrderSubtotalAmount, true, false))); } bool minOrderTotalAmountOk = ValidateMinOrderTotalAmount(cart); if (!minOrderTotalAmountOk) { decimal minOrderTotalAmount = _currencyService.ConvertFromPrimaryStoreCurrency(_orderSettings.MinOrderTotalAmount, _workContext.WorkingCurrency); throw new SmartException(string.Format(_localizationService.GetResource("Checkout.MinOrderTotalAmount"), _priceFormatter.FormatPrice(minOrderTotalAmount, true, false))); } } //tax display type var customerTaxDisplayType = TaxDisplayType.IncludingTax; if (!processPaymentRequest.IsRecurringPayment) { customerTaxDisplayType = _workContext.GetTaxDisplayTypeFor(customer, processPaymentRequest.StoreId); } else { customerTaxDisplayType = initialOrder.CustomerTaxDisplayType; } //checkout attributes string checkoutAttributeDescription, checkoutAttributesXml; if (!processPaymentRequest.IsRecurringPayment) { checkoutAttributesXml = customer.GetAttribute<string>(SystemCustomerAttributeNames.CheckoutAttributes); checkoutAttributeDescription = _checkoutAttributeFormatter.FormatAttributes(checkoutAttributesXml, customer); } else { checkoutAttributesXml = initialOrder.CheckoutAttributesXml; checkoutAttributeDescription = initialOrder.CheckoutAttributeDescription; } //applied discount (used to store discount usage history) var appliedDiscounts = new List<Discount>(); //sub total decimal orderSubTotalInclTax, orderSubTotalExclTax; decimal orderSubTotalDiscountInclTax = 0, orderSubTotalDiscountExclTax = 0; if (!processPaymentRequest.IsRecurringPayment) { //sub total (incl tax) decimal orderSubTotalDiscountAmount1 = decimal.Zero; Discount orderSubTotalAppliedDiscount1 = null; decimal subTotalWithoutDiscountBase1 = decimal.Zero; decimal subTotalWithDiscountBase1 = decimal.Zero; _orderTotalCalculationService.GetShoppingCartSubTotal(cart, true, out orderSubTotalDiscountAmount1, out orderSubTotalAppliedDiscount1, out subTotalWithoutDiscountBase1, out subTotalWithDiscountBase1); orderSubTotalInclTax = subTotalWithoutDiscountBase1; orderSubTotalDiscountInclTax = orderSubTotalDiscountAmount1; //discount history if (orderSubTotalAppliedDiscount1 != null && !appliedDiscounts.ContainsDiscount(orderSubTotalAppliedDiscount1)) appliedDiscounts.Add(orderSubTotalAppliedDiscount1); //sub total (excl tax) decimal orderSubTotalDiscountAmount2 = decimal.Zero; Discount orderSubTotalAppliedDiscount2 = null; decimal subTotalWithoutDiscountBase2 = decimal.Zero; decimal subTotalWithDiscountBase2 = decimal.Zero; _orderTotalCalculationService.GetShoppingCartSubTotal(cart, false, out orderSubTotalDiscountAmount2, out orderSubTotalAppliedDiscount2, out subTotalWithoutDiscountBase2, out subTotalWithDiscountBase2); orderSubTotalExclTax = subTotalWithoutDiscountBase2; orderSubTotalDiscountExclTax = orderSubTotalDiscountAmount2; } else { orderSubTotalInclTax = initialOrder.OrderSubtotalInclTax; orderSubTotalExclTax = initialOrder.OrderSubtotalExclTax; } //shipping info bool shoppingCartRequiresShipping = false; if (!processPaymentRequest.IsRecurringPayment) { shoppingCartRequiresShipping = cart.RequiresShipping(); } else { shoppingCartRequiresShipping = initialOrder.ShippingStatus != ShippingStatus.ShippingNotRequired; } string shippingMethodName = "", shippingRateComputationMethodSystemName = ""; if (shoppingCartRequiresShipping) { if (!processPaymentRequest.IsRecurringPayment) { var shippingOption = customer.GetAttribute<ShippingOption>(SystemCustomerAttributeNames.SelectedShippingOption, processPaymentRequest.StoreId); if (shippingOption != null) { shippingMethodName = shippingOption.Name; shippingRateComputationMethodSystemName = shippingOption.ShippingRateComputationMethodSystemName; } } else { shippingMethodName = initialOrder.ShippingMethod; shippingRateComputationMethodSystemName = initialOrder.ShippingRateComputationMethodSystemName; } } //shipping total decimal? orderShippingTotalInclTax, orderShippingTotalExclTax = null; if (!processPaymentRequest.IsRecurringPayment) { decimal taxRate = decimal.Zero; Discount shippingTotalDiscount = null; orderShippingTotalInclTax = _orderTotalCalculationService.GetShoppingCartShippingTotal(cart, true, out taxRate, out shippingTotalDiscount); orderShippingTotalExclTax = _orderTotalCalculationService.GetShoppingCartShippingTotal(cart, false); if (!orderShippingTotalInclTax.HasValue || !orderShippingTotalExclTax.HasValue) throw new SmartException("Shipping total couldn't be calculated"); if (shippingTotalDiscount != null && !appliedDiscounts.ContainsDiscount(shippingTotalDiscount)) appliedDiscounts.Add(shippingTotalDiscount); } else { orderShippingTotalInclTax = initialOrder.OrderShippingInclTax; orderShippingTotalExclTax = initialOrder.OrderShippingExclTax; } //payment total decimal paymentAdditionalFeeInclTax, paymentAdditionalFeeExclTax; if (!processPaymentRequest.IsRecurringPayment) { decimal paymentAdditionalFee = _paymentService.GetAdditionalHandlingFee(cart, processPaymentRequest.PaymentMethodSystemName); paymentAdditionalFeeInclTax = _taxService.GetPaymentMethodAdditionalFee(paymentAdditionalFee, true, customer); paymentAdditionalFeeExclTax = _taxService.GetPaymentMethodAdditionalFee(paymentAdditionalFee, false, customer); } else { paymentAdditionalFeeInclTax = initialOrder.PaymentMethodAdditionalFeeInclTax; paymentAdditionalFeeExclTax = initialOrder.PaymentMethodAdditionalFeeExclTax; } //tax total decimal orderTaxTotal = decimal.Zero; string vatNumber = "", taxRates = ""; if (!processPaymentRequest.IsRecurringPayment) { //tax amount SortedDictionary<decimal, decimal> taxRatesDictionary = null; orderTaxTotal = _orderTotalCalculationService.GetTaxTotal(cart, out taxRatesDictionary); //VAT number var customerVatStatus = (VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId); if (_taxSettings.EuVatEnabled && customerVatStatus == VatNumberStatus.Valid) vatNumber = customer.GetAttribute<string>(SystemCustomerAttributeNames.VatNumber); //tax rates foreach (var kvp in taxRatesDictionary) { var taxRate = kvp.Key; var taxValue = kvp.Value; taxRates += string.Format("{0}:{1}; ", taxRate.ToString(CultureInfo.InvariantCulture), taxValue.ToString(CultureInfo.InvariantCulture)); } } else { orderTaxTotal = initialOrder.OrderTax; //VAT number vatNumber = initialOrder.VatNumber; } processPaymentRequest.OrderTax = orderTaxTotal; //order total (and applied discounts, gift cards, reward points) decimal? orderTotal = null; decimal orderDiscountAmount = decimal.Zero; List<AppliedGiftCard> appliedGiftCards = null; int redeemedRewardPoints = 0; decimal redeemedRewardPointsAmount = decimal.Zero; if (!processPaymentRequest.IsRecurringPayment) { Discount orderAppliedDiscount = null; orderTotal = _orderTotalCalculationService.GetShoppingCartTotal(cart, out orderDiscountAmount, out orderAppliedDiscount, out appliedGiftCards, out redeemedRewardPoints, out redeemedRewardPointsAmount); if (!orderTotal.HasValue) throw new SmartException("Order total couldn't be calculated"); //discount history if (orderAppliedDiscount != null && !appliedDiscounts.ContainsDiscount(orderAppliedDiscount)) appliedDiscounts.Add(orderAppliedDiscount); } else { orderDiscountAmount = initialOrder.OrderDiscount; orderTotal = initialOrder.OrderTotal; } processPaymentRequest.OrderTotal = orderTotal.Value; #endregion #region Addresses & pre payment workflow // give payment processor the opportunity to fullfill billing address var preProcessPaymentResult = _paymentService.PreProcessPayment(processPaymentRequest); if (!preProcessPaymentResult.Success) { foreach (var paymentError in preProcessPaymentResult.Errors) { result.AddError(string.Format("Payment error: {0}", paymentError)); } throw new SmartException("Error while pre-processing the payment"); } Address billingAddress = null; if (!processPaymentRequest.IsRecurringPayment) { if (customer.BillingAddress == null) throw new SmartException("Billing address is not provided"); if (!customer.BillingAddress.Email.IsEmail()) throw new SmartException("Email is not valid"); billingAddress = (Address)customer.BillingAddress.Clone(); } else { if (initialOrder.BillingAddress == null) throw new SmartException("Billing address is not available"); billingAddress = (Address)initialOrder.BillingAddress.Clone(); } if (billingAddress.Country != null && !billingAddress.Country.AllowsBilling) throw new SmartException(string.Format("Country '{0}' is not allowed for billing", billingAddress.Country.Name)); Address shippingAddress = null; if (shoppingCartRequiresShipping) { if (!processPaymentRequest.IsRecurringPayment) { if (customer.ShippingAddress == null) throw new SmartException("Shipping address is not provided"); if (!customer.ShippingAddress.Email.IsEmail()) throw new SmartException("Email is not valid"); shippingAddress = (Address)customer.ShippingAddress.Clone(); } else { if (initialOrder.ShippingAddress == null) throw new SmartException("Shipping address is not available"); shippingAddress = (Address)initialOrder.ShippingAddress.Clone(); } if (shippingAddress.Country != null && !shippingAddress.Country.AllowsShipping) throw new SmartException(string.Format("Country '{0}' is not allowed for shipping", shippingAddress.Country.Name)); } #endregion #region Payment workflow //skip payment workflow if order total equals zero bool skipPaymentWorkflow = false; if (orderTotal.Value == decimal.Zero) skipPaymentWorkflow = true; //payment workflow Provider<IPaymentMethod> paymentMethod = null; if (!skipPaymentWorkflow) { paymentMethod = _paymentService.LoadPaymentMethodBySystemName(processPaymentRequest.PaymentMethodSystemName); if (paymentMethod == null) throw new SmartException("Payment method couldn't be loaded"); //ensure that payment method is active if (!paymentMethod.IsPaymentMethodActive(_paymentSettings)) throw new SmartException("Payment method is not active"); } else processPaymentRequest.PaymentMethodSystemName = ""; //recurring or standard shopping cart? bool isRecurringShoppingCart = false; if (!processPaymentRequest.IsRecurringPayment) { isRecurringShoppingCart = cart.IsRecurring(); if (isRecurringShoppingCart) { int recurringCycleLength = 0; RecurringProductCyclePeriod recurringCyclePeriod; int recurringTotalCycles = 0; string recurringCyclesError = cart.GetRecurringCycleInfo(_localizationService, out recurringCycleLength, out recurringCyclePeriod, out recurringTotalCycles); if (!string.IsNullOrEmpty(recurringCyclesError)) throw new SmartException(recurringCyclesError); processPaymentRequest.RecurringCycleLength = recurringCycleLength; processPaymentRequest.RecurringCyclePeriod = recurringCyclePeriod; processPaymentRequest.RecurringTotalCycles = recurringTotalCycles; } } else { isRecurringShoppingCart = true; } //process payment ProcessPaymentResult processPaymentResult = null; if (!skipPaymentWorkflow) { if (!processPaymentRequest.IsRecurringPayment) { if (isRecurringShoppingCart) { //recurring cart var recurringPaymentType = _paymentService.GetRecurringPaymentType(processPaymentRequest.PaymentMethodSystemName); switch (recurringPaymentType) { case RecurringPaymentType.NotSupported: throw new SmartException("Recurring payments are not supported by selected payment method"); case RecurringPaymentType.Manual: case RecurringPaymentType.Automatic: processPaymentResult = _paymentService.ProcessRecurringPayment(processPaymentRequest); break; default: throw new SmartException("Not supported recurring payment type"); } } else { //standard cart processPaymentResult = _paymentService.ProcessPayment(processPaymentRequest); } } else { if (isRecurringShoppingCart) { //Old credit card info processPaymentRequest.CreditCardType = initialOrder.AllowStoringCreditCardNumber ? _encryptionService.DecryptText(initialOrder.CardType) : ""; processPaymentRequest.CreditCardName = initialOrder.AllowStoringCreditCardNumber ? _encryptionService.DecryptText(initialOrder.CardName) : ""; processPaymentRequest.CreditCardNumber = initialOrder.AllowStoringCreditCardNumber ? _encryptionService.DecryptText(initialOrder.CardNumber) : ""; //MaskedCreditCardNumber processPaymentRequest.CreditCardCvv2 = initialOrder.AllowStoringCreditCardNumber ? _encryptionService.DecryptText(initialOrder.CardCvv2) : ""; try { processPaymentRequest.CreditCardExpireMonth = initialOrder.AllowStoringCreditCardNumber ? Convert.ToInt32(_encryptionService.DecryptText(initialOrder.CardExpirationMonth)) : 0; processPaymentRequest.CreditCardExpireYear = initialOrder.AllowStoringCreditCardNumber ? Convert.ToInt32(_encryptionService.DecryptText(initialOrder.CardExpirationYear)) : 0; } catch {} var recurringPaymentType = _paymentService.GetRecurringPaymentType(processPaymentRequest.PaymentMethodSystemName); switch (recurringPaymentType) { case RecurringPaymentType.NotSupported: throw new SmartException("Recurring payments are not supported by selected payment method"); case RecurringPaymentType.Manual: processPaymentResult = _paymentService.ProcessRecurringPayment(processPaymentRequest); break; case RecurringPaymentType.Automatic: //payment is processed on payment gateway site processPaymentResult = new ProcessPaymentResult(); break; default: throw new SmartException("Not supported recurring payment type"); } } else { throw new SmartException("No recurring products"); } } } else { //payment is not required if (processPaymentResult == null) processPaymentResult = new ProcessPaymentResult(); processPaymentResult.NewPaymentStatus = PaymentStatus.Paid; } if (processPaymentResult == null) throw new SmartException("processPaymentResult is not available"); #endregion if (processPaymentResult.Success) { //save order in data storage //uncomment this line to support transactions //using (var scope = new System.Transactions.TransactionScope()) { #region Save order details var shippingStatus = ShippingStatus.NotYetShipped; if (!shoppingCartRequiresShipping) shippingStatus = ShippingStatus.ShippingNotRequired; var order = new Order() { StoreId = processPaymentRequest.StoreId, OrderGuid = processPaymentRequest.OrderGuid, CustomerId = customer.Id, CustomerLanguageId = customerLanguage.Id, CustomerTaxDisplayType = customerTaxDisplayType, CustomerIp = _webHelper.GetCurrentIpAddress(), OrderSubtotalInclTax = orderSubTotalInclTax, OrderSubtotalExclTax = orderSubTotalExclTax, OrderSubTotalDiscountInclTax = orderSubTotalDiscountInclTax, OrderSubTotalDiscountExclTax = orderSubTotalDiscountExclTax, OrderShippingInclTax = orderShippingTotalInclTax.Value, OrderShippingExclTax = orderShippingTotalExclTax.Value, PaymentMethodAdditionalFeeInclTax = paymentAdditionalFeeInclTax, PaymentMethodAdditionalFeeExclTax = paymentAdditionalFeeExclTax, TaxRates = taxRates, OrderTax = orderTaxTotal, OrderTotal = orderTotal.Value, RefundedAmount = decimal.Zero, OrderDiscount = orderDiscountAmount, CheckoutAttributeDescription = checkoutAttributeDescription, CheckoutAttributesXml = checkoutAttributesXml, CustomerCurrencyCode = customerCurrencyCode, CurrencyRate = customerCurrencyRate, AffiliateId = affiliateId, OrderStatus = OrderStatus.Pending, AllowStoringCreditCardNumber = processPaymentResult.AllowStoringCreditCardNumber, CardType = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardType) : string.Empty, CardName = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardName) : string.Empty, CardNumber = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardNumber) : string.Empty, MaskedCreditCardNumber = _encryptionService.EncryptText(_paymentService.GetMaskedCreditCardNumber(processPaymentRequest.CreditCardNumber)), CardCvv2 = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardCvv2) : string.Empty, CardExpirationMonth = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardExpireMonth.ToString()) : string.Empty, CardExpirationYear = processPaymentResult.AllowStoringCreditCardNumber ? _encryptionService.EncryptText(processPaymentRequest.CreditCardExpireYear.ToString()) : string.Empty, AllowStoringDirectDebit = processPaymentResult.AllowStoringDirectDebit, DirectDebitAccountHolder = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitAccountHolder) : string.Empty, DirectDebitAccountNumber = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitAccountNumber) : string.Empty, DirectDebitBankCode = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitBankCode) : string.Empty, DirectDebitBankName = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitBankName) : string.Empty, DirectDebitBIC = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitBic) : string.Empty, DirectDebitCountry = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitCountry) : string.Empty, DirectDebitIban = processPaymentResult.AllowStoringDirectDebit ? _encryptionService.EncryptText(processPaymentRequest.DirectDebitIban) : string.Empty, PaymentMethodSystemName = processPaymentRequest.PaymentMethodSystemName, AuthorizationTransactionId = processPaymentResult.AuthorizationTransactionId, AuthorizationTransactionCode = processPaymentResult.AuthorizationTransactionCode, AuthorizationTransactionResult = processPaymentResult.AuthorizationTransactionResult, CaptureTransactionId = processPaymentResult.CaptureTransactionId, CaptureTransactionResult = processPaymentResult.CaptureTransactionResult, SubscriptionTransactionId = processPaymentResult.SubscriptionTransactionId, PurchaseOrderNumber = processPaymentRequest.PurchaseOrderNumber, PaymentStatus = processPaymentResult.NewPaymentStatus, PaidDateUtc = null, BillingAddress = billingAddress, ShippingAddress = shippingAddress, ShippingStatus = shippingStatus, ShippingMethod = shippingMethodName, ShippingRateComputationMethodSystemName = shippingRateComputationMethodSystemName, VatNumber = vatNumber, CreatedOnUtc = utcNow, UpdatedOnUtc = utcNow, CustomerOrderComment = extraData.ContainsKey("CustomerComment") ? extraData["CustomerComment"] : "" }; _orderService.InsertOrder(order); result.PlacedOrder = order; if (!processPaymentRequest.IsRecurringPayment) { //move shopping cart items to order products foreach (var sc in cart) { //prices decimal taxRate = decimal.Zero; decimal scUnitPrice = _priceCalculationService.GetUnitPrice(sc, true); decimal scSubTotal = _priceCalculationService.GetSubTotal(sc, true); decimal scUnitPriceInclTax = _taxService.GetProductPrice(sc.Item.Product, scUnitPrice, true, customer, out taxRate); decimal scUnitPriceExclTax = _taxService.GetProductPrice(sc.Item.Product, scUnitPrice, false, customer, out taxRate); decimal scSubTotalInclTax = _taxService.GetProductPrice(sc.Item.Product, scSubTotal, true, customer, out taxRate); decimal scSubTotalExclTax = _taxService.GetProductPrice(sc.Item.Product, scSubTotal, false, customer, out taxRate); //discounts Discount scDiscount = null; decimal discountAmount = _priceCalculationService.GetDiscountAmount(sc, out scDiscount); decimal discountAmountInclTax = _taxService.GetProductPrice(sc.Item.Product, discountAmount, true, customer, out taxRate); decimal discountAmountExclTax = _taxService.GetProductPrice(sc.Item.Product, discountAmount, false, customer, out taxRate); if (scDiscount != null && !appliedDiscounts.ContainsDiscount(scDiscount)) appliedDiscounts.Add(scDiscount); //attributes string attributeDescription = _productAttributeFormatter.FormatAttributes(sc.Item.Product, sc.Item.AttributesXml, customer); var itemWeight = _shippingService.GetShoppingCartItemWeight(sc); //save order item var orderItem = new OrderItem() { OrderItemGuid = Guid.NewGuid(), Order = order, ProductId = sc.Item.ProductId, UnitPriceInclTax = scUnitPriceInclTax, UnitPriceExclTax = scUnitPriceExclTax, PriceInclTax = scSubTotalInclTax, PriceExclTax = scSubTotalExclTax, AttributeDescription = attributeDescription, AttributesXml = sc.Item.AttributesXml, Quantity = sc.Item.Quantity, DiscountAmountInclTax = discountAmountInclTax, DiscountAmountExclTax = discountAmountExclTax, DownloadCount = 0, IsDownloadActivated = false, LicenseDownloadId = 0, ItemWeight = itemWeight, ProductCost = _priceCalculationService.GetProductCost(sc.Item.Product, sc.Item.AttributesXml) }; if (sc.Item.Product.ProductType == ProductType.BundledProduct && sc.ChildItems != null) { var listBundleData = new List<ProductBundleItemOrderData>(); foreach (var childItem in sc.ChildItems) { decimal bundleItemSubTotal = _taxService.GetProductPrice(childItem.Item.Product, _priceCalculationService.GetSubTotal(childItem, true), out taxRate); string attributesInfo = _productAttributeFormatter.FormatAttributes(childItem.Item.Product, childItem.Item.AttributesXml, order.Customer, renderPrices: false, allowHyperlinks: false); childItem.BundleItemData.ToOrderData(listBundleData, bundleItemSubTotal, childItem.Item.AttributesXml, attributesInfo); } orderItem.SetBundleData(listBundleData); } order.OrderItems.Add(orderItem); _orderService.UpdateOrder(order); //gift cards if (sc.Item.Product.IsGiftCard) { string giftCardRecipientName, giftCardRecipientEmail, giftCardSenderName, giftCardSenderEmail, giftCardMessage; _productAttributeParser.GetGiftCardAttribute(sc.Item.AttributesXml, out giftCardRecipientName, out giftCardRecipientEmail, out giftCardSenderName, out giftCardSenderEmail, out giftCardMessage); for (int i = 0; i < sc.Item.Quantity; i++) { var gc = new GiftCard() { GiftCardType = sc.Item.Product.GiftCardType, PurchasedWithOrderItem = orderItem, Amount = scUnitPriceExclTax, IsGiftCardActivated = false, GiftCardCouponCode = _giftCardService.GenerateGiftCardCode(), RecipientName = giftCardRecipientName, RecipientEmail = giftCardRecipientEmail, SenderName = giftCardSenderName, SenderEmail = giftCardSenderEmail, Message = giftCardMessage, IsRecipientNotified = false, CreatedOnUtc = utcNow }; _giftCardService.InsertGiftCard(gc); } } //inventory _productService.AdjustInventory(sc, true); } //clear shopping cart cart.ToList().ForEach(sci => _shoppingCartService.DeleteShoppingCartItem(sci.Item, false)); } else { //recurring payment var initialOrderItems = initialOrder.OrderItems; foreach (var orderItem in initialOrderItems) { //save item var newOrderItem = new OrderItem() { OrderItemGuid = Guid.NewGuid(), Order = order, ProductId = orderItem.ProductId, UnitPriceInclTax = orderItem.UnitPriceInclTax, UnitPriceExclTax = orderItem.UnitPriceExclTax, PriceInclTax = orderItem.PriceInclTax, PriceExclTax = orderItem.PriceExclTax, AttributeDescription = orderItem.AttributeDescription, AttributesXml = orderItem.AttributesXml, Quantity = orderItem.Quantity, DiscountAmountInclTax = orderItem.DiscountAmountInclTax, DiscountAmountExclTax = orderItem.DiscountAmountExclTax, DownloadCount = 0, IsDownloadActivated = false, LicenseDownloadId = 0, ItemWeight = orderItem.ItemWeight, BundleData = orderItem.BundleData, ProductCost = orderItem.ProductCost }; order.OrderItems.Add(newOrderItem); _orderService.UpdateOrder(order); //gift cards if (orderItem.Product.IsGiftCard) { string giftCardRecipientName, giftCardRecipientEmail, giftCardSenderName, giftCardSenderEmail, giftCardMessage; _productAttributeParser.GetGiftCardAttribute(orderItem.AttributesXml, out giftCardRecipientName, out giftCardRecipientEmail, out giftCardSenderName, out giftCardSenderEmail, out giftCardMessage); for (int i = 0; i < orderItem.Quantity; i++) { var gc = new GiftCard() { GiftCardType = orderItem.Product.GiftCardType, PurchasedWithOrderItem = newOrderItem, Amount = orderItem.UnitPriceExclTax, IsGiftCardActivated = false, GiftCardCouponCode = _giftCardService.GenerateGiftCardCode(), RecipientName = giftCardRecipientName, RecipientEmail = giftCardRecipientEmail, SenderName = giftCardSenderName, SenderEmail = giftCardSenderEmail, Message = giftCardMessage, IsRecipientNotified = false, CreatedOnUtc = utcNow }; _giftCardService.InsertGiftCard(gc); } } //inventory _productService.AdjustInventory(orderItem, true, orderItem.Quantity); } } //discount usage history if (!processPaymentRequest.IsRecurringPayment) { foreach (var discount in appliedDiscounts) { var duh = new DiscountUsageHistory() { Discount = discount, Order = order, CreatedOnUtc = utcNow }; _discountService.InsertDiscountUsageHistory(duh); } } //gift card usage history if (!processPaymentRequest.IsRecurringPayment && appliedGiftCards != null) { foreach (var agc in appliedGiftCards) { decimal amountUsed = agc.AmountCanBeUsed; var gcuh = new GiftCardUsageHistory() { GiftCard = agc.GiftCard, UsedWithOrder = order, UsedValue = amountUsed, CreatedOnUtc = utcNow }; agc.GiftCard.GiftCardUsageHistory.Add(gcuh); _giftCardService.UpdateGiftCard(agc.GiftCard); } } //reward points history if (redeemedRewardPointsAmount > decimal.Zero) { customer.AddRewardPointsHistoryEntry(-redeemedRewardPoints, string.Format(_localizationService.GetResource("RewardPoints.Message.RedeemedForOrder", order.CustomerLanguageId), order.GetOrderNumber()), order, redeemedRewardPointsAmount); _customerService.UpdateCustomer(customer); } //recurring orders if (!processPaymentRequest.IsRecurringPayment && isRecurringShoppingCart) { //create recurring payment (the first payment) var rp = new RecurringPayment() { CycleLength = processPaymentRequest.RecurringCycleLength, CyclePeriod = processPaymentRequest.RecurringCyclePeriod, TotalCycles = processPaymentRequest.RecurringTotalCycles, StartDateUtc = utcNow, IsActive = true, CreatedOnUtc = utcNow, InitialOrder = order, }; _orderService.InsertRecurringPayment(rp); var recurringPaymentType = _paymentService.GetRecurringPaymentType(processPaymentRequest.PaymentMethodSystemName); switch (recurringPaymentType) { case RecurringPaymentType.NotSupported: { //not supported } break; case RecurringPaymentType.Manual: { //first payment var rph = new RecurringPaymentHistory() { RecurringPayment = rp, CreatedOnUtc = utcNow, OrderId = order.Id, }; rp.RecurringPaymentHistory.Add(rph); _orderService.UpdateRecurringPayment(rp); } break; case RecurringPaymentType.Automatic: { //will be created later (process is automated) } break; default: break; } } #endregion #region Notifications, notes and attributes //notes, messages order.OrderNotes.Add(new OrderNote() { Note = T("OrderPlaced"), DisplayToCustomer = false, CreatedOnUtc = utcNow }); _orderService.UpdateOrder(order); //send email notifications int orderPlacedStoreOwnerNotificationQueuedEmailId = _workflowMessageService.SendOrderPlacedStoreOwnerNotification(order, _localizationSettings.DefaultAdminLanguageId); if (orderPlacedStoreOwnerNotificationQueuedEmailId > 0) { order.OrderNotes.Add(new OrderNote() { Note = string.Format(T("MerchantEmailQueued"), orderPlacedStoreOwnerNotificationQueuedEmailId), DisplayToCustomer = false, CreatedOnUtc = utcNow }); _orderService.UpdateOrder(order); } int orderPlacedCustomerNotificationQueuedEmailId = _workflowMessageService.SendOrderPlacedCustomerNotification(order, order.CustomerLanguageId); if (orderPlacedCustomerNotificationQueuedEmailId > 0) { order.OrderNotes.Add(new OrderNote() { Note = string.Format(T("CustomerEmailQueued"), orderPlacedCustomerNotificationQueuedEmailId), DisplayToCustomer = false, CreatedOnUtc = utcNow }); _orderService.UpdateOrder(order); } //check order status CheckOrderStatus(order); //reset checkout data if (!processPaymentRequest.IsRecurringPayment) _customerService.ResetCheckoutData(customer, processPaymentRequest.StoreId, clearCouponCodes: true, clearCheckoutAttributes: true); // check for generic attributes to be inserted automatically foreach (var customProperty in processPaymentRequest.CustomProperties.Where(x => x.Key.HasValue() && x.Value.AutoCreateGenericAttribute)) { _genericAttributeService.SaveAttribute<object>(order, customProperty.Key, customProperty.Value.Value, order.StoreId); } //uncomment this line to support transactions //scope.Complete(); //raise event _eventPublisher.PublishOrderPlaced(order); if (!processPaymentRequest.IsRecurringPayment) { _customerActivityService.InsertActivity( "PublicStore.PlaceOrder", _localizationService.GetResource("ActivityLog.PublicStore.PlaceOrder"), order.GetOrderNumber()); } //raise event if (order.PaymentStatus == PaymentStatus.Paid) { _eventPublisher.PublishOrderPaid(order); } #endregion } } else { foreach (var paymentError in processPaymentResult.Errors) result.AddError(paymentError); } } catch (Exception exc) { _logger.Error(exc.Message, exc); result.AddError(exc.Message); } #region Process errors string error = ""; for (int i = 0; i < result.Errors.Count; i++) { error += string.Format("Error {0}: {1}", i + 1, result.Errors[i]); if (i != result.Errors.Count - 1) error += ". "; } if (!String.IsNullOrEmpty(error)) { //log it string logError = string.Format("Error while placing order. {0}", error); _logger.Error(logError); } #endregion return result; }
protected OrderDetailsModel PrepareOrderDetailsModel(Order order) { if (order == null) throw new ArgumentNullException("order"); var store = _storeService.GetStoreById(order.StoreId) ?? _services.StoreContext.CurrentStore; var orderSettings = _services.Settings.LoadSetting<OrderSettings>(store.Id); var catalogSettings = _services.Settings.LoadSetting<CatalogSettings>(store.Id); var taxSettings = _services.Settings.LoadSetting<TaxSettings>(store.Id); var pdfSettings = _services.Settings.LoadSetting<PdfSettings>(store.Id); var addressSettings = _services.Settings.LoadSetting<AddressSettings>(store.Id); var companyInfoSettings = _services.Settings.LoadSetting<CompanyInformationSettings>(store.Id); var model = new OrderDetailsModel(); model.MerchantCompanyInfo = companyInfoSettings; model.Id = order.Id; model.StoreId = order.StoreId; model.OrderNumber = order.GetOrderNumber(); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(order.CreatedOnUtc, DateTimeKind.Utc); model.OrderStatus = order.OrderStatus.GetLocalizedEnum(_services.Localization, _services.WorkContext); model.IsReOrderAllowed = orderSettings.IsReOrderAllowed; model.IsReturnRequestAllowed = _orderProcessingService.IsReturnRequestAllowed(order); model.DisplayPdfInvoice = pdfSettings.Enabled; model.RenderOrderNotes = pdfSettings.RenderOrderNotes; //shipping info model.ShippingStatus = order.ShippingStatus.GetLocalizedEnum(_services.Localization, _services.WorkContext); if (order.ShippingStatus != ShippingStatus.ShippingNotRequired) { model.IsShippable = true; model.ShippingAddress.PrepareModel(order.ShippingAddress, false, addressSettings); model.ShippingMethod = order.ShippingMethod; //shipments (only already shipped) var shipments = order.Shipments.Where(x => x.ShippedDateUtc.HasValue).OrderBy(x => x.CreatedOnUtc).ToList(); foreach (var shipment in shipments) { var shipmentModel = new OrderDetailsModel.ShipmentBriefModel { Id = shipment.Id, TrackingNumber = shipment.TrackingNumber, }; if (shipment.ShippedDateUtc.HasValue) shipmentModel.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc); if (shipment.DeliveryDateUtc.HasValue) shipmentModel.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc); model.Shipments.Add(shipmentModel); } } //billing info model.BillingAddress.PrepareModel(order.BillingAddress, false, addressSettings); //VAT number model.VatNumber = order.VatNumber; //payment method var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName); model.PaymentMethod = paymentMethod != null ? _pluginMediator.GetLocalizedFriendlyName(paymentMethod.Metadata) : order.PaymentMethodSystemName; model.CanRePostProcessPayment = _paymentService.CanRePostProcessPayment(order); //purchase order number (we have to find a better to inject this information because it's related to a certain plugin) if (paymentMethod != null && paymentMethod.Metadata.SystemName.Equals("Payments.PurchaseOrder", StringComparison.InvariantCultureIgnoreCase)) { model.DisplayPurchaseOrderNumber = true; model.PurchaseOrderNumber = order.PurchaseOrderNumber; } // totals switch (order.CustomerTaxDisplayType) { case TaxDisplayType.ExcludingTax: { //order subtotal var orderSubtotalExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubtotalExclTax, order.CurrencyRate); model.OrderSubtotal = _priceFormatter.FormatPrice(orderSubtotalExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, false, false); //discount (applied to order subtotal) var orderSubTotalDiscountExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubTotalDiscountExclTax, order.CurrencyRate); if (orderSubTotalDiscountExclTaxInCustomerCurrency > decimal.Zero) { model.OrderSubTotalDiscount = _priceFormatter.FormatPrice(-orderSubTotalDiscountExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, false, false); } //order shipping var orderShippingExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderShippingExclTax, order.CurrencyRate); model.OrderShipping = _priceFormatter.FormatShippingPrice(orderShippingExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, false, false); //payment method additional fee var paymentMethodAdditionalFeeExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.PaymentMethodAdditionalFeeExclTax, order.CurrencyRate); if (paymentMethodAdditionalFeeExclTaxInCustomerCurrency != decimal.Zero) model.PaymentMethodAdditionalFee = _priceFormatter.FormatPaymentMethodAdditionalFee(paymentMethodAdditionalFeeExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, false, false); } break; case TaxDisplayType.IncludingTax: { //order subtotal var orderSubtotalInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubtotalInclTax, order.CurrencyRate); model.OrderSubtotal = _priceFormatter.FormatPrice(orderSubtotalInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, true, false); //discount (applied to order subtotal) var orderSubTotalDiscountInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubTotalDiscountInclTax, order.CurrencyRate); if (orderSubTotalDiscountInclTaxInCustomerCurrency > decimal.Zero) { model.OrderSubTotalDiscount = _priceFormatter.FormatPrice(-orderSubTotalDiscountInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, true, false); } //order shipping var orderShippingInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderShippingInclTax, order.CurrencyRate); model.OrderShipping = _priceFormatter.FormatShippingPrice(orderShippingInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, true, false); //payment method additional fee var paymentMethodAdditionalFeeInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.PaymentMethodAdditionalFeeInclTax, order.CurrencyRate); if (paymentMethodAdditionalFeeInclTaxInCustomerCurrency != decimal.Zero) model.PaymentMethodAdditionalFee = _priceFormatter.FormatPaymentMethodAdditionalFee(paymentMethodAdditionalFeeInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, _services.WorkContext.WorkingLanguage, true, false); } break; } //tax bool displayTax = true; bool displayTaxRates = true; if (taxSettings.HideTaxInOrderSummary && order.CustomerTaxDisplayType == TaxDisplayType.IncludingTax) { displayTax = false; displayTaxRates = false; } else { if (order.OrderTax == 0 && taxSettings.HideZeroTax) { displayTax = false; displayTaxRates = false; } else { displayTaxRates = taxSettings.DisplayTaxRates && order.TaxRatesDictionary.Count > 0; displayTax = !displayTaxRates; var orderTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderTax, order.CurrencyRate); //TODO pass languageId to _priceFormatter.FormatPrice model.Tax = _priceFormatter.FormatPrice(orderTaxInCustomerCurrency, true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage); foreach (var tr in order.TaxRatesDictionary) { var rate = _priceFormatter.FormatTaxRate(tr.Key); var labelKey = "ShoppingCart.Totals.TaxRateLine" + (_services.WorkContext.TaxDisplayType == TaxDisplayType.IncludingTax ? "Incl" : "Excl"); model.TaxRates.Add(new OrderDetailsModel.TaxRate() { Rate = rate, Label = T(labelKey).Text.FormatCurrent(rate), //TODO pass languageId to _priceFormatter.FormatPrice Value = _priceFormatter.FormatPrice(_currencyService.ConvertCurrency(tr.Value, order.CurrencyRate), true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage), }); } } } model.DisplayTaxRates = displayTaxRates; model.DisplayTax = displayTax; //discount (applied to order total) var orderDiscountInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderDiscount, order.CurrencyRate); if (orderDiscountInCustomerCurrency > decimal.Zero) model.OrderTotalDiscount = _priceFormatter.FormatPrice(-orderDiscountInCustomerCurrency, true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage); //gift cards foreach (var gcuh in order.GiftCardUsageHistory) { model.GiftCards.Add(new OrderDetailsModel.GiftCard { CouponCode = gcuh.GiftCard.GiftCardCouponCode, Amount = _priceFormatter.FormatPrice(-(_currencyService.ConvertCurrency(gcuh.UsedValue, order.CurrencyRate)), true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage), }); } //reward points if (order.RedeemedRewardPointsEntry != null) { model.RedeemedRewardPoints = -order.RedeemedRewardPointsEntry.Points; model.RedeemedRewardPointsAmount = _priceFormatter.FormatPrice(-(_currencyService.ConvertCurrency(order.RedeemedRewardPointsEntry.UsedAmount, order.CurrencyRate)), true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage); } //total var orderTotalInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderTotal, order.CurrencyRate); model.OrderTotal = _priceFormatter.FormatPrice(orderTotalInCustomerCurrency, true, order.CustomerCurrencyCode, false, _services.WorkContext.WorkingLanguage); //checkout attributes model.CheckoutAttributeInfo = HtmlUtils.ConvertPlainTextToTable(HtmlUtils.ConvertHtmlToPlainText(order.CheckoutAttributeDescription)); //order notes foreach (var orderNote in order.OrderNotes .Where(on => on.DisplayToCustomer) .OrderByDescending(on => on.CreatedOnUtc) .ToList()) { model.OrderNotes.Add(new OrderDetailsModel.OrderNote { Note = orderNote.FormatOrderNoteText(), CreatedOn = _dateTimeHelper.ConvertToUserTime(orderNote.CreatedOnUtc, DateTimeKind.Utc) }); } //purchased products model.ShowSku = catalogSettings.ShowProductSku; var orderItems = _orderService.GetAllOrderItems(order.Id, null, null, null, null, null, null); foreach (var orderItem in orderItems) { var orderItemModel = PrepareOrderItemModel(order, orderItem); model.Items.Add(orderItemModel); } return model; }
private string GetEcommerceScript(Order order) { var googleAnalyticsSettings = _settingService.LoadSetting<GoogleAnalyticsSettings>(_storeContext.CurrentStore.Id); var usCulture = new CultureInfo("en-US"); string analyticsTrackingScript = ""; analyticsTrackingScript = googleAnalyticsSettings.TrackingScript + "\n"; analyticsTrackingScript = analyticsTrackingScript.Replace("{GOOGLEID}", googleAnalyticsSettings.GoogleId); string analyticsEcommerceScript = ""; if (order != null) { analyticsEcommerceScript = googleAnalyticsSettings.EcommerceScript + "\n"; analyticsEcommerceScript = analyticsEcommerceScript.Replace("{GOOGLEID}", googleAnalyticsSettings.GoogleId); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{ORDERID}", order.GetOrderNumber()); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{SITE}", _storeContext.CurrentStore.Url.Replace("http://", "").Replace("/", "")); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{TOTAL}", order.OrderTotal.ToString("0.00", usCulture)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{TAX}", order.OrderTax.ToString("0.00", usCulture)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{SHIP}", order.OrderShippingInclTax.ToString("0.00", usCulture)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{CITY}", order.BillingAddress == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.City)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{STATEPROVINCE}", order.BillingAddress == null || order.BillingAddress.StateProvince == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.StateProvince.Name)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{COUNTRY}", order.BillingAddress == null || order.BillingAddress.Country == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.Country.Name)); analyticsEcommerceScript = analyticsEcommerceScript.Replace("{CURRENCY}", order.CustomerCurrencyCode); var sb = new StringBuilder(); foreach (var item in order.OrderItems) { string analyticsEcommerceDetailScript = googleAnalyticsSettings.EcommerceDetailScript; //get category string categ = ""; var defaultProductCategory = _categoryService.GetProductCategoriesByProductId(item.ProductId).FirstOrDefault(); if (defaultProductCategory != null) categ = defaultProductCategory.Category.Name; analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{ORDERID}", order.GetOrderNumber()); //The SKU code is a required parameter for every item that is added to the transaction item.Product.MergeWithCombination(item.AttributesXml); analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTSKU}", FixIllegalJavaScriptChars(item.Product.Sku)); analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTNAME}", FixIllegalJavaScriptChars(item.Product.Name)); analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{CATEGORYNAME}", FixIllegalJavaScriptChars(categ)); analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{UNITPRICE}", item.UnitPriceInclTax.ToString("0.00", usCulture)); analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{QUANTITY}", item.Quantity.ToString()); sb.AppendLine(analyticsEcommerceDetailScript); } analyticsEcommerceScript = analyticsEcommerceScript.Replace("{DETAILS}", sb.ToString()); analyticsTrackingScript = analyticsTrackingScript.Replace("{ECOMMERCE}", analyticsEcommerceScript); } return analyticsTrackingScript; }
protected void PrepareOrderDetailsModel(OrderModel model, Order order) { if (order == null) throw new ArgumentNullException("order"); if (model == null) throw new ArgumentNullException("model"); var store = _storeService.GetStoreById(order.StoreId); var primaryStoreCurrency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId); model.Id = order.Id; model.OrderStatus = order.OrderStatus.GetLocalizedEnum(_localizationService, _workContext); model.OrderNumber = order.GetOrderNumber(); model.OrderGuid = order.OrderGuid; model.StoreName = (store != null ? store.Name : "".NaIfEmpty()); model.CustomerId = order.CustomerId; model.CustomerName = order.Customer.GetFullName(); model.CustomerIp = order.CustomerIp; model.VatNumber = order.VatNumber; model.CreatedOn = _dateTimeHelper.ConvertToUserTime(order.CreatedOnUtc, DateTimeKind.Utc); model.UpdatedOn = _dateTimeHelper.ConvertToUserTime(order.UpdatedOnUtc, DateTimeKind.Utc); model.DisplayPdfInvoice = _pdfSettings.Enabled; model.AllowCustomersToSelectTaxDisplayType = _taxSettings.AllowCustomersToSelectTaxDisplayType; model.TaxDisplayType = _taxSettings.TaxDisplayType; model.AffiliateId = order.AffiliateId; model.CustomerComment = order.CustomerOrderComment; if (order.AffiliateId != 0) { var affiliate = _affiliateService.GetAffiliateById(order.AffiliateId); if (affiliate != null && affiliate.Address != null) model.AffiliateFullName = affiliate.Address.GetFullName(); } #region Order totals //subtotal model.OrderSubtotalInclTax = _priceFormatter.FormatPrice(order.OrderSubtotalInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true); model.OrderSubtotalExclTax = _priceFormatter.FormatPrice(order.OrderSubtotalExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false); model.OrderSubtotalInclTaxValue = order.OrderSubtotalInclTax; model.OrderSubtotalExclTaxValue = order.OrderSubtotalExclTax; //discount (applied to order subtotal) string orderSubtotalDiscountInclTaxStr = _priceFormatter.FormatPrice(order.OrderSubTotalDiscountInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true); string orderSubtotalDiscountExclTaxStr = _priceFormatter.FormatPrice(order.OrderSubTotalDiscountExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false); if (order.OrderSubTotalDiscountInclTax > decimal.Zero) model.OrderSubTotalDiscountInclTax = orderSubtotalDiscountInclTaxStr; if (order.OrderSubTotalDiscountExclTax > decimal.Zero) model.OrderSubTotalDiscountExclTax = orderSubtotalDiscountExclTaxStr; model.OrderSubTotalDiscountInclTaxValue = order.OrderSubTotalDiscountInclTax; model.OrderSubTotalDiscountExclTaxValue = order.OrderSubTotalDiscountExclTax; //shipping model.OrderShippingInclTax = _priceFormatter.FormatShippingPrice(order.OrderShippingInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true); model.OrderShippingExclTax = _priceFormatter.FormatShippingPrice(order.OrderShippingExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false); model.OrderShippingInclTaxValue = order.OrderShippingInclTax; model.OrderShippingExclTaxValue = order.OrderShippingExclTax; //payment method additional fee if (order.PaymentMethodAdditionalFeeInclTax != decimal.Zero) { model.PaymentMethodAdditionalFeeInclTax = _priceFormatter.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true); model.PaymentMethodAdditionalFeeExclTax = _priceFormatter.FormatPaymentMethodAdditionalFee(order.PaymentMethodAdditionalFeeExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false); } model.PaymentMethodAdditionalFeeInclTaxValue = order.PaymentMethodAdditionalFeeInclTax; model.PaymentMethodAdditionalFeeExclTaxValue = order.PaymentMethodAdditionalFeeExclTax; //tax model.Tax = _priceFormatter.FormatPrice(order.OrderTax, true, false); SortedDictionary<decimal, decimal> taxRates = order.TaxRatesDictionary; bool displayTaxRates = _taxSettings.DisplayTaxRates && taxRates.Count > 0; bool displayTax = !displayTaxRates; foreach (var tr in order.TaxRatesDictionary) { model.TaxRates.Add(new OrderModel.TaxRate() { Rate = _priceFormatter.FormatTaxRate(tr.Key), Value = _priceFormatter.FormatPrice(tr.Value, true, false), }); } model.DisplayTaxRates = displayTaxRates; model.DisplayTax = displayTax; model.TaxValue = order.OrderTax; model.TaxRatesValue = order.TaxRates; //discount if (order.OrderDiscount > 0) model.OrderTotalDiscount = _priceFormatter.FormatPrice(-order.OrderDiscount, true, false); model.OrderTotalDiscountValue = order.OrderDiscount; //gift cards foreach (var gcuh in order.GiftCardUsageHistory) { model.GiftCards.Add(new OrderModel.GiftCard() { CouponCode = gcuh.GiftCard.GiftCardCouponCode, Amount = _priceFormatter.FormatPrice(-gcuh.UsedValue, true, false), }); } //reward points if (order.RedeemedRewardPointsEntry != null) { model.RedeemedRewardPoints = -order.RedeemedRewardPointsEntry.Points; model.RedeemedRewardPointsAmount = _priceFormatter.FormatPrice(-order.RedeemedRewardPointsEntry.UsedAmount, true, false); } //total model.OrderTotal = _priceFormatter.FormatPrice(order.OrderTotal, true, false); model.OrderTotalValue = order.OrderTotal; //refunded amount if (order.RefundedAmount > decimal.Zero) model.RefundedAmount = _priceFormatter.FormatPrice(order.RefundedAmount, true, false); #endregion #region Payment info if (order.AllowStoringCreditCardNumber) { //card type model.CardType = _encryptionService.DecryptText(order.CardType); //cardholder name model.CardName = _encryptionService.DecryptText(order.CardName); //card number model.CardNumber = _encryptionService.DecryptText(order.CardNumber); //cvv model.CardCvv2 = _encryptionService.DecryptText(order.CardCvv2); //expiry date string cardExpirationMonthDecrypted = _encryptionService.DecryptText(order.CardExpirationMonth); if (!String.IsNullOrEmpty(cardExpirationMonthDecrypted) && cardExpirationMonthDecrypted != "0") model.CardExpirationMonth = cardExpirationMonthDecrypted; string cardExpirationYearDecrypted = _encryptionService.DecryptText(order.CardExpirationYear); if (!String.IsNullOrEmpty(cardExpirationYearDecrypted) && cardExpirationYearDecrypted != "0") model.CardExpirationYear = cardExpirationYearDecrypted; model.AllowStoringCreditCardNumber = true; } else { string maskedCreditCardNumberDecrypted = _encryptionService.DecryptText(order.MaskedCreditCardNumber); if (!String.IsNullOrEmpty(maskedCreditCardNumberDecrypted)) model.CardNumber = maskedCreditCardNumberDecrypted; } if (order.AllowStoringDirectDebit) { model.DirectDebitAccountHolder = _encryptionService.DecryptText(order.DirectDebitAccountHolder); model.DirectDebitAccountNumber = _encryptionService.DecryptText(order.DirectDebitAccountNumber); model.DirectDebitBankCode = _encryptionService.DecryptText(order.DirectDebitBankCode); model.DirectDebitBankName = _encryptionService.DecryptText(order.DirectDebitBankName); model.DirectDebitBIC = _encryptionService.DecryptText(order.DirectDebitBIC); model.DirectDebitCountry = _encryptionService.DecryptText(order.DirectDebitCountry); model.DirectDebitIban = _encryptionService.DecryptText(order.DirectDebitIban); model.AllowStoringDirectDebit = true; } //purchase order number (we have to find a better to inject this information because it's related to a certain plugin) var pm = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName); if (pm != null) { if (pm.Metadata.SystemName.Equals("Payments.PurchaseOrder", StringComparison.InvariantCultureIgnoreCase)) { model.DisplayPurchaseOrderNumber = true; model.PurchaseOrderNumber = order.PurchaseOrderNumber; } model.PaymentMethod = _pluginMediator.GetLocalizedFriendlyName(pm.Metadata); model.PaymentMethodSystemName = order.PaymentMethodSystemName; } else { model.PaymentMethod = order.PaymentMethodSystemName; } //payment transaction info model.AuthorizationTransactionId = order.AuthorizationTransactionId; model.CaptureTransactionId = order.CaptureTransactionId; model.SubscriptionTransactionId = order.SubscriptionTransactionId; model.AuthorizationTransactionResult = order.AuthorizationTransactionResult; model.CaptureTransactionResult = order.CaptureTransactionResult; //payment method info model.PaymentStatus = order.PaymentStatus.GetLocalizedEnum(_localizationService, _workContext); //payment method buttons model.CanCancelOrder = _orderProcessingService.CanCancelOrder(order); model.CanCompleteOrder = _orderProcessingService.CanCompleteOrder(order); model.CanCapture = _orderProcessingService.CanCapture(order); model.CanMarkOrderAsPaid = _orderProcessingService.CanMarkOrderAsPaid(order); model.CanRefund = _orderProcessingService.CanRefund(order); model.CanRefundOffline = _orderProcessingService.CanRefundOffline(order); model.CanPartiallyRefund = _orderProcessingService.CanPartiallyRefund(order, decimal.Zero); model.CanPartiallyRefundOffline = _orderProcessingService.CanPartiallyRefundOffline(order, decimal.Zero); model.CanVoid = _orderProcessingService.CanVoid(order); model.CanVoidOffline = _orderProcessingService.CanVoidOffline(order); model.MaxAmountToRefund = order.OrderTotal - order.RefundedAmount; model.MaxAmountToRefundFormatted = _priceFormatter.FormatPrice(model.MaxAmountToRefund, true, primaryStoreCurrency, _workContext.WorkingLanguage, false, false); //recurring payment record var recurringPayment = _orderService.SearchRecurringPayments(0, 0, order.Id, null, true).FirstOrDefault(); if (recurringPayment != null) { model.RecurringPaymentId = recurringPayment.Id; } #endregion #region Billing & shipping info model.BillingAddress = order.BillingAddress.ToModel(); model.BillingAddress.FirstNameEnabled = true; model.BillingAddress.FirstNameRequired = true; model.BillingAddress.LastNameEnabled = true; model.BillingAddress.LastNameRequired = true; model.BillingAddress.EmailEnabled = true; model.BillingAddress.EmailRequired = true; model.BillingAddress.CompanyEnabled = _addressSettings.CompanyEnabled; model.BillingAddress.CompanyRequired = _addressSettings.CompanyRequired; model.BillingAddress.CountryEnabled = _addressSettings.CountryEnabled; model.BillingAddress.StateProvinceEnabled = _addressSettings.StateProvinceEnabled; model.BillingAddress.CityEnabled = _addressSettings.CityEnabled; model.BillingAddress.CityRequired = _addressSettings.CityRequired; model.BillingAddress.StreetAddressEnabled = _addressSettings.StreetAddressEnabled; model.BillingAddress.StreetAddressRequired = _addressSettings.StreetAddressRequired; model.BillingAddress.StreetAddress2Enabled = _addressSettings.StreetAddress2Enabled; model.BillingAddress.StreetAddress2Required = _addressSettings.StreetAddress2Required; model.BillingAddress.ZipPostalCodeEnabled = _addressSettings.ZipPostalCodeEnabled; model.BillingAddress.ZipPostalCodeRequired = _addressSettings.ZipPostalCodeRequired; model.BillingAddress.PhoneEnabled = _addressSettings.PhoneEnabled; model.BillingAddress.PhoneRequired = _addressSettings.PhoneRequired; model.BillingAddress.FaxEnabled = _addressSettings.FaxEnabled; model.BillingAddress.FaxRequired = _addressSettings.FaxRequired; model.ShippingStatus = order.ShippingStatus.GetLocalizedEnum(_localizationService, _workContext); ; if (order.ShippingStatus != ShippingStatus.ShippingNotRequired) { model.IsShippable = true; model.ShippingAddress = order.ShippingAddress.ToModel(); model.ShippingAddress.FirstNameEnabled = true; model.ShippingAddress.FirstNameRequired = true; model.ShippingAddress.LastNameEnabled = true; model.ShippingAddress.LastNameRequired = true; model.ShippingAddress.EmailEnabled = true; model.ShippingAddress.EmailRequired = true; model.ShippingAddress.CompanyEnabled = _addressSettings.CompanyEnabled; model.ShippingAddress.CompanyRequired = _addressSettings.CompanyRequired; model.ShippingAddress.CountryEnabled = _addressSettings.CountryEnabled; model.ShippingAddress.StateProvinceEnabled = _addressSettings.StateProvinceEnabled; model.ShippingAddress.CityEnabled = _addressSettings.CityEnabled; model.ShippingAddress.CityRequired = _addressSettings.CityRequired; model.ShippingAddress.StreetAddressEnabled = _addressSettings.StreetAddressEnabled; model.ShippingAddress.StreetAddressRequired = _addressSettings.StreetAddressRequired; model.ShippingAddress.StreetAddress2Enabled = _addressSettings.StreetAddress2Enabled; model.ShippingAddress.StreetAddress2Required = _addressSettings.StreetAddress2Required; model.ShippingAddress.ZipPostalCodeEnabled = _addressSettings.ZipPostalCodeEnabled; model.ShippingAddress.ZipPostalCodeRequired = _addressSettings.ZipPostalCodeRequired; model.ShippingAddress.PhoneEnabled = _addressSettings.PhoneEnabled; model.ShippingAddress.PhoneRequired = _addressSettings.PhoneRequired; model.ShippingAddress.FaxEnabled = _addressSettings.FaxEnabled; model.ShippingAddress.FaxRequired = _addressSettings.FaxRequired; model.ShippingMethod = order.ShippingMethod; model.ShippingAddressGoogleMapsUrl = string.Format("http://maps.google.com/maps?f=q&hl=en&ie=UTF8&oe=UTF8&geocode=&q={0}", Server.UrlEncode(order.ShippingAddress.Address1 + " " + order.ShippingAddress.ZipPostalCode + " " + order.ShippingAddress.City + " " + (order.ShippingAddress.Country != null ? order.ShippingAddress.Country.Name : ""))); model.CanAddNewShipments = order.HasItemsToAddToShipment(); } #endregion #region Products model.CheckoutAttributeInfo = HtmlUtils.ConvertPlainTextToTable(HtmlUtils.ConvertHtmlToPlainText(order.CheckoutAttributeDescription)); //model.CheckoutAttributeInfo = order.CheckoutAttributeDescription; //model.CheckoutAttributeInfo = _checkoutAttributeFormatter.FormatAttributes(_workContext.CurrentCustomer.CheckoutAttributes, _workContext.CurrentCustomer, "", false); bool hasDownloadableItems = false; foreach (var orderItem in order.OrderItems) { if (orderItem.Product.IsDownload) hasDownloadableItems = true; orderItem.Product.MergeWithCombination(orderItem.AttributesXml); var orderItemModel = new OrderModel.OrderItemModel() { Id = orderItem.Id, ProductId = orderItem.ProductId, ProductName = orderItem.Product.GetLocalized(x => x.Name), Sku = orderItem.Product.Sku, ProductType = orderItem.Product.ProductType, ProductTypeName = orderItem.Product.GetProductTypeLabel(_localizationService), ProductTypeLabelHint = orderItem.Product.ProductTypeLabelHint, Quantity = orderItem.Quantity, IsDownload = orderItem.Product.IsDownload, DownloadCount = orderItem.DownloadCount, DownloadActivationType = orderItem.Product.DownloadActivationType, IsDownloadActivated = orderItem.IsDownloadActivated, LicenseDownloadId = orderItem.LicenseDownloadId }; if (orderItem.Product.ProductType == ProductType.BundledProduct && orderItem.BundleData.HasValue()) { var bundleData = orderItem.GetBundleData(); orderItemModel.BundlePerItemPricing = orderItem.Product.BundlePerItemPricing; orderItemModel.BundlePerItemShoppingCart = bundleData.Any(x => x.PerItemShoppingCart); foreach (var bundleItem in bundleData) { var bundleItemModel = new OrderModel.BundleItemModel() { ProductId = bundleItem.ProductId, Sku = bundleItem.Sku, ProductName = bundleItem.ProductName, ProductSeName = bundleItem.ProductSeName, VisibleIndividually = bundleItem.VisibleIndividually, Quantity = bundleItem.Quantity, DisplayOrder = bundleItem.DisplayOrder, AttributeInfo = bundleItem.AttributesInfo }; if (orderItemModel.BundlePerItemShoppingCart) { bundleItemModel.PriceWithDiscount = _priceFormatter.FormatPrice(bundleItem.PriceWithDiscount, true, primaryStoreCurrency, _workContext.WorkingLanguage, false); } orderItemModel.BundleItems.Add(bundleItemModel); } } //unit price orderItemModel.UnitPriceInclTaxValue = orderItem.UnitPriceInclTax; orderItemModel.UnitPriceExclTaxValue = orderItem.UnitPriceExclTax; orderItemModel.UnitPriceInclTax = _priceFormatter.FormatPrice(orderItem.UnitPriceInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true, true); orderItemModel.UnitPriceExclTax = _priceFormatter.FormatPrice(orderItem.UnitPriceExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false, true); //discounts orderItemModel.DiscountInclTaxValue = orderItem.DiscountAmountInclTax; orderItemModel.DiscountExclTaxValue = orderItem.DiscountAmountExclTax; orderItemModel.DiscountInclTax = _priceFormatter.FormatPrice(orderItem.DiscountAmountInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true, true); orderItemModel.DiscountExclTax = _priceFormatter.FormatPrice(orderItem.DiscountAmountExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false, true); //subtotal orderItemModel.SubTotalInclTaxValue = orderItem.PriceInclTax; orderItemModel.SubTotalExclTaxValue = orderItem.PriceExclTax; orderItemModel.SubTotalInclTax = _priceFormatter.FormatPrice(orderItem.PriceInclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, true, true); orderItemModel.SubTotalExclTax = _priceFormatter.FormatPrice(orderItem.PriceExclTax, true, primaryStoreCurrency, _workContext.WorkingLanguage, false, true); orderItemModel.AttributeInfo = orderItem.AttributeDescription; if (orderItem.Product.IsRecurring) { orderItemModel.RecurringInfo = string.Format(_localizationService.GetResource("Admin.Orders.Products.RecurringPeriod"), orderItem.Product.RecurringCycleLength, orderItem.Product.RecurringCyclePeriod.GetLocalizedEnum(_localizationService, _workContext)); } //return requests orderItemModel.ReturnRequests = _orderService.SearchReturnRequests(0, 0, orderItem.Id, null, 0, int.MaxValue).Select(x => { return new OrderModel.ReturnRequestModel() { Id = x.Id, Quantity = x.Quantity, Status = x.ReturnRequestStatus, StatusString = x.ReturnRequestStatus.GetLocalizedEnum(_localizationService, _workContext) }; }) .ToList(); //gift cards orderItemModel.PurchasedGiftCardIds = _giftCardService.GetGiftCardsByPurchasedWithOrderItemId(orderItem.Id) .Select(gc => gc.Id).ToList(); model.Items.Add(orderItemModel); } model.HasDownloadableProducts = hasDownloadableItems; model.AutoUpdateOrderItem.Caption = _localizationService.GetResource("Admin.Orders.EditOrderDetails"); model.AutoUpdateOrderItem.ShowUpdateTotals = (order.OrderStatusId <= (int)OrderStatus.Pending); // UpdateRewardPoints only visible for unpending orders (see RewardPointsSettingsValidator). model.AutoUpdateOrderItem.ShowUpdateRewardPoints = (order.OrderStatusId > (int)OrderStatus.Pending && order.RewardPointsWereAdded); model.AutoUpdateOrderItem.UpdateTotals = model.AutoUpdateOrderItem.ShowUpdateTotals; model.AutoUpdateOrderItem.UpdateRewardPoints = order.RewardPointsWereAdded; model.AutoUpdateOrderItemInfo = TempData[AutoUpdateOrderItemContext.InfoKey] as string; #endregion }
/// <summary> /// Award reward points /// </summary> /// <param name="order">Order</param> protected void ReduceRewardPoints(Order order) { if (!_rewardPointsSettings.Enabled) return; if (_rewardPointsSettings.PointsForPurchases_Amount <= decimal.Zero) return; //Ensure that reward points are applied only to registered users if (order.Customer == null || order.Customer.IsGuest()) return; int points = (int)Math.Truncate(order.OrderTotal / _rewardPointsSettings.PointsForPurchases_Amount * _rewardPointsSettings.PointsForPurchases_Points); if (points == 0) return; //ensure that reward points were already earned for this order before if (!order.RewardPointsWereAdded) return; //reduce reward points order.Customer.AddRewardPointsHistoryEntry(-points, string.Format(_localizationService.GetResource("RewardPoints.Message.ReducedForOrder"), order.GetOrderNumber())); _orderService.UpdateOrder(order); }
private void ProcessErrors(Order order, IList<string> errors, string messageKey) { if (errors.Any()) { var msg = string.Concat(T(messageKey, order.GetOrderNumber()), " ", string.Join(" ", errors)); _orderService.AddOrderNote(order, msg); _logger.InsertLog(LogLevel.Error, msg, msg); } }