public OnlineSchedulingProcessAndCartViewModel Create(TempCart tempCart, Customer customer, EventSchedulingSlot appointmentSlot, string sponsoredBy, string checkoutPhoneNumber)
        {
            var model = new OnlineSchedulingProcessAndCartViewModel
            {
                CartGuid           = tempCart.Guid,
                PackageId          = tempCart.EventPackageId,
                AppointmentId      = tempCart.AppointmentId,
                CustomerId         = tempCart.CustomerId,
                CustomerName       = customer != null ? customer.NameAsString : "",
                SponsoredBy        = sponsoredBy,
                ProspectCustomerId = tempCart.ProspectCustomerId,
                EventId            = tempCart.EventId,
                Tests              = tempCart.TestId,
                Products           = tempCart.ProductId,
                IsExistingCustomer = tempCart.IsExistingCustomer,
                AppointmentTime    = appointmentSlot != null ? (DateTime?)appointmentSlot.StartTime : null,
                IsUsedAppointmentSlotExpiryExtension = tempCart.IsUsedAppointmentSlotExpiryExtension,
                ScreenResolution    = tempCart.ScreenResolution,
                CheckoutPhoneNumber = checkoutPhoneNumber
            };

            return(model);
        }
Example #2
0
        public OnlineSchedulingCustomerInfoEditModel SavePaymentInfo(OnlineSchedulingCustomerInfoEditModel model)
        {
            OnlineSchedulingProcessAndCartViewModel cartModel = model.ProcessAndCartViewModel;
            SourceCodeApplyEditModel sourceCodeModel          = model.SourceCodeApplyEditModel;
            PaymentEditModel         paymentModel             = model.PaymentEditModel;

            var onlineRequestValidationModel = _tempcartService.ValidateOnlineRequest(cartModel.CartGuid);

            model.RequestValidationModel = onlineRequestValidationModel;

            if (onlineRequestValidationModel.RequestStatus != OnlineRequestStatus.Valid)
            {
                return(model);
            }
            var tempCart = onlineRequestValidationModel.TempCart;


            if (tempCart.EventId.HasValue)
            {
                var summarymodel = _eventSchedulerService.GetEventCustomerOrderSummaryModel(tempCart);

                var couponAmount = sourceCodeModel != null ? sourceCodeModel.DiscountApplied : 0;
                if ((summarymodel.TotalPrice != null && summarymodel.TotalPrice.Value != (paymentModel.Amount + couponAmount)) || (summarymodel.TotalPrice == null && paymentModel.Amount > 0))
                {
                    throw new Exception("Seems like you changed your order. Please go back to the Package screen, and confirm!");
                }

                var result = ValidatePaymentModel(paymentModel);
                if (!result)
                {
                    throw new Exception("Payment error - Please re-enter your payment information or contact our customer care line at " +
                                        _settings.PhoneTollFree);
                }

                var isAppointmentTemporarilyBooked = _slotService.IsSlotTemporarilyBooked(tempCart.InChainAppointmentSlotIds);
                if (!isAppointmentTemporarilyBooked)
                {
                    //throw new Exception("The appointment time selected by you is no longer temporarliy booked for you. Please go back to appointment page to choose the time slot");
                    model.PaymentEditModel.IsTemporaryBookedSlotExpired = true;
                    return(model);
                }


                var isAppointmentBooked = _slotService.IsSlotBooked(tempCart.InChainAppointmentSlotIds);
                if (isAppointmentBooked)
                {
                    throw new Exception("The appointment slot selected by you seems to have exhausted. Please re-select another slot!");
                }
            }
            bool?    paymentSucceded = null;
            Customer customer;

            try
            {
                using (var scope = new TransactionScope())
                {
                    customer = tempCart.CustomerId.HasValue ? _customerRepository.GetCustomer(tempCart.CustomerId.Value) : null;

                    var address = Mapper.Map <AddressEditModel, Address>(paymentModel.ExistingShippingAddress);

                    address = _addressService.SaveAfterSanitizing(address, true);

                    if (customer == null)
                    {
                        throw new Exception("System Failure! Data not saved. Please try again.");
                    }

                    var customerAddress = Mapper.Map <AddressEditModel, Address>(paymentModel.ExistingShippingAddress);
                    customerAddress  = _addressService.SaveAfterSanitizing(customerAddress, true);
                    customer.Address = customerAddress;

                    customer.RequestForNewsLetter = model.RequestForNewsLetter;

                    if (paymentModel.Payments != null && paymentModel.Payments.Where(p => p.PaymentType != PaymentType.Onsite_Value && p.PaymentType != PaymentType.GiftCertificate.PersistenceLayerId).Count() > 0)
                    {
                        if (paymentModel.ExistingBillingAddress != null)
                        {
                            if (customer.BillingAddress != null && customer.BillingAddress.Id > 0)
                            {
                                paymentModel.ExistingBillingAddress.Id = customer.BillingAddress.Id;
                            }

                            var billingAddress = Mapper.Map <AddressEditModel, Address>(paymentModel.ExistingBillingAddress);
                            billingAddress = _addressService.SaveAfterSanitizing(billingAddress, true);

                            customer.BillingAddress = billingAddress;
                        }
                    }
                    else
                    {
                        var billingAddress = Mapper.Map <AddressEditModel, Address>(paymentModel.ExistingShippingAddress);
                        billingAddress = _addressService.SaveAfterSanitizing(billingAddress, true);

                        customer.BillingAddress = billingAddress;
                    }

                    _customerService.SaveCustomer(customer, customer.CustomerId);

                    tempCart.SourceCodeId = sourceCodeModel == null || sourceCodeModel.SourceCodeId < 1
                                                ? null
                                                : (long?)sourceCodeModel.SourceCodeId;

                    tempCart.ShippingAddressId = address.Id;
                    tempCart.ScreenResolution  = cartModel.ScreenResolution;

                    if (paymentModel.Payments != null)
                    {
                        tempCart.PaymentMode = "";

                        if (paymentModel.Payments.Any(p => p.ChargeCard != null))
                        {
                            tempCart.PaymentMode += PaymentType.CreditCard.Name + ",";
                        }

                        if (paymentModel.Payments.Any(p => p.ECheck != null))
                        {
                            tempCart.PaymentMode += PaymentType.ElectronicCheck.Name + ",";
                        }

                        if (paymentModel.Payments.Any(p => p.GiftCertificate != null))
                        {
                            tempCart.PaymentMode += PaymentType.GiftCertificate.Name + ",";
                        }

                        if (paymentModel.Payments.Any(p => p.PaymentType == (int)PaymentType.Onsite_Value))
                        {
                            tempCart.PaymentMode += PaymentType.PayLater_Text + ",";
                        }

                        tempCart.PaymentMode = tempCart.PaymentMode.Substring(0, tempCart.PaymentMode.LastIndexOf(","));
                    }

                    tempCart.PaymentAmount = paymentModel.Amount;
                    try
                    {
                        _eventSchedulerService.CreateOrder(tempCart, paymentModel);
                        paymentSucceded = true;
                    }
                    catch (Exception ex)
                    {
                        _paymentController.VoidCreditCardGatewayRequests(paymentModel);
                        paymentSucceded = false;
                        throw new Exception("\nOnline Payment. Message: " + ex.Message + ". \n\t Stack Trace: " + ex.StackTrace);
                    }

                    tempCart.IsPaymentSuccessful = true;
                    tempCart.IsCompleted         = true;
                    _tempcartService.SaveTempCart(tempCart);

                    UpdateProspectCustomer(tempCart, customer);
                    scope.Complete();
                }

                try
                {
                    var account = _corporateAccountRepository.GetbyEventId(tempCart.EventId.Value);

                    if (account == null || account.SendWelcomeEmail)
                    {
                        var welcomeEmailViewModel = _emailNotificationModelsFactory.GetWelcomeWithUserNameNotificationModel(customer.UserLogin.UserName, customer.Name.FullName, customer.DateCreated);
                        _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.CustomerWelcomeEmailWithUsername, EmailTemplateAlias.CustomerWelcomeEmailWithUsername, welcomeEmailViewModel, customer.Id, customer.CustomerId, Request.RequestUri.OriginalString);
                    }

                    var eventData = _eventRepository.GetById(tempCart.EventId.Value);

                    _customerRegistrationService.SendAppointmentConfirmationMail(customer, eventData, customer.CustomerId, Request.RequestUri.OriginalString, account);

                    //If somebody registered within 24 hours of the event Date then send notification.
                    if (eventData.EventDate.AddDays(-1).Date <= DateTime.Now.Date)
                    {
                        var appointmentBookedInTwentyFourHoursNotificationModel = _emailNotificationModelsFactory.GetAppointmentBookedInTwentyFourHoursModel(tempCart.EventId.Value, tempCart.CustomerId.Value);
                        _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.AppointmentBookedInTwentyFourHours, EmailTemplateAlias.AppointmentBookedInTwentyFourHours, appointmentBookedInTwentyFourHoursNotificationModel, 0, customer.CustomerId, Request.RequestUri.OriginalString);
                    }

                    _callQueueCustomerRepository.UpdateOtherCustomerStatus(customer.CustomerId, tempCart.ProspectCustomerId.HasValue ? tempCart.ProspectCustomerId.Value : 0, CallQueueStatus.Completed, customer.CustomerId);

                    _eventSchedulingSlotService.SendEventFillingNotification(tempCart.EventId.Value, customer.CustomerId);
                }
                catch (Exception)
                {
                    //throw  new Exception("\nOnline Payment. Exception caused while sending notification. Message: " + ex.Message);
                }
            }
            catch (InvalidAddressException ex)
            {
                throw new Exception("Address provided by you is not a valid address. " + ex.Message);
            }
            catch (Exception)
            {
                if (paymentSucceded != null && paymentSucceded == false)
                {
                    tempCart.IsPaymentSuccessful = false;
                    tempCart.IsCompleted         = false;
                    tempCart.PaymentMode         = "";
                    tempCart.SourceCodeId        = 0;
                    tempCart.ShippingAddressId   = null;
                    _tempcartService.SaveTempCart(tempCart);
                    throw new Exception("OOPS! Payment could not be settled. Please try again or Call our Care Agent at " + _settings.PhoneTollFree);
                }

                throw new Exception("OOPS! Some error while generating the Order. Please try again or Call our Care Agent at " + _settings.PhoneTollFree);
            }

            return(model);
        }