protected CheckoutPaymentMethodModel PreparePaymentMethodModel(IList<ShoppingCartItem> cart) { var model = new CheckoutPaymentMethodModel(); //reward points if (_rewardPointsSettings.Enabled && !cart.IsRecurring()) { int rewardPointsBalance = _workContext.CurrentCustomer.GetRewardPointsBalance(); decimal rewardPointsAmountBase = _orderTotalCalculationService.ConvertRewardPointsToAmount(rewardPointsBalance); decimal 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; } } var boundPaymentMethods = _paymentService .LoadActivePaymentMethods(_workContext.CurrentCustomer.Id) .Where(pm => pm.PaymentMethodType == PaymentMethodType.Standard || pm.PaymentMethodType == PaymentMethodType.Redirection) .ToList(); foreach (var pm in boundPaymentMethods) { if (cart.IsRecurring() && pm.RecurringPaymentType == RecurringPaymentType.NotSupported) continue; var pmModel = new CheckoutPaymentMethodModel.PaymentMethodModel() { Name = pm.GetLocalizedFriendlyName(_localizationService, _workContext.WorkingLanguage.Id), PaymentMethodSystemName = pm.PluginDescriptor.SystemName, }; //payment method additional fee decimal paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(cart, pm.PluginDescriptor.SystemName); decimal rateBase = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, _workContext.CurrentCustomer); decimal 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 = _workContext.CurrentCustomer.GetAttribute<string>( SystemCustomerAttributeNames.SelectedPaymentMethod, _genericAttributeService, _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; }
public ActionResult OpcSavePaymentMethod(FormCollection form) { try { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) throw new Exception("Your cart is empty"); if (!UseOnePageCheckout()) throw new Exception("One page checkout is disabled"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) throw new Exception("Anonymous checkout is not allowed"); string paymentmethod = form["paymentmethod"]; //payment method if (String.IsNullOrEmpty(paymentmethod)) throw new Exception("Selected payment method can't be parsed"); var model = new CheckoutPaymentMethodModel(); TryUpdateModel(model); //reward points if (_rewardPointsSettings.Enabled) { _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, model.UseRewardPoints, _storeContext.CurrentStore.Id); } //Check whether payment workflow is required bool isPaymentWorkflowRequired = IsPaymentWorkflowRequired(cart); if (!isPaymentWorkflowRequired) { //payment is not required _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, null, _storeContext.CurrentStore.Id); var confirmOrderModel = PrepareConfirmOrderModel(cart); return Json(new { update_section = new UpdateSectionJsonModel() { name = "confirm-order", html = this.RenderPartialViewToString("OpcConfirmOrder", confirmOrderModel) }, goto_section = "confirm_order" }); } var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings)) throw new Exception("Selected payment method can't be parsed"); //save _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, paymentmethod, _storeContext.CurrentStore.Id); var paymenInfoModel = PreparePaymentInfoModel(paymentMethodInst); return Json(new { update_section = new UpdateSectionJsonModel() { name = "payment-info", html = this.RenderPartialViewToString("OpcPaymentInfo", paymenInfoModel) }, goto_section = "payment_info" }); } catch (Exception exc) { _logger.Warning(exc.Message, exc, _workContext.CurrentCustomer); return Json(new { error = 1, message = exc.Message }); } }
public ActionResult SelectPaymentMethod(string paymentmethod, CheckoutPaymentMethodModel model) { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (UseOnePageCheckout()) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); //reward points if (_rewardPointsSettings.Enabled) { _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, model.UseRewardPoints, _storeContext.CurrentStore.Id); } //Check whether payment workflow is required bool isPaymentWorkflowRequired = IsPaymentWorkflowRequired(cart); if (!isPaymentWorkflowRequired) { _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, null, _storeContext.CurrentStore.Id); return RedirectToRoute("CheckoutPaymentInfo"); } //payment method if (String.IsNullOrEmpty(paymentmethod)) return PaymentMethod(); var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings)) return PaymentMethod(); //save _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, paymentmethod, _storeContext.CurrentStore.Id); return RedirectToRoute("CheckoutPaymentInfo"); }
public ActionResult OpcSavePaymentMethod(FormCollection form) { try { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (cart.Count == 0) throw new Exception("Your cart is empty"); if (!_orderSettings.OnePageCheckoutEnabled) throw new Exception("One page checkout is disabled"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) throw new Exception("Anonymous checkout is not allowed"); string paymentmethod = form["paymentmethod"]; //payment method if (String.IsNullOrEmpty(paymentmethod)) throw new Exception("Selected payment method can't be parsed"); var model = new CheckoutPaymentMethodModel(); TryUpdateModel(model); //reward points _workContext.CurrentCustomer.UseRewardPointsDuringCheckout = model.UseRewardPoints; _customerService.UpdateCustomer(_workContext.CurrentCustomer); var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings)) throw new Exception("Selected payment method can't be parsed"); //save _workContext.CurrentCustomer.SelectedPaymentMethodSystemName = paymentmethod; _customerService.UpdateCustomer(_workContext.CurrentCustomer); var paymenInfoModel = PreparePaymentInfoModel(paymentMethodInst); return Json(new { update_section = new UpdateSectionJsonModel() { name = "payment-info", html = RenderPartialViewToString("OpcPaymentInfo", paymenInfoModel) }, goto_section = "payment_info" }); } catch (Exception exc) { _logger.Warning(exc.Message, exc, _workContext.CurrentCustomer); return Json(new { error = 1, message = exc.Message }); } }
private CheckoutPaymentMethodModel PreparePaymentMethodModel(IList<ShoppingCartItem> cart) { var model = new CheckoutPaymentMethodModel(); //reward points if (_rewardPointsSettings.Enabled && !cart.IsRecurring()) { int rewardPointsBalance = _workContext.CurrentCustomer.GetRewardPointsBalance(); decimal rewardPointsAmountBase = _orderTotalCalculationService.ConvertRewardPointsToAmount(rewardPointsBalance); decimal rewardPointsAmount = _currencyService.ConvertFromPrimaryStoreCurrency(rewardPointsAmountBase, _workContext.WorkingCurrency); if (rewardPointsAmount > decimal.Zero) { model.DisplayRewardPoints = true; model.RewardPointsAmount = _priceFormatter.FormatPrice(rewardPointsAmount, true, false); model.RewardPointsBalance = rewardPointsBalance; } } var boundPaymentMethods = _paymentService .LoadActivePaymentMethods() .Where(pm => pm.PaymentMethodType == PaymentMethodType.Standard || pm.PaymentMethodType == PaymentMethodType.Redirection) .ToList(); foreach (var pm in boundPaymentMethods) { if (cart.IsRecurring() && pm.RecurringPaymentType == RecurringPaymentType.NotSupported) continue; var pmModel = new CheckoutPaymentMethodModel.PaymentMethodModel() { Name = pm.PluginDescriptor.FriendlyName, PaymentMethodSystemName = pm.PluginDescriptor.SystemName, }; //payment method additional fee decimal paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(pm.PluginDescriptor.SystemName); decimal rateBase = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, _workContext.CurrentCustomer); decimal rate = _currencyService.ConvertFromPrimaryStoreCurrency(rateBase, _workContext.WorkingCurrency); if (rate > decimal.Zero) pmModel.Fee = _priceFormatter.FormatPaymentMethodAdditionalFee(rate, true); model.PaymentMethods.Add(pmModel); } return model; }
public ActionResult SelectPaymentMethod(string paymentmethod, CheckoutPaymentMethodModel model) { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (_orderSettings.OnePageCheckoutEnabled) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); //Check whether payment workflow is required bool isPaymentWorkflowRequired = IsPaymentWorkflowRequired(cart); if (!isPaymentWorkflowRequired) { _workContext.CurrentCustomer.SelectedPaymentMethodSystemName = ""; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutPaymentInfo"); } //payment method if (String.IsNullOrEmpty(paymentmethod)) return PaymentMethod(); //reward points _workContext.CurrentCustomer.UseRewardPointsDuringCheckout = model.UseRewardPoints; _customerService.UpdateCustomer(_workContext.CurrentCustomer); var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings)) return PaymentMethod(); //save _workContext.CurrentCustomer.SelectedPaymentMethodSystemName = paymentmethod; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutPaymentInfo"); }
public ActionResult SelectPaymentMethod(string paymentmethod, CheckoutPaymentMethodModel model) { //validation //var cart = _workContext.CurrentCustomer.ShoppingCartItems // .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) // .LimitPerStore(_storeContext.CurrentStore.Id) // .ToList(); var gcCount = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart && sci.Product.AdminComment == "Gift Card") .LimitPerStore(_storeContext.CurrentStore.Id) .Count(); IList<ShoppingCartItem> cart = new List<ShoppingCartItem>(); if (gcCount != 0) { cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart && sci.Product.AdminComment == "Gift Card") .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); } else { cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); } if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (_orderSettings.OnePageCheckoutEnabled) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); //reward points if (_rewardPointsSettings.Enabled) { _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, model.UseRewardPoints, _storeContext.CurrentStore.Id); } //Check whether payment workflow is required bool isPaymentWorkflowRequired = IsPaymentWorkflowRequired(cart); if (!isPaymentWorkflowRequired) { _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, null, _storeContext.CurrentStore.Id); return RedirectToRoute("CheckoutPaymentInfo"); } //payment method if (String.IsNullOrEmpty(paymentmethod)) return PaymentMethod(); var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(paymentmethod); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings) || !_pluginFinder.AuthenticateStore(paymentMethodInst.PluginDescriptor, _storeContext.CurrentStore.Id)) return PaymentMethod(); //save _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, paymentmethod, _storeContext.CurrentStore.Id); return RedirectToRoute("CheckoutPaymentInfo"); }