public PrepaidVoucherResponse ConsumeVoucher(PrepaidVoucherRequest request)
 {
     return(this.GetPrepaidVoucherResponse(request));
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns>PrepaidVoucherResponse</returns>
        private PrepaidVoucherResponse GetPrepaidVoucherResponse(PrepaidVoucherRequest request)
        {
            string output         = string.Empty;
            bool   knownException = false;

            // Initilize consume voucher.
            var consumeVoucherData = new ConsumeVoucherData(request);

            // Create the response object.
            PrepaidVoucherResponse response = new PrepaidVoucherResponse();

            try
            {
                // Get the Authentication Header.
                if (request.header == null)
                {
                    // Set the correct return code of the on the response.
                    response.returnCode = ReturnCode.IbsCiUnauthorized;
                    knownException      = true;
                    throw new IntegrationException("No Authentication Header was Provided.\r\n");
                }

                AuthenticationHeader authHeader = request.header;

                // Set the voucher to use in the response as the original request voucher.
                response.prepaidVoucher = request.prepaidVoucher;

                // Assume error unless otherwise specified. Set voucher amount = 0;
                // Per IDD, On error: The Web Method returns zero ("0") for "amount" and one of four possible codes for error:
                response.prepaidVoucher.VoucherAmount = 0;

                if (string.IsNullOrEmpty(authHeader.Dsn))
                {
                    // Set the correct return code of the on the response.
                    response.returnCode = ReturnCode.IbsCiUnauthorized;
                    knownException      = true;
                    throw new IntegrationException("Authentication credentials are invalid. No Dsn was provided.\r\n");
                }

                try
                {
                    var authService = GetAuthenticationService();
                    var result      = authService.AuthenticateByProof(new UserIdentity
                    {
                        UserName = authHeader.Username,
                        Dsns     = new DsnCollection {
                            new Dsn {
                                Name = authHeader.Dsn
                            }
                        }
                    }, authHeader.Password, string.Empty);

                    if (result == null || !result.Authenticated.Value)
                    {
                        response.returnCode = ReturnCode.IbsCiUnauthorized;
                        knownException      = true;
                        throw new IntegrationException("Authentication credentials are invalid. No Dsn was provided.\r\n");
                    }
                }
                catch (Exception ex)
                {
                    // Set the correct return code of the on the response.
                    response.returnCode = ReturnCode.IbsCiUnauthorized;

                    knownException = true;

                    // Just throw the exception.
                    throw ex;
                }

                // Serialize the Prepaid Voucher to pass to the worrker.
                var inputXml = SerializationUtilities <PrepaidVoucher> .Xml.Serialize(request.prepaidVoucher);

                WriteCommLogEntry(
                    consumeVoucherData.CustomerId,
                    null,
                    this.Name,
                    inputXml,
                    CommunicationLogEntryMessageQualifier.Receive,
                    System.Threading.Interlocked.Increment(ref commLogSequenceId).ToString(),
                    consumeVoucherData.VoucherTicketNumber.ToString()
                    );

                // ensure that the command that we did get is the type we expected.
                PP_01_ConsumeVoucher voucherCommand = CreateVoucherCommand(context, consumeVoucherData);

                // execute our worker and get the response.
                voucherCommand.GetResponse(out output);

                // Exception should only be thrown in a rare case. It is a System Error.
                // normal response will be ftId and return code combine with char | eg: 1234|VmsValidationSuccessful
                if (string.IsNullOrEmpty(output) || !output.Contains("|"))
                {
                    // Set the correct return code of the on the response.
                    response.returnCode = ReturnCode.IbsCiSystemError;
                    Diagnostics.Error(output);
                    return(response);
                }

                string[] outputs = output.Split(new char[] { '|' });
                if (outputs.Length > 1)
                {
                    int financialTransactionId = int.Parse(outputs[0]);
                    response.financialTransactionId = financialTransactionId;
                    output = outputs[1];
                }

                // Obtain the enum value from the response code returned from the response command.
                var returnCode = (ReturnCode)Enum.Parse(typeof(ReturnCode), output);

                // Set the voucher amount on the Response voucher from the worker.
                response.prepaidVoucher.VoucherAmount = voucherCommand.VoucherAmount;

                // Set the return code on the response.
                response.returnCode = returnCode;
            }
            catch (Exception ex)
            {
                // Log the error.
                if (!knownException)
                {
                    response.returnCode = ReturnCode.IbsCiSystemError;
                }
                // Log the error.
                string error = string.Format("Error in {0}.{1}() processing received message.\r\n\r\n{2}\r\n\nMessage content:\r\n{3}.", this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.ToString(), SerializationUtilities <PrepaidVoucher> .Xml.Serialize(request.prepaidVoucher));
                Diagnostics.Error(error);
            }
            finally
            {
                string outputXml = SerializationUtilities <PrepaidVoucherResponse> .Xml.Serialize(response);

                WriteCommLogEntry(
                    consumeVoucherData.CustomerId,
                    null,
                    this.Name,
                    outputXml,
                    CommunicationLogEntryMessageQualifier.Send,
                    System.Threading.Interlocked.Increment(ref commLogSequenceId).ToString(),
                    consumeVoucherData.VoucherTicketNumber.ToString());
            }

            return(response);
        }