public string GetRawResponse(ResultOfProcessTransaction result)
        {
            string[] resultsparam = result.Params.Where(x =>
                                                        x.Key.Contains("authorization_num") ||
                                                        x.Key.Contains("transaction_approved") ||
                                                        x.Key.Contains("exact_message") ||
                                                        x.Key.Contains("retrieval_ref_no") ||
                                                        x.Key.Contains("transaction_tag") ||
                                                        x.Key.Contains("transaction_error")
                                                        ).Select(m => m.Key + ":" + m.Value).ToArray();

            var paramAuth = string.Join(", ", resultsparam);

            return(string.Format("Result:'{0}' Auth Code:'{1}' Message:'{2}' Error:'{3}' Ref #:'{4}' Test:{5} Params:{6})",
                                 result.Success.ToString(), result.Authorization, result.Message, result.Error, result.ReferenceNumber, result.Test.ToString(), paramAuth));
        }
        /// <summary>
        /// Performs a Process Transaction request on First Data Gateway.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Response PerformAuthRequest(Request request)
        {
            //create our client
            TokenServicesClient client = new TokenServicesClient();
            //create our token action
            ProcessTransationAction action = new ProcessTransationAction();

            //your tokenex credentials
            action.APIKey    = APIKey;
            action.TokenExID = TokenExID;
            //is the TransactionRequest json or xml;
            action.TransactionRequestFormat = TransactionRequestFormatEnum.XML;
            //what type of transaction we are performing
            action.TransactionType = TransactionTypeEnum.Authorize;
            //Get xml request
            string requestXML = RequestGenerator.GetAuthorizationRequest(request);

            requestXML = requestXML.Replace("[GATEWAYNAME]", GatewayName);
            requestXML = requestXML.Replace("[LOGIN]", GatewayLogin);
            requestXML = requestXML.Replace("[PASSWORD]", GatewayPassword);
            action.TransactionRequest = requestXML;
            //call the web service
            ResultOfProcessTransaction result = client.ProcessTransaction(action);
            //Generate and populate response object
            Response response = new Response();

            response.GatewayRequestRaw  = requestXML;
            response.GatewayResponseRaw = GetRawResponse(result);
            Dictionary <string, string> resultsparam = result.Params;

            //if our call was a success, save authorization code
            if (resultsparam.ContainsKey("transaction_tag"))
            {
                response.TransactionID = resultsparam["transaction_tag"].PadLeft(10, '0');;
            }
            // response.TransactionID = result.ReferenceNumber;
            response.AuthCode = result.Authorization;


            if (result.Success)
            {
                bool authApproved = false;
                try
                {
                    if (resultsparam.ContainsKey("transaction_approved"))
                    {
                        authApproved = Convert.ToBoolean(resultsparam["transaction_approved"]);
                    }
                    if (authApproved)
                    {
                        response.ResponseType = TransactionResponseType.Approved;
                    }
                    else
                    {
                        response.ResponseType = TransactionResponseType.Denied;
                    }
                }
                catch (Exception e)
                {
                    response.ResponseType = TransactionResponseType.Error;
                    response.ReasonText   = e.InnerException.ToString();
                }
            }
            else if (!string.IsNullOrEmpty(result.Error))
            {
                response.ResponseType = TransactionResponseType.Error;
                response.ReasonText   = result.Error;
            }
            else
            {
                response.ResponseType = TransactionResponseType.Denied;
                response.ReasonText   = result.Error;
            }

            return(response);
        }