Example #1
0
 private bool ValidateAddresses(CheckoutViewModel vm)
 {
     return(ValidateVM(vm.AsAddressesVM()));
 }
Example #2
0
        public ActionResult ReviewPOST(CheckoutViewModel model)
        {
            // redirect the user to their payment method of choice
            // is there any validation that should be happening here?
            var user = _workContextAccessor.GetContext().CurrentUser;

            if (!_checkoutHelperService.UserMayCheckout(user, out ActionResult redirect))
            {
                if (redirect != null)
                {
                    return(redirect);
                }
                return(Redirect(RedirectUrl));
            }
            if (string.IsNullOrWhiteSpace(model.SelectedPosService))
            {
                // the user selected no payment method
                _notifier.Error(T("Impossible to start payment with the selected provider. Please try again."));
                TempData["CheckoutViewModel"] = model;
                return(RedirectToAction("Review"));
            }
            // get the pos by name
            var selectedService = _posServices
                                  .FirstOrDefault(ps => ps.GetPosName()
                                                  .Equals(model.SelectedPosService, StringComparison.OrdinalIgnoreCase));

            if (selectedService == null)
            {
                // data got corrupted?
                _notifier.Error(T("Impossible to start payment with the selected provider. Please try again."));
                TempData["CheckoutViewModel"] = model;
                return(RedirectToAction("Review"));
            }
            // Re-validate the entire model to be safe
            ReinflateViewModelAddresses(model);
            // later we'll need the country and postal code
            var countryName = !string.IsNullOrWhiteSpace(model.ShippingAddressVM?.Country)
                ? model.ShippingAddressVM?.Country
                : (!string.IsNullOrWhiteSpace(model.BillingAddressVM?.Country)
                    ? model.BillingAddressVM?.Country
                    : "");

            _shoppingCart.Country = countryName;
            var postalCode = model.ShippingAddressVM != null
                ? model.ShippingAddressVM.PostalCode
                : model.BillingAddressVM.PostalCode;

            _shoppingCart.ZipCode = postalCode;
            // Validate ShippingOption
            model.ShippingRequired = IsShippingRequired();
            if (model.ShippingRequired && model.SelectedShippingOption == null)
            {
                if (string.IsNullOrWhiteSpace(model.ShippingOption))
                {
                    // TODO: manage this error condition
                    // Here we need a selected shipping method, but we don't have it somehow
                }

                // TODO: check this: we are reinflating from the model but maybe we have
                // this in _shoppingCart?
                var selectedOption = ShippingService.RebuildShippingOption(model.ShippingOption);
                _shoppingCart.ShippingOption = selectedOption;
                model.SelectedShippingOption = selectedOption;
            }
            // TODO: Validate Cart
            // Here we want to:
            // 1. Create the PayementGatewayCharge we'll use for events
            var paymentGuid = Guid.NewGuid().ToString();
            // 2. Create the Order ContentItem
            var order = _checkoutHelperService.CreateOrder(model.AsAddressesVM(), paymentGuid, countryName, postalCode);

            // 3. Don't attach the address from the Order to the Contact for
            //   the user, because that was done when inputing the address.
            // 3.1. If there is a User, we may wish to add their email and phone
            //   number to the Contact.
            // 4. Create the payment record for the Order.
            var reason  = string.Format("Purchase Order {0}", order.OrderKey);
            var payment = new PaymentRecord {
                Reason        = reason,
                Amount        = order.Total,
                Currency      = order.CurrencyCode,
                ContentItemId = order.Id
            };

            // 4.1. Invoke the StartPayment method for the selected IPosService.
            payment = selectedService.StartPayment(payment, paymentGuid);
            // 5. Get form the IPosService the controller URL and redirect there.
            // Put the model in TempData so it can be reused in the next action.
            TempData["CheckoutViewModel"] = model;
            return(Redirect(selectedService.GetPosActionUrl(payment.Guid)));
        }