/// <summary> /// If we ever need to support fees or CoF network payment with other providers than CMT/RideLinq, /// we'll need to keep track of on which company the pre-auth has been made and handle it properly because, as of today, orderPaymentDetail is unique by order. /// </summary> public PreAuthorizePaymentResponse PreAuthorize(string companyKey, Guid orderId, AccountDetail account, decimal amountToPreAuthorize, bool isReAuth = false, bool isSettlingOverduePayment = false, bool isForPrepaid = false, string cvv = null) { // we pass the orderId just in case it might exist but most of the time it won't since preauth is done before order creation if (IsPayPal(account.Id, orderId, isForPrepaid)) { return(_payPalServiceFactory.GetInstance(companyKey).PreAuthorize(account.Id, orderId, account.Email, amountToPreAuthorize, isReAuth)); } // if we call preauth more than once, the cvv will be null but since preauth already passed once, it's safe to assume it's ok var response = GetInstance(companyKey).PreAuthorize(companyKey, orderId, account, amountToPreAuthorize, isReAuth, isSettlingOverduePayment, false, cvv); // when CMT has preauth enabled, remove this ugly code and delete temp info for everyone // we can't delete here for CMT because we need the cvv info in the CommitPayment method if (GetPaymentSettings(companyKey).PaymentMode != PaymentMethod.Cmt && GetPaymentSettings(companyKey).PaymentMode != PaymentMethod.RideLinqCmt) { // delete the cvv stored in database once preauth is done, doesn't fail if it doesn't exist _orderDao.DeleteTemporaryPaymentInfo(orderId); } return(response); }
public CommitPreauthorizedPaymentResponse CommitPayment(string companyKey, Guid orderId, AccountDetail account, decimal preauthAmount, decimal amount, decimal meterAmount, decimal tipAmount, string transactionId, string reAuthOrderId = null, bool isForPrepaid = false, string kountSessionId = null, string customerIpAddress = null) { // No need to use preauthAmount for CMT because we can't preauthorize try { var orderDetail = _orderDao.FindById(orderId); if (orderDetail == null) { throw new Exception("Order not found"); } var creditCard = _creditCardDao.FindById(account.DefaultCreditCard.GetValueOrDefault()); var commitTransactionId = transactionId; if (_serverPaymentSettings.PaymentMode == PaymentMethod.RideLinqCmt) { InitializeServiceClient(); var pairingToken = orderDetail.IsManualRideLinq ? _orderDao.GetManualRideLinqById(orderId).PairingToken : _orderDao.FindOrderPairingById(orderId).PairingToken; var request = new CmtRideLinqAuthorizationRequest { PairingToken = pairingToken, CofToken = creditCard.Token, LastFour = creditCard.Last4Digits, SessionId = kountSessionId, Email = account.Email, CustomerIpAddress = customerIpAddress, BillingFullName = creditCard.NameOnCard }; if (creditCard.ZipCode.HasValue()) { request.ZipCode = creditCard.ZipCode; } var requestUrl = string.Format("payment/{0}/authorize/{1}", request.PairingToken, request.CofToken); _logger.LogMessage("Trying to authorize payment for CMT RideLinq (settling an overdue payment). Url: {0} Request {1}", requestUrl, request.ToJson()); try { var response = _cmtMobileServiceClient.Post(requestUrl, request); if (response != null && response.StatusCode == HttpStatusCode.OK) { _logger.LogMessage("Call to CMT RideLinq authorize succeeded"); return(new CommitPreauthorizedPaymentResponse { IsSuccessful = true, AuthorizationCode = "NoAuthCodeReturnedFromCmtRideLinqAuthorize", Message = "Success", TransactionId = commitTransactionId, TransactionDate = DateTime.UtcNow }); } } catch (WebServiceException ex) { _logger.LogMessage("Response: {0} {1} (Body: {2})", ex.StatusCode, ex.StatusDescription, ex.ResponseBody); var cmtErrorCode = ex.ResponseBody.FromJson <ErrorResponse>(); if (cmtErrorCode.ResponseCode == CmtErrorCodes.TripAlreadyAuthorized) { // this should be considered a success _logger.LogMessage("Received error code 615, consider it a success"); return(new CommitPreauthorizedPaymentResponse { IsSuccessful = true, AuthorizationCode = "NoAuthCodeReturnedFromCmtRideLinqAuthorize", Message = "Success", TransactionId = commitTransactionId, TransactionDate = DateTime.UtcNow }); } return(new CommitPreauthorizedPaymentResponse { IsSuccessful = false, TransactionId = commitTransactionId, Message = ex.ResponseBody }); } return(new CommitPreauthorizedPaymentResponse { IsSuccessful = false, TransactionId = commitTransactionId }); } else { string authorizationCode = null; var orderStatus = _orderDao.FindOrderStatusById(orderId); if (orderStatus == null) { throw new Exception("Order status not found"); } var deviceId = orderStatus.VehicleNumber; var driverId = orderStatus.DriverInfos == null ? 0 : orderStatus.DriverInfos.DriverId.To <int>(); var employeeId = orderStatus.DriverInfos == null ? string.Empty : orderStatus.DriverInfos.DriverId; var tripId = orderStatus.IBSOrderId.Value; var fleetToken = _serverPaymentSettings.CmtPaymentSettings.FleetToken; var customerReferenceNumber = orderStatus.ReferenceNumber.HasValue() ? orderStatus.ReferenceNumber : orderDetail.IBSOrderId.ToString(); var tempPaymentInfo = _orderDao.GetTemporaryPaymentInfo(orderId); var cvv = tempPaymentInfo != null ? tempPaymentInfo.Cvv : null; var authRequest = new AuthorizationRequest { FleetToken = fleetToken, DeviceId = deviceId, Amount = (int)(amount * 100), CardOnFileToken = creditCard.Token, CustomerReferenceNumber = customerReferenceNumber, DriverId = driverId, EmployeeId = employeeId, ShiftUuid = orderDetail.Id.ToString(), Fare = (int)(meterAmount * 100), Tip = (int)(tipAmount * 100), TripId = tripId, ConvenienceFee = 0, Extras = 0, Surcharge = 0, Tax = 0, Tolls = 0, Cvv2 = cvv }; // remove temp payment info _orderDao.DeleteTemporaryPaymentInfo(orderId); var authResponse = Authorize(authRequest); var isSuccessful = authResponse.ResponseCode == 1; var isCardDeclined = authResponse.ResponseCode == 607; if (isSuccessful) { commitTransactionId = authResponse.TransactionId.ToString(CultureInfo.InvariantCulture); authorizationCode = authResponse.AuthorizationCode; } return(new CommitPreauthorizedPaymentResponse { IsSuccessful = isSuccessful, AuthorizationCode = authorizationCode, Message = authResponse.ResponseMessage, TransactionId = commitTransactionId, IsDeclined = isCardDeclined, TransactionDate = isSuccessful ? (DateTime?)authResponse.AuthorizationDate : null }); } } catch (Exception ex) { return(new CommitPreauthorizedPaymentResponse { IsSuccessful = false, TransactionId = transactionId, Message = ex.Message }); } }