/// <summary> /// Prepare paged payment method list model /// </summary> /// <param name="searchModel">Payment method search model</param> /// <returns>Payment method list model</returns> public virtual PaymentMethodListModel PreparePaymentMethodListModel(PaymentMethodSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get payment methods var paymentMethods = _paymentPluginManager.LoadAllPlugins().ToPagedList(searchModel); //prepare grid model var model = new PaymentMethodListModel().PrepareToGrid(searchModel, paymentMethods, () => { return(paymentMethods.Select(method => { //fill in model values from the entity var paymentMethodModel = method.ToPluginModel <PaymentMethodModel>(); //fill in additional values (not existing in the entity) paymentMethodModel.IsActive = _paymentPluginManager.IsPluginActive(method); paymentMethodModel.ConfigurationUrl = method.GetConfigurationPageUrl(); paymentMethodModel.LogoUrl = _paymentPluginManager.GetPluginLogoUrl(method); paymentMethodModel.RecurringPaymentType = _localizationService.GetLocalizedEnum(method.RecurringPaymentType); return paymentMethodModel; })); }); return(model); }
/// <summary> /// Prepare payment method model /// </summary> /// <param name="cart">Cart</param> /// <param name="filterByCountryId">Filter by country identifier</param> /// <returns>Payment method model</returns> public virtual CheckoutPaymentMethodModel PreparePaymentMethodModel(IList <ShoppingCartItem> cart, int filterByCountryId) { var model = new CheckoutPaymentMethodModel(); //reward points if (_rewardPointsSettings.Enabled && !_shoppingCartService.ShoppingCartIsRecurring(cart)) { var rewardPointsBalance = _rewardPointService.GetRewardPointsBalance(_workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id); rewardPointsBalance = _rewardPointService.GetReducedPointsBalance(rewardPointsBalance); var rewardPointsAmountBase = _orderTotalCalculationService.ConvertRewardPointsToAmount(rewardPointsBalance); var rewardPointsAmount = _currencyService.ConvertFromPrimaryStoreCurrency(rewardPointsAmountBase, _workContext.WorkingCurrency); if (rewardPointsAmount > decimal.Zero && _orderTotalCalculationService.CheckMinimumRewardPointsToUseRequirement(rewardPointsBalance)) { model.DisplayRewardPoints = true; model.RewardPointsAmount = _priceFormatter.FormatPrice(rewardPointsAmount, true, false); model.RewardPointsBalance = rewardPointsBalance; //are points enough to pay for entire order? like if this option (to use them) was selected model.RewardPointsEnoughToPayForOrder = !_orderProcessingService.IsPaymentWorkflowRequired(cart, true); } } //filter by country var paymentMethods = _paymentPluginManager .LoadActivePlugins(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id, filterByCountryId) .Where(pm => pm.PaymentMethodType == PaymentMethodType.Standard || pm.PaymentMethodType == PaymentMethodType.Redirection) .Where(pm => !pm.HidePaymentMethod(cart)) .ToList(); foreach (var pm in paymentMethods) { if (_shoppingCartService.ShoppingCartIsRecurring(cart) && pm.RecurringPaymentType == RecurringPaymentType.NotSupported) { continue; } var pmModel = new CheckoutPaymentMethodModel.PaymentMethodModel { Name = _localizationService.GetLocalizedFriendlyName(pm, _workContext.WorkingLanguage.Id), Description = _paymentSettings.ShowPaymentMethodDescriptions ? pm.PaymentMethodDescription : string.Empty, PaymentMethodSystemName = pm.PluginDescriptor.SystemName, LogoUrl = _paymentPluginManager.GetPluginLogoUrl(pm) }; //payment method additional fee var paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(cart, pm.PluginDescriptor.SystemName); var rateBase = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, _workContext.CurrentCustomer); var rate = _currencyService.ConvertFromPrimaryStoreCurrency(rateBase, _workContext.WorkingCurrency); if (rate > decimal.Zero) { pmModel.Fee = _priceFormatter.FormatPaymentMethodAdditionalFee(rate, true); } model.PaymentMethods.Add(pmModel); } //find a selected (previously) payment method var selectedPaymentMethodSystemName = _genericAttributeService.GetAttribute <string>(_workContext.CurrentCustomer, NopCustomerDefaults.SelectedPaymentMethodAttribute, _storeContext.CurrentStore.Id); if (!string.IsNullOrEmpty(selectedPaymentMethodSystemName)) { var paymentMethodToSelect = model.PaymentMethods.ToList() .Find(pm => pm.PaymentMethodSystemName.Equals(selectedPaymentMethodSystemName, StringComparison.InvariantCultureIgnoreCase)); if (paymentMethodToSelect != null) { paymentMethodToSelect.Selected = true; } } //if no option has been selected, let's do it for the first one if (model.PaymentMethods.FirstOrDefault(so => so.Selected) == null) { var paymentMethodToSelect = model.PaymentMethods.FirstOrDefault(); if (paymentMethodToSelect != null) { paymentMethodToSelect.Selected = true; } } return(model); }