Example #1
0
        public void when_creating_an_order_successfully()
        {
            var pickupDate = DateTime.Now;
            var orderId    = Guid.NewGuid();

            var rideLinqPairing = new CreateOrderForManualRideLinqPair
            {
                AccountId          = _accountId,
                ClientLanguageCode = "fr",
                UserAgent          = "TestUserAgent",
                ClientVersion      = "1.0.0",
                PairingCode        = "0000000",
                PairingDate        = pickupDate,
                PairingToken       = "TESTTOKEN",
                Distance           = 0f,
                OrderId            = orderId,
                Medallion          = "3",
                DeviceName         = "EH1234",
                Fare = 12f,
                FareAtAlternateRate = 13f,
                RateAtTripEnd       = 99f,
                RateAtTripStart     = 3f,
                RateChangeTime      = "10",
                Extra     = 50f,
                Surcharge = 40f,
                Tax       = 9f,
                Tip       = 14f,
                Toll      = 7f,
                Total     = 5f
            };

            _sut.When(rideLinqPairing);

            var rideLinqPaired = _sut.ThenHasSingle <OrderManuallyPairedForRideLinq>();

            Assert.AreEqual(_accountId, rideLinqPaired.AccountId);
            Assert.AreEqual(pickupDate, rideLinqPaired.PairingDate);
            Assert.AreEqual(orderId, rideLinqPaired.SourceId);
            Assert.AreEqual(50f, rideLinqPaired.Extra);
            Assert.AreEqual("3", rideLinqPaired.Medallion);
            Assert.AreEqual("EH1234", rideLinqPaired.DeviceName);
            Assert.AreEqual(0f, rideLinqPaired.Distance);
            Assert.AreEqual(12f, rideLinqPaired.Fare);
            Assert.AreEqual(13f, rideLinqPaired.FareAtAlternateRate);
            Assert.AreEqual(99f, rideLinqPaired.RateAtTripEnd);
            Assert.AreEqual(3f, rideLinqPaired.RateAtTripStart);
            Assert.AreEqual("10", rideLinqPaired.RateChangeTime);
            Assert.AreEqual(40f, rideLinqPaired.Surcharge);
            Assert.AreEqual(9f, rideLinqPaired.Tax);
            Assert.AreEqual(14f, rideLinqPaired.Tip);
            Assert.AreEqual(7f, rideLinqPaired.Toll);
            Assert.AreEqual(5f, rideLinqPaired.Total);
        }
        public void Handle(CreateOrderForManualRideLinqPair command)
        {
            var order = new Order(command.OrderId);

            var creditCardToken = GetCreditCardTokenIfPossible(command.CreditCardId);

            order.UpdateOrderManuallyPairedForRideLinq(command.AccountId, command.PairingDate, command.PairingCode, command.PairingToken,
                                                       command.PickupAddress, command.UserAgent, command.ClientLanguageCode, command.ClientVersion, command.Distance, command.Total,
                                                       command.Fare, command.FareAtAlternateRate, command.Tax, command.Tip, command.Toll, command.Extra,
                                                       command.Surcharge, command.RateAtTripStart, command.RateAtTripEnd, command.RateChangeTime, command.Medallion, command.DeviceName,
                                                       command.TripId, command.DriverId, command.AccessFee, command.LastFour, command.OriginatingIpAddress, command.KountSessionId, command.CreditCardId, creditCardToken);

            _repository.Save(order, command.Id.ToString());
        }
        public object Post(ManualRideLinqPairingRequest request)
        {
            try
            {
                var accountId = new Guid(this.GetSession().UserAuthId);
                var account   = _accountDao.FindById(accountId);

                ValidateAppVersion(account);

                var currentRideLinq = _orderDao.GetCurrentManualRideLinq(request.PairingCode, account.Id);

                if (currentRideLinq != null)
                {
                    return(new ManualRideLinqResponse
                    {
                        Data = currentRideLinq,
                        IsSuccessful = true,
                        Message = "Ok"
                    });
                }

                var creditCard = account.DefaultCreditCard.HasValue
                    ? _creditCardDao.FindById(account.DefaultCreditCard.Value)
                    : null;

                if (creditCard == null)
                {
                    throw new HttpError(HttpStatusCode.BadRequest,
                                        ErrorCode.ManualRideLinq_NoCardOnFile.ToString(),
                                        _resources.Get("ManualRideLinq_NoCardOnFile", account.Language));
                }

                if (creditCard.IsDeactivated)
                {
                    throw new HttpError(HttpStatusCode.BadRequest,
                                        ErrorCode.ManualRideLinq_CardOnFileDeactivated.ToString(),
                                        _resources.Get("ManualRideLinq_CreditCardDisabled", account.Language));
                }

                // Send pairing request to CMT API
                var pairingRequest = new ManualRideLinqCoFPairingRequest
                {
                    AutoTipPercentage   = account.DefaultTipPercent ?? _serverSettings.ServerData.DefaultTipPercentage,
                    CustomerId          = accountId.ToString(),
                    CustomerName        = account.Name,
                    Latitude            = request.PickupAddress.Latitude,
                    Longitude           = request.PickupAddress.Longitude,
                    PairingCode         = request.PairingCode,
                    AutoCompletePayment = true,
                    CardOnFileId        = creditCard.Token,
                    LastFour            = creditCard.Last4Digits,
                    ZipCode             = creditCard.ZipCode,
                    Email             = account.Email,
                    CustomerIpAddress = RequestContext.IpAddress,
                    BillingFullName   = creditCard.NameOnCard,
                    SessionId         = request.KountSessionId
                };

                _logger.LogMessage("Pairing for manual RideLinq with Pairing Code {0}", request.PairingCode);

                var response = _cmtMobileServiceClient.Post(pairingRequest);

                _logger.LogMessage("Pairing result: {0}", response.ToJson());

                var trip = _cmtTripInfoServiceHelper.WaitForTripInfo(response.PairingToken, response.TimeoutSeconds);

                if (trip.HttpStatusCode == (int)HttpStatusCode.OK)
                {
                    var command = new CreateOrderForManualRideLinqPair
                    {
                        OrderId            = Guid.NewGuid(),
                        AccountId          = accountId,
                        UserAgent          = Request.UserAgent,
                        ClientVersion      = Request.Headers.Get("ClientVersion"),
                        PairingCode        = request.PairingCode,
                        PickupAddress      = request.PickupAddress,
                        PairingToken       = response.PairingToken,
                        PairingDate        = DateTime.Now,
                        ClientLanguageCode = request.ClientLanguageCode,
                        Distance           = trip.Distance,
                        StartTime          = trip.StartTime,
                        EndTime            = trip.EndTime,
                        Extra                = Math.Round(((double)trip.Extra / 100), 2),
                        Fare                 = Math.Round(((double)trip.Fare / 100), 2),
                        Tax                  = Math.Round(((double)trip.Tax / 100), 2),
                        Tip                  = Math.Round(((double)trip.Tip / 100), 2),
                        Toll                 = trip.TollHistory.Sum(toll => Math.Round(((double)toll.TollAmount / 100), 2)),
                        Surcharge            = Math.Round(((double)trip.Surcharge / 100), 2),
                        Total                = Math.Round(((double)trip.Total / 100), 2),
                        FareAtAlternateRate  = Math.Round(((double)trip.FareAtAlternateRate / 100), 2),
                        Medallion            = response.Medallion,
                        DeviceName           = response.DeviceName,
                        RateAtTripStart      = trip.RateAtTripStart,
                        RateAtTripEnd        = trip.RateAtTripEnd,
                        RateChangeTime       = trip.RateChangeTime,
                        TripId               = trip.TripId,
                        DriverId             = trip.DriverId,
                        LastFour             = trip.LastFour,
                        AccessFee            = Math.Round(((double)trip.AccessFee / 100), 2),
                        OriginatingIpAddress = request.CustomerIpAddress,
                        KountSessionId       = request.KountSessionId,
                        CreditCardId         = creditCard.CreditCardId,
                    };

                    _commandBus.Send(command);

                    var data = new OrderManualRideLinqDetail
                    {
                        OrderId             = command.OrderId,
                        Distance            = trip.Distance,
                        StartTime           = trip.StartTime,
                        EndTime             = trip.EndTime,
                        Extra               = command.Extra,
                        Fare                = command.Fare,
                        Tax                 = command.Tax,
                        Tip                 = command.Tip,
                        Toll                = command.Toll,
                        Surcharge           = command.Surcharge,
                        Total               = command.Total,
                        FareAtAlternateRate = command.FareAtAlternateRate,
                        Medallion           = response.Medallion,
                        DeviceName          = response.DeviceName,
                        RateAtTripStart     = command.RateAtTripStart,
                        RateAtTripEnd       = command.RateAtTripEnd,
                        RateChangeTime      = trip.RateChangeTime,
                        AccountId           = accountId,
                        PairingDate         = command.PairingDate,
                        PairingCode         = pairingRequest.PairingCode,
                        PairingToken        = trip.PairingToken,
                        DriverId            = trip.DriverId,
                        LastFour            = command.LastFour,
                        AccessFee           = command.AccessFee
                    };

                    return(new ManualRideLinqResponse
                    {
                        Data = data,
                        IsSuccessful = true,
                        Message = "Ok",
                        TripInfoHttpStatusCode = trip.HttpStatusCode,
                        ErrorCode = trip.ErrorCode.ToString()
                    });
                }
                else
                {
                    if (trip.HttpStatusCode == (int)HttpStatusCode.BadRequest)
                    {
                        switch (trip.ErrorCode)
                        {
                        case CmtErrorCodes.CreditCardDeclinedOnPreauthorization:
                            _notificationService.SendCmtPaymentFailedPush(accountId, _resources.Get("CreditCardDeclinedOnPreauthorizationErrorText", request.ClientLanguageCode));
                            break;

                        case CmtErrorCodes.UnablePreauthorizeCreditCard:
                            _notificationService.SendCmtPaymentFailedPush(accountId, _resources.Get("CreditCardUnableToPreathorizeErrorText", request.ClientLanguageCode));
                            break;

                        default:
                            _notificationService.SendCmtPaymentFailedPush(accountId, _resources.Get("TripUnableToPairErrorText", request.ClientLanguageCode));
                            break;
                        }
                    }

                    return(new ManualRideLinqResponse
                    {
                        IsSuccessful = false,
                        TripInfoHttpStatusCode = trip.HttpStatusCode,
                        ErrorCode = trip.ErrorCode != null?trip.ErrorCode.ToString() : null
                    });
                }
            }
            catch (WebServiceException ex)
            {
                _logger.LogMessage(
                    string.Format("A WebServiceException occured while trying to manually pair with CMT with pairing code: {0}",
                                  request.PairingCode));
                _logger.LogError(ex);

                ErrorResponse errorResponse = null;

                if (ex.ResponseBody != null)
                {
                    _logger.LogMessage("Error Response: {0}", ex.ResponseBody);

                    errorResponse = ex.ResponseBody.FromJson <ErrorResponse>();
                }

                return(new ManualRideLinqResponse
                {
                    IsSuccessful = false,
                    Message = errorResponse != null ? errorResponse.Message : ex.ErrorMessage,
                    ErrorCode = errorResponse != null?errorResponse.ResponseCode.ToString() : ex.ErrorCode
                });
            }
            catch (Exception ex)
            {
                _logger.LogMessage(string.Format("An error occured while trying to manually pair with CMT with pairing code: {0}", request.PairingCode));
                _logger.LogError(ex);

                return(new ManualRideLinqResponse
                {
                    IsSuccessful = false,
                    Message = ex.Message
                });
            }
        }