public object Put(AccountUpdateRequest accountUpdateRequest)
        {
            Guid accountId = accountUpdateRequest.AccountId;
            var  request   = accountUpdateRequest.BookingSettingsRequest;

            AccountDetail existingEmailAccountDetail = _accountDao.FindByEmail(request.Email);
            AccountDetail currentAccountDetail       = _accountDao.FindById(accountId);

            if (currentAccountDetail.Email != request.Email && currentAccountDetail.FacebookId.HasValue())
            {
                throw new HttpError(HttpStatusCode.BadRequest, _resources.Get("EmailChangeWithFacebookAccountErrorMessage"));
            }

            if (existingEmailAccountDetail != null && existingEmailAccountDetail.Email == request.Email && existingEmailAccountDetail.Id != accountId)
            {
                throw new HttpError(HttpStatusCode.BadRequest, ErrorCode.EmailAlreadyUsed.ToString(), _resources.Get("EmailUsedMessage"));
            }

            CountryCode countryCode = CountryCode.GetCountryCodeByIndex(CountryCode.GetCountryCodeIndexByCountryISOCode(request.Country));

            if (PhoneHelper.IsPossibleNumber(countryCode, request.Phone))
            {
                request.Phone = PhoneHelper.GetDigitsFromPhoneNumber(request.Phone);
            }
            else
            {
                throw new HttpError(string.Format(_resources.Get("PhoneNumberFormat"), countryCode.GetPhoneExample()));
            }

            var isChargeAccountEnabled = _serverSettings.GetPaymentSettings().IsChargeAccountPaymentEnabled;

            // Validate account number if charge account is enabled and account number is set.
            if (isChargeAccountEnabled && !string.IsNullOrWhiteSpace(request.AccountNumber))
            {
                if (!request.CustomerNumber.HasValue())
                {
                    throw new HttpError(HttpStatusCode.Forbidden, ErrorCode.AccountCharge_InvalidAccountNumber.ToString());
                }

                // Validate locally that the account exists
                var account = _accountChargeDao.FindByAccountNumber(request.AccountNumber);
                if (account == null)
                {
                    throw new HttpError(HttpStatusCode.Forbidden, ErrorCode.AccountCharge_InvalidAccountNumber.ToString());
                }

                // Validate with IBS to make sure the account/customer is still active
                var ibsChargeAccount = _ibsServiceProvider.ChargeAccount().GetIbsAccount(request.AccountNumber, request.CustomerNumber);
                if (!ibsChargeAccount.IsValid())
                {
                    throw new HttpError(HttpStatusCode.Forbidden, ErrorCode.AccountCharge_InvalidAccountNumber.ToString());
                }
            }

            var command = new UpdateBookingSettings();

            Mapper.Map(request, command);

            command.AccountId = accountId;

            _commandBus.Send(command);

            return(new HttpResult(HttpStatusCode.OK));
        }
Example #2
0
        public async Task <bool> ValidateRideSettings()
        {
            if (!EmailHelper.IsEmail(Email))
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["InvalidEmailTitle"], this.Services().Localize["InvalidEmailMessage"]);

                return(false);
            }

            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Phone))
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"], this.Services().Localize["UpdateBookingSettingsEmptyField"]);

                return(false);
            }

            if (!PhoneNumber.IsNumberPossible())
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"],
                                                          string.Format(this.Services().Localize["InvalidPhoneErrorMessage"], PhoneNumber.GetPhoneExample()));

                return(false);
            }

            if (ChargeTypeId == ChargeTypes.Account.Id && string.IsNullOrWhiteSpace(AccountNumber) && string.IsNullOrWhiteSpace(CustomerNumber))
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"], this.Services().Localize["UpdateBookingSettingsEmptyAccount"]);

                return(false);
            }

            if (Settings.IsPayBackRegistrationFieldRequired == true && !PayBack.HasValue())
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"], this.Services().Localize["NoPayBackErrorMessage"]);

                return(false);
            }

            if (PayBack.HasValue() && (PayBack.Length > 10 || !PayBack.IsNumber()))
            {
                await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"], this.Services().Localize["InvalidPayBackErrorMessage"]);

                return(false);
            }

            // PayBack value is set to string empty if the field is left empty by the user
            _bookingSettings.PayBack = _bookingSettings.PayBack == string.Empty ? null : _bookingSettings.PayBack;

            Phone = PhoneHelper.GetDigitsFromPhoneNumber(Phone);

            if (ChargeTypeId == ChargeTypes.Account.Id)
            {
                var creditCard = PaymentPreferences.SelectedCreditCardId == Guid.Empty
                                        ? default(Guid?)
                                 : PaymentPreferences.SelectedCreditCardId;

                try
                {
                    // Validate if the charge account needs to have a card on file to be used
                    var chargeAccount = await _accountPaymentService.GetAccountCharge(AccountNumber, CustomerNumber);

                    if (chargeAccount.UseCardOnFileForPayment && creditCard == default(Guid?))
                    {
                        await this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"],
                                                                  this.Services().Localize["UpdateBookingSettingsInvalidCoF"]);

                        return(false);
                    }
                }
                catch
                {
                    this.Services().Message.ShowMessage(this.Services().Localize["UpdateBookingSettingsInvalidDataTitle"],
                                                        this.Services().Localize["UpdateBookingSettingsInvalidAccount"]).HandleErrors();
                    return(false);
                }
            }

            return(true);
        }
Example #3
0
        protected CreateOrder BuildCreateOrderCommand(CreateOrderRequest request, AccountDetail account, CreateReportOrder createReportOrder)
        {
            _logger.LogMessage("Create order request : " + request);

            if (request.Settings.Country == null || !request.Settings.Country.Code.HasValueTrimmed())
            {
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable,
                                     string.Format(_resources.Get("PhoneNumberCountryNotProvided", request.ClientLanguageCode)));
            }

            var countryCode = CountryCode.GetCountryCodeByIndex(CountryCode.GetCountryCodeIndexByCountryISOCode(request.Settings.Country));

            if (PhoneHelper.IsPossibleNumber(countryCode, request.Settings.Phone))
            {
                request.Settings.Phone = PhoneHelper.GetDigitsFromPhoneNumber(request.Settings.Phone);
            }
            else
            {
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable,
                                     string.Format(_resources.Get("PhoneNumberFormat", request.ClientLanguageCode), countryCode.GetPhoneExample()));
            }

            // TODO MKTAXI-3576: Find a better way to do this...
            var isFromWebApp = request.FromWebApp;

            if (!isFromWebApp)
            {
                ValidateAppVersion(request.ClientLanguageCode, createReportOrder);
            }

            // Find market
            var marketSettings = _taxiHailNetworkServiceClient.GetCompanyMarketSettings(request.PickupAddress.Latitude, request.PickupAddress.Longitude);
            var market         = marketSettings.Market.HasValue() ? marketSettings.Market : null;

            createReportOrder.Market = market;

            var isFutureBooking = IsFutureBooking(request.PickupDate, marketSettings);

            if (!marketSettings.EnableFutureBooking && isFutureBooking)
            {
                // future booking not allowed
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable, _resources.Get("CannotCreateOrder_FutureBookingNotAllowed", request.ClientLanguageCode));
            }

            BestAvailableCompany bestAvailableCompany;

            if (request.OrderCompanyKey.HasValue() || request.OrderFleetId.HasValue)
            {
                // For API user, it's possible to manually specify which company to dispatch to by using a fleet id
                bestAvailableCompany = _taxiHailNetworkHelper.FindSpecificCompany(market, createReportOrder, request.OrderCompanyKey, request.OrderFleetId, request.PickupAddress.Latitude, request.PickupAddress.Longitude);
            }
            else
            {
                bestAvailableCompany = _taxiHailNetworkHelper.FindBestAvailableCompany(marketSettings, request.PickupAddress.Latitude, request.PickupAddress.Longitude, isFutureBooking);
            }

            _logger.LogMessage("Best available company determined: {0}, in {1}",
                               bestAvailableCompany.CompanyKey.HasValue() ? bestAvailableCompany.CompanyKey : "local company",
                               market.HasValue() ? market : "local market");

            createReportOrder.CompanyKey  = bestAvailableCompany.CompanyKey;
            createReportOrder.CompanyName = bestAvailableCompany.CompanyName;

            if (market.HasValue())
            {
                if (!bestAvailableCompany.CompanyKey.HasValue())
                {
                    // No companies available that are desserving this region for the company
                    ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable, _resources.Get("CannotCreateOrder_NoCompanies", request.ClientLanguageCode));
                }

                _taxiHailNetworkHelper.UpdateVehicleTypeFromMarketData(request.Settings, bestAvailableCompany.CompanyKey);
                var isConfiguredForCmtPayment = _taxiHailNetworkHelper.FetchCompanyPaymentSettings(bestAvailableCompany.CompanyKey);

                if (!isConfiguredForCmtPayment)
                {
                    // Only companies configured for CMT payment can support CoF orders outside of home market
                    request.Settings.ChargeTypeId = ChargeTypes.PaymentInCar.Id;
                }

                if (marketSettings.DisableOutOfAppPayment && request.Settings.ChargeTypeId == ChargeTypes.PaymentInCar.Id)
                {
                    // No payment method available since we can't pay in car
                    ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_NoChargeType, _resources.Get("CannotCreateOrder_NoChargeType", request.ClientLanguageCode));
                }
            }

            var isPaypal    = request.Settings.ChargeTypeId == ChargeTypes.PayPal.Id;
            var isBraintree = (request.Settings.ChargeTypeId == ChargeTypes.CardOnFile.Id) && (_serverSettings.GetPaymentSettings().PaymentMode == PaymentMethod.Braintree);

            var isPrepaid = isFromWebApp && (isPaypal || isBraintree);

            createReportOrder.IsPrepaid = isPrepaid;

            account.IBSAccountId = CreateIbsAccountIfNeeded(account, bestAvailableCompany.CompanyKey);

            var pickupDate = request.PickupDate ?? GetCurrentOffsetedTime(bestAvailableCompany.CompanyKey);

            createReportOrder.PickupDate = pickupDate;

            // User can still create future order, but we allow only one active Book now order.
            if (!isFutureBooking)
            {
                var pendingOrderId = GetPendingOrder();

                // We don't allow order creation if there's already an order scheduled
                if (!_serverSettings.ServerData.AllowSimultaneousAppOrders &&
                    pendingOrderId != null &&
                    !isFromWebApp)
                {
                    ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_PendingOrder, pendingOrderId.ToString());
                }
            }

            var rule = _ruleCalculator.GetActiveDisableFor(
                isFutureBooking,
                pickupDate,
                () =>
                _ibsServiceProvider.StaticData(bestAvailableCompany.CompanyKey)
                .GetZoneByCoordinate(
                    request.Settings.ProviderId,
                    request.PickupAddress.Latitude,
                    request.PickupAddress.Longitude),
                () => request.DropOffAddress != null
                    ? _ibsServiceProvider.StaticData(bestAvailableCompany.CompanyKey)
                .GetZoneByCoordinate(
                    request.Settings.ProviderId,
                    request.DropOffAddress.Latitude,
                    request.DropOffAddress.Longitude)
                        : null,
                market, new Position(request.PickupAddress.Latitude, request.PickupAddress.Longitude));

            if (rule != null)
            {
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable, rule.Message);
            }

            // We need to validate the rules of the roaming market.
            if (market.HasValue())
            {
                // External market, query company site directly to validate their rules
                var orderServiceClient = new RoamingValidationServiceClient(bestAvailableCompany.CompanyKey, _serverSettings.ServerData.Target);

                _logger.LogMessage(string.Format("Validating rules for company in external market... Target: {0}, Server: {1}", _serverSettings.ServerData.Target, orderServiceClient.Url));

                var validationResult = orderServiceClient.ValidateOrder(request, true);
                if (validationResult.HasError)
                {
                    ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_RuleDisable, validationResult.Message);
                }
            }

            if (Params.Get(request.Settings.Name, request.Settings.Phone).Any(p => p.IsNullOrEmpty()))
            {
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_SettingsRequired);
            }

            var referenceData = (ReferenceData)_referenceDataService.Get(new ReferenceDataRequest {
                CompanyKey = bestAvailableCompany.CompanyKey
            });

            request.PickupDate = pickupDate;

            request.Settings.Passengers = request.Settings.Passengers <= 0
                ? 1
                : request.Settings.Passengers;

            if (_serverSettings.ServerData.Direction.NeedAValidTarif &&
                (!request.Estimate.Price.HasValue || request.Estimate.Price == 0))
            {
                ThrowAndLogException(createReportOrder, ErrorCode.CreateOrder_NoFareEstimateAvailable,
                                     GetCreateOrderServiceErrorMessage(ErrorCode.CreateOrder_NoFareEstimateAvailable, request.ClientLanguageCode));
            }

            // IBS provider validation
            ValidateProvider(request, referenceData, market.HasValue(), createReportOrder);

            // Map the command to obtain a OrderId (web doesn't prepopulate it in the request)
            var orderCommand = Mapper.Map <Commands.CreateOrder>(request);

            _logger.LogMessage("MarketSettings for order {0}: {1}", orderCommand.OrderId, marketSettings.ToJson());

            var marketFees = _feesDao.GetMarketFees(market);

            orderCommand.BookingFees          = marketFees != null ? marketFees.Booking : 0;
            createReportOrder.BookingFees     = orderCommand.BookingFees;
            createReportOrder.AssignVehicleId = orderCommand.AssignVehicleId;

            // Promo code validation
            var promotionId = ValidatePromotion(bestAvailableCompany.CompanyKey, request.PromoCode, request.Settings.ChargeTypeId, account.Id, pickupDate, isFutureBooking, request.ClientLanguageCode, createReportOrder);

            // Charge account validation
            var accountValidationResult = ValidateChargeAccountIfNecessary(bestAvailableCompany.CompanyKey, request, orderCommand.OrderId, account, isFutureBooking, isFromWebApp, orderCommand.BookingFees, createReportOrder);

            createReportOrder.IsChargeAccountPaymentWithCardOnFile = accountValidationResult.IsChargeAccountPaymentWithCardOnFile;

            // if ChargeAccount uses payment with card on file, payment validation was already done
            if (!accountValidationResult.IsChargeAccountPaymentWithCardOnFile)
            {
                // Payment method validation
                ValidatePayment(bestAvailableCompany.CompanyKey, request, orderCommand.OrderId, account, isFutureBooking, request.Estimate.Price, orderCommand.BookingFees, isPrepaid, createReportOrder);
            }

            var chargeTypeIbs   = string.Empty;
            var chargeTypeEmail = string.Empty;
            var chargeTypeKey   = ChargeTypes.GetList()
                                  .Where(x => x.Id == request.Settings.ChargeTypeId)
                                  .Select(x => x.Display)
                                  .FirstOrDefault();

            chargeTypeKey = accountValidationResult.ChargeTypeKeyOverride ?? chargeTypeKey;

            if (chargeTypeKey != null)
            {
                // this must be localized with the priceformat to be localized in the language of the company
                // because it is sent to the driver
                chargeTypeIbs = _resources.Get(chargeTypeKey, _serverSettings.ServerData.PriceFormat);

                chargeTypeEmail = _resources.Get(chargeTypeKey, request.ClientLanguageCode);
            }

            // Get Vehicle Type from reference data
            var vehicleType = referenceData.VehiclesList
                              .Where(x => x.Id == request.Settings.VehicleTypeId)
                              .Select(x => x.Display)
                              .FirstOrDefault();

            // Use address alias if present.
            var addressAlias = request.PickupAddress.FriendlyName.HasValueTrimmed()
                ? request.PickupAddress.FriendlyName
                : request.PickupAddress.BuildingName;

            var ibsInformationNote = IbsHelper.BuildNote(
                _serverSettings.ServerData.IBS.NoteTemplate,
                chargeTypeIbs,
                request.Note,
                addressAlias,
                request.Settings.LargeBags,
                _serverSettings.ServerData.IBS.HideChargeTypeInUserNote);

            var fare = FareHelper.GetFareFromEstimate(request.Estimate);

            orderCommand.AccountId     = account.Id;
            orderCommand.UserAgent     = Request.UserAgent;
            orderCommand.ClientVersion = Request.Headers.Get("ClientVersion");
            orderCommand.IsChargeAccountPaymentWithCardOnFile = accountValidationResult.IsChargeAccountPaymentWithCardOnFile;
            orderCommand.CompanyKey               = bestAvailableCompany.CompanyKey;
            orderCommand.CompanyName              = bestAvailableCompany.CompanyName;
            orderCommand.CompanyFleetId           = bestAvailableCompany.FleetId;
            orderCommand.Market                   = market;
            orderCommand.IsPrepaid                = isPrepaid;
            orderCommand.Settings.ChargeType      = chargeTypeIbs;
            orderCommand.Settings.VehicleType     = vehicleType;
            orderCommand.IbsAccountId             = account.IBSAccountId.Value;
            orderCommand.ReferenceDataCompanyList = referenceData.CompaniesList.ToArray();
            orderCommand.IbsInformationNote       = ibsInformationNote;
            orderCommand.Fare                 = fare;
            orderCommand.Prompts              = accountValidationResult.Prompts;
            orderCommand.PromptsLength        = accountValidationResult.PromptsLength;
            orderCommand.PromotionId          = promotionId;
            orderCommand.ChargeTypeEmail      = chargeTypeEmail;
            orderCommand.OriginatingIpAddress = createReportOrder.OriginatingIpAddress = request.CustomerIpAddress;
            orderCommand.KountSessionId       = createReportOrder.OriginatingIpAddress = request.KountSessionId;
            orderCommand.IsFutureBooking      = createReportOrder.IsFutureBooking = isFutureBooking;
            orderCommand.AssignVehicleId      = createReportOrder.AssignVehicleId;

            Debug.Assert(request.PickupDate != null, "request.PickupDate != null");

            return(orderCommand);
        }
Example #4
0
        public object Post(RegisterAccount request)
        {
            // Ensure user is not signed in
            RequestContext.Get <IHttpRequest>().RemoveSession();

            if (_accountDao.FindByEmail(request.Email) != null)
            {
                throw new HttpError(ErrorCode.CreateAccount_AccountAlreadyExist.ToString());
            }

            CountryCode countryCode = CountryCode.GetCountryCodeByIndex(CountryCode.GetCountryCodeIndexByCountryISOCode(request.Country));

            if (PhoneHelper.IsPossibleNumber(countryCode, request.Phone))
            {
                request.Phone = PhoneHelper.GetDigitsFromPhoneNumber(request.Phone);
            }
            else
            {
                throw new HttpError(string.Format(_resources.Get("PhoneNumberFormat"), countryCode.GetPhoneExample()));
            }

            if (_blackListEntryService.GetAll().Any(e => e.PhoneNumber.Equals(request.Phone)))
            {
                throw new HttpError(_resources.Get("PhoneBlackListed"));
            }

            if (request.FacebookId.HasValue())
            {
                // Facebook registration
                if (_accountDao.FindByFacebookId(request.FacebookId) != null)
                {
                    throw new HttpError(ErrorCode.CreateAccount_AccountAlreadyExist.ToString());
                }

                var command = new RegisterFacebookAccount();

                Mapper.Map(request, command);
                command.Id = Guid.NewGuid();

                _commandBus.Send(command);

                return(new Account {
                    Id = command.AccountId
                });
            }
            if (request.TwitterId.HasValue())
            {
                // Twitter registration
                if (_accountDao.FindByTwitterId(request.TwitterId) != null)
                {
                    throw new HttpError(ErrorCode.CreateAccount_AccountAlreadyExist.ToString());
                }

                var command = new RegisterTwitterAccount();

                Mapper.Map(request, command);
                command.Id = Guid.NewGuid();

                _commandBus.Send(command);

                return(new Account {
                    Id = command.AccountId
                });
            }
            else
            {
                // Normal registration
                var accountActivationDisabled = _serverSettings.ServerData.AccountActivationDisabled;
                var smsConfirmationEnabled    = _serverSettings.ServerData.SMSConfirmationEnabled;

                var confirmationToken = smsConfirmationEnabled
                    ? GenerateActivationCode()
                    : Guid.NewGuid().ToString();

                var command = new Commands.RegisterAccount();

                Mapper.Map(request, command);
                command.Id = Guid.NewGuid();

                command.ConfimationToken          = confirmationToken;
                command.AccountActivationDisabled = accountActivationDisabled;
                _commandBus.Send(command);

                if (!accountActivationDisabled)
                {
                    if (smsConfirmationEnabled &&
                        (request.ActivationMethod == null ||
                         request.ActivationMethod == ActivationMethod.Sms))
                    {
                        _commandBus.Send(new SendAccountConfirmationSMS
                        {
                            ClientLanguageCode = command.Language,
                            Code        = confirmationToken,
                            CountryCode = command.Country,
                            PhoneNumber = command.Phone
                        });
                    }
                    else
                    {
                        _commandBus.Send(new SendAccountConfirmationEmail
                        {
                            ClientLanguageCode = command.Language,
                            EmailAddress       = command.Email,
                            ConfirmationUrl    =
                                new Uri(string.Format("/api/account/confirm/{0}/{1}", command.Email, confirmationToken), UriKind.Relative),
                        });
                    }
                }

                return(new Account {
                    Id = command.AccountId
                });
            }
        }