private AvsInfo AssignAVSValidationFieldsFromPaymentMethod(Entity paymentMethod, AvsInfo avsCheck, LocalPluginContext localContext, IOrganizationService service)
 {
     localContext.TracingService.Trace("Entering AssignAVSValidationFieldsFromPaymentMethod().");
     try
     {
         if (!paymentMethod.Contains("msnfp_billing_line1") || !paymentMethod.Contains("msnfp_billing_postalcode"))
         {
             localContext.TracingService.Trace("Customer is missing either msnfp_billing_line1 or msnfp_billing_postalcode fields on their payment method. Exiting plugin.");
             throw new Exception("Customer is missing either msnfp_billing_line1 or msnfp_billing_postalcode fields on their payment method. Exiting plugin.");
         }
         string[] array = ((string)paymentMethod["msnfp_billing_line1"]).Split(' ');
         if (array.Length <= 1)
         {
             localContext.TracingService.Trace("Could not split address for AVS Validation. Please ensure the Street 1 billing address on the payment method is in the form '123 Example Street'. Exiting plugin.");
             throw new ArgumentNullException("msnfp_billing_line1");
         }
         string text = (string)paymentMethod["msnfp_billing_line1"];
         localContext.TracingService.Trace("Unformatted Street Name: " + text);
         text = text.Replace(array[0], "").Trim(' ');
         localContext.TracingService.Trace("Formatted Street Name: " + text);
         localContext.TracingService.Trace("Formatted Street Number: " + array[0]);
         avsCheck.SetAvsStreetNumber(array[0]);
         avsCheck.SetAvsStreetName(text);
         avsCheck.SetAvsZipCode((string)paymentMethod["msnfp_billing_postalcode"]);
         if (paymentMethod.Contains("msnfp_emailaddress1"))
         {
             avsCheck.SetAvsEmail((string)paymentMethod["msnfp_emailaddress1"]);
         }
         avsCheck.SetAvsShipMethod("G");
         if (paymentMethod.Contains("msnfp_telephone1"))
         {
             avsCheck.SetAvsCustPhone((string)paymentMethod["msnfp_telephone1"]);
         }
         localContext.TracingService.Trace("Updated AVS Check variable successfully.");
     }
     catch (Exception ex)
     {
         localContext.TracingService.Trace("AssignAVSValidationFieldsFromPaymentMethod() Error: " + ex.ToString());
         throw new Exception("AssignAVSValidationFieldsFromPaymentMethod() Error: " + ex.ToString());
     }
     return(avsCheck);
 }
        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
                });
            }
        }
        public static void Main(string[] args)
        {
            string host = "esqa.moneris.com";
              string store_id = "store5";
              string api_token = "yesguy";

              string order_id = "Need_Unique_Order_ID_123456";
              string orig_order_id = "Need_Unique_Order_ID_12345";
              string amount = "304.00";
              string txn_number = "174885-0_7";
              string crypt = "7";

            /*************** Address Verification Service **********************/

               	AvsInfo avsCheck = new AvsInfo();

               	avsCheck.SetAvsStreetNumber ("212");
               	avsCheck.SetAvsStreetName ("Payton Street");
               	avsCheck.SetAvsZipCode ("M1M1M1");
            avsCheck.SetAvsEmail ("*****@*****.**");
            avsCheck.SetAvsHostname ("hostname");
            avsCheck.SetAvsBrowser ("Mozilla");
            avsCheck.SetAvsShipToCountry ("Canada");
            avsCheck.SetAvsShipMethod ("Ground");
            avsCheck.SetAvsMerchProdSku ("123456");
            avsCheck.SetAvsCustIp ("192.168.0.1");
            avsCheck.SetAvsCustPhone ("5556667777");

            ReAuth reauthTxn = new ReAuth(order_id, orig_order_id, txn_number, amount, crypt);
               	reauthTxn.SetAvsInfo (avsCheck);

               	/****************** Card Validation Digits *************************/

               	CvdInfo cvdCheck = new CvdInfo();
               	cvdCheck.SetCvdIndicator ("1");
               	cvdCheck.SetCvdValue ("099");

               	reauthTxn.SetCvdInfo (cvdCheck);

               	/************************** Request *************************/

            HttpsPostRequest mpgReq =
                new HttpsPostRequest(host, store_id, api_token,reauthTxn);

            /************************** Receipt *************************/

            try
            {
               	Receipt receipt = mpgReq.GetReceipt();

                Console.WriteLine("CardType = " + receipt.GetCardType());
                Console.WriteLine("TransAmount = " + receipt.GetTransAmount());
                Console.WriteLine("TxnNumber = " + receipt.GetTxnNumber());
                Console.WriteLine("ReceiptId = " + receipt.GetReceiptId());
                Console.WriteLine("TransType = " + receipt.GetTransType());
                Console.WriteLine("ReferenceNum = " + receipt.GetReferenceNum());
                Console.WriteLine("ResponseCode = " + receipt.GetResponseCode());
                Console.WriteLine("ISO = " + receipt.GetISO());
                Console.WriteLine("BankTotals = " + receipt.GetBankTotals());
                Console.WriteLine("Message = " + receipt.GetMessage());
                Console.WriteLine("AuthCode = " + receipt.GetAuthCode());
                Console.WriteLine("Complete = " + receipt.GetComplete());
                Console.WriteLine("TransDate = " + receipt.GetTransDate());
                Console.WriteLine("TransTime = " + receipt.GetTransTime());
                Console.WriteLine("Ticket = " + receipt.GetTicket());
                Console.WriteLine("TimedOut = " + receipt.GetTimedOut ());
                Console.WriteLine("Avs Response = " + receipt.GetAvsResultCode());
                Console.WriteLine("Cvd Response = " + receipt.GetCvdResultCode());
                Console.WriteLine("IsVisaDebit = " + receipt.GetIsVisaDebit());

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void Main(string[] args)
        {
            /********************** Post Request Variables *************************/

            string host = "esqa.moneris.com";
            string store_id = "store5";
            string api_token = "yesguy";

            /********************** Transactional Variables **********************/

            string order_id = "Test_eFraud_dotNET_1234";
            string amount = "10.02";
            string pan = "4242424242424242";
            string expiry_date = "1111";
            string crypt = "7";

            /*************** Address Verification Service **********************/

               	AvsInfo avsCheck = new AvsInfo();

               	avsCheck.SetAvsStreetNumber ("212");
               	avsCheck.SetAvsStreetName ("Payton Street");
               	avsCheck.SetAvsZipCode ("M1M1M1");
            avsCheck.SetAvsEmail("*****@*****.**");
            avsCheck.SetAvsHostname("hostname");
            avsCheck.SetAvsBrowser("Mozilla");
            avsCheck.SetAvsShipToCountry("CAN");
            avsCheck.SetAvsShipMethod("G");
            avsCheck.SetAvsMerchProdSku("123456");
            avsCheck.SetAvsCustIp("192.168.0.1");
            avsCheck.SetAvsCustPhone("5556667777");

               	Purchase purchaseTxn = new Purchase(order_id, amount, pan, expiry_date, crypt);
               	purchaseTxn.SetAvsInfo (avsCheck);

               	/****************** Card Validation Digits *************************/

               	CvdInfo cvdCheck = new CvdInfo();
               	cvdCheck.SetCvdIndicator ("1");
               	cvdCheck.SetCvdValue ("099");

               	purchaseTxn.SetCvdInfo (cvdCheck);

               	/************************** Request *************************/

            HttpsPostRequest mpgReq =
                new HttpsPostRequest(host, store_id, api_token,purchaseTxn);

            /************************** Receipt *************************/

            try
            {
               	Receipt receipt = mpgReq.GetReceipt();

                Console.WriteLine("CardType = " + receipt.GetCardType());
                Console.WriteLine("TransAmount = " + receipt.GetTransAmount());
                Console.WriteLine("TxnNumber = " + receipt.GetTxnNumber());
                Console.WriteLine("ReceiptId = " + receipt.GetReceiptId());
                Console.WriteLine("TransType = " + receipt.GetTransType());
                Console.WriteLine("ReferenceNum = " + receipt.GetReferenceNum());
                Console.WriteLine("ResponseCode = " + receipt.GetResponseCode());
                Console.WriteLine("ISO = " + receipt.GetISO());
                Console.WriteLine("BankTotals = " + receipt.GetBankTotals());
                Console.WriteLine("Message = " + receipt.GetMessage());
                Console.WriteLine("AuthCode = " + receipt.GetAuthCode());
                Console.WriteLine("Complete = " + receipt.GetComplete());
                Console.WriteLine("TransDate = " + receipt.GetTransDate());
                Console.WriteLine("TransTime = " + receipt.GetTransTime());
                Console.WriteLine("Ticket = " + receipt.GetTicket());
                Console.WriteLine("TimedOut = " + receipt.GetTimedOut ());
                Console.WriteLine("Avs Response = " + receipt.GetAvsResultCode());
                Console.WriteLine("Cvd Response = " + receipt.GetCvdResultCode());
                Console.WriteLine("ITD Response = " + receipt.GetITDResponse());
                Console.WriteLine("IsVisaDebit = " + receipt.GetIsVisaDebit());

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }