private void UpdatePayment(CheckoutViewModel viewModel) { viewModel.PaymentMethodViewModels = _paymentMethodViewModelFactory.GetPaymentMethodViewModels(); var selectedPaymentMethod = viewModel.Payment == null ? viewModel.PaymentMethodViewModels.First() : viewModel.PaymentMethodViewModels.Single(p => p.SystemName == viewModel.Payment.SystemName); viewModel.Payment = PaymentMethodViewModelResolver.Resolve(selectedPaymentMethod.SystemName, _orderFactory); viewModel.Payment.Description = selectedPaymentMethod.Description; viewModel.Payment.SystemName = selectedPaymentMethod.SystemName; viewModel.Payment.PaymentMethod.PaymentMethodId = selectedPaymentMethod.PaymentMethodId; }
public async Task <ActionResult> Index(CheckoutPage currentPage) { if (CartIsNullOrEmpty()) { return(View("EmptyCart")); } IPaymentMethodViewModel <PaymentMethodBase> paymentMethod = null; var selectedPaymentMethod = Request["paymentMethod"]; if (!string.IsNullOrEmpty(selectedPaymentMethod)) { paymentMethod = PaymentMethodViewModelResolver.Resolve(selectedPaymentMethod); paymentMethod.SystemName = selectedPaymentMethod; } var viewModel = CreateCheckoutViewModel(currentPage, paymentMethod); Cart.Currency = _currencyService.GetCurrentCurrency(); if (User.Identity.IsAuthenticated) { _checkoutService.UpdateShippingAddresses(Cart, viewModel); } _checkoutService.UpdateShippingMethods(Cart, viewModel.Shipments); _checkoutService.ApplyDiscounts(Cart); _orderRepository.Save(Cart); if (viewModel.Payment.SystemName.Equals(Klarna.Payments.Constants.KlarnaPaymentSystemKeyword)) { await _klarnaPaymentsService.CreateOrUpdateSession(Cart); (viewModel.Payment as KlarnaPaymentsViewModel)?.InitializeValues(); } if (viewModel.Payment.SystemName.Equals(Klarna.Checkout.Constants.KlarnaCheckoutSystemKeyword)) { _klarnaCheckoutService.CreateOrUpdateOrder(Cart); (viewModel.Payment as KlarnaCheckoutViewModel)?.InitializeValues(); } return(View(viewModel.ViewName, viewModel)); }
/// <summary> /// Creates and returns a new instance if a CheckoutViewModel. /// </summary> /// <param name="selectedPaymentMethod">The default payment method to be used for new purchases.</param> /// <param name="shippingMethodId">The default shipping method id.</param> /// <param name="customer">The currently logged on user. Will be null for anonymous quests.</param> /// <returns>A new CheckoutViewModel.</returns> private CheckoutViewModel CreateCheckoutViewModel(PaymentMethodViewModel <IPaymentOption> selectedPaymentMethod, Guid shippingMethodId, CustomerContact customer) { CheckoutViewModel viewModel = new CheckoutViewModel(); ShippingAddress billingAddress = new ShippingAddress { AddressId = customer != null ? customer.PreferredBillingAddressId : null, Name = _localizationService.GetString("/Shared/Address/NewAddress"), ShippingMethodId = shippingMethodId, HtmlFieldPrefix = _billingAddressPrefix }; // If the customer uses the same address for billing and shipment then we prepare a new empty address as shipping address // in case the customer chooses to not use the billing address after all. ShippingAddress shippingAddress = new ShippingAddress { AddressId = (customer != null && customer.PreferredShippingAddressId != customer.PreferredBillingAddressId) ? customer.PreferredShippingAddressId : null, Name = _localizationService.GetString("/Shared/Address/NewAddress"), ShippingMethodId = billingAddress.ShippingMethodId, HtmlFieldPrefix = _shippingAddressPrefix }; _addressBookService.LoadAddress(billingAddress); _addressBookService.LoadAddress(shippingAddress); viewModel = new CheckoutViewModel { BillingAddress = billingAddress, Payment = PaymentMethodViewModelResolver.Resolve(selectedPaymentMethod.SystemName), UseBillingAddressForShipment = (customer == null || (customer != null && customer.PreferredShippingAddressId == customer.PreferredBillingAddressId)), ShippingAddresses = new ShippingAddress[] { shippingAddress } }; viewModel.Payment.Description = selectedPaymentMethod.Description; viewModel.Payment.SystemName = selectedPaymentMethod.SystemName; ((PaymentMethodBase)viewModel.Payment.PaymentMethod).PaymentMethodId = selectedPaymentMethod.Id; return(viewModel); }