private ResPreauthCC CreateAuthWithVault(string dataKey, string orderId, string amount, string cvd)
        {
            // Create purchase with dataKey
            var auth = new ResPreauthCC();

            auth.SetDataKey(dataKey);
            auth.SetOrderId(orderId);
            auth.SetAmount(amount);
            auth.SetCryptType(_monerisPaymentSettings.Crypt);
            auth.SetDynamicDescriptor(_monerisPaymentSettings.DynamicDescriptor);
            // CVD check
            auth.SetCvdInfo(CreateCvdInfo(cvd));

            return(auth);
        }
 private void AddCvvInfo(ResPreauthCC preAuthorizeCommand, string cvv)
 {
     if (_serverSettings.GetPaymentSettings().AskForCVVAtBooking)
     {
         if (!cvv.HasValue())
         {
             _logger.LogMessage("AskForCVVAtBooking setting is enabled but no cvv found for this order, could be from a reauth");
         }
         else
         {
             // Only supported for Visa/MasterCard/Amex
             var cvdCheck = new CvdInfo();
             cvdCheck.SetCvdIndicator("1");
             cvdCheck.SetCvdValue(cvv);
             preAuthorizeCommand.SetCvdInfo(cvdCheck);
         }
     }
 }
        public PreAuthorizePaymentResponse PreAuthorize(string companyKey, Guid orderId, AccountDetail account, decimal amountToPreAuthorize, bool isReAuth = false, bool isSettlingOverduePayment = false, bool isForPrepaid = false, string cvv = null)
        {
            var      message         = string.Empty;
            var      transactionId   = string.Empty;
            DateTime?transactionDate = null;

            try
            {
                bool isSuccessful;
                var  isCardDeclined = false;
                var  creditCard     = _creditCardDao.FindById(account.DefaultCreditCard.GetValueOrDefault());

                var order = _orderDao.FindOrderStatusById(orderId);

                // We cannot re-use the same id has a previously failed payment
                var shouldGenerateNewOrderId = isReAuth || isSettlingOverduePayment;

                var orderIdentifier = shouldGenerateNewOrderId
                    ? string.Format("{0}-{1}", orderId, GenerateShortUid())
                    : orderId.ToString();

                if (amountToPreAuthorize > 0)
                {
                    // PreAuthorize transaction
                    var monerisSettings = _serverPaymentSettings.MonerisPaymentSettings;

                    var customerId = monerisSettings.UseCarIdInTransaction
                        ? order.SelectOrDefault(o => o.VehicleNumber)
                        : order != null?order.DriverInfos.SelectOrDefault(driverInfos => driverInfos.DriverId) : null;

                    var preAuthorizeCommand = new ResPreauthCC(creditCard.Token, orderIdentifier, customerId, amountToPreAuthorize.ToString("F"), CryptType_SSLEnabledMerchant);
                    AddCvvInfo(preAuthorizeCommand, cvv);

                    var info = new AvsInfo();
                    if (_serverPaymentSettings.EnableAddressVerification)
                    {
                        info.SetAvsStreetName(creditCard.StreetName);
                        info.SetAvsStreetNumber(creditCard.StreetNumber);
                        info.SetAvsZipCode(creditCard.ZipCode);
                        info.SetAvsCustPhone(creditCard.Phone);
                        info.SetAvsEmail(creditCard.Email);
                        preAuthorizeCommand.SetAvsInfo(info);
                    }

                    var preAuthRequest = MonerisHttpRequestWrapper.NewHttpsPostRequest(monerisSettings.Host, monerisSettings.StoreId, monerisSettings.ApiToken, preAuthorizeCommand);
                    var preAuthReceipt = preAuthRequest.GetAndLogReceipt(_logger);

                    isSuccessful    = RequestSuccesful(preAuthReceipt, out message);
                    isCardDeclined  = IsCardDeclined(preAuthReceipt);
                    transactionId   = preAuthReceipt.GetTxnNumber();
                    transactionDate = GetTransactionDate(preAuthReceipt);
                }
                else
                {
                    // if we're preauthorizing $0, we skip the preauth with payment provider
                    // but we still send the InitiateCreditCardPayment command
                    // this should never happen in the case of a real preauth (hence the minimum of $50)
                    isSuccessful = true;
                }

                if (isSuccessful && !isReAuth)
                {
                    var paymentId = Guid.NewGuid();
                    _commandBus.Send(new InitiateCreditCardPayment
                    {
                        PaymentId     = paymentId,
                        Amount        = amountToPreAuthorize,
                        TransactionId = transactionId,
                        OrderId       = orderId,
                        CardToken     = creditCard.Token,
                        Provider      = PaymentProvider.Moneris,
                        IsNoShowFee   = false,
                        CompanyKey    = companyKey
                    });
                }

                return(new PreAuthorizePaymentResponse
                {
                    IsSuccessful = isSuccessful,
                    Message = message,
                    TransactionId = transactionId,
                    ReAuthOrderId = shouldGenerateNewOrderId ? orderIdentifier : null,
                    IsDeclined = isCardDeclined,
                    TransactionDate = transactionDate,
                });
            }
            catch (Exception e)
            {
                _logger.LogMessage(string.Format("Error during preauthorization (validation of the card) for client {0}: {1} - {2}", account.Email, message, e));
                _logger.LogError(e);

                return(new PreAuthorizePaymentResponse
                {
                    IsSuccessful = false,
                    Message = message
                });
            }
        }