Пример #1
0
        /// <summary>
        /// Adds a call completion entry to the log if exhaustive detail entries are added.
        /// </summary>
        /// <param name="callName">
        /// The name of the call whose completion to log.
        /// </param>
        /// <param name="callCompletionStatus">
        /// The call status at the completion of the call.
        /// </param>
        /// <param name="analyticsInformation">
        /// The object through which analytics information can be added and obtained.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Parameter analyticsInformation cannot be null.
        /// </exception>
        /// <remarks>
        /// This should eventually be replaced with performance counters.
        /// </remarks>
        public void ExhaustiveCallCompletion(string callName,
                                             CallCompletionStatus callCompletionStatus,
                                             PerformanceInformation analyticsInformation)
        {
            if (analyticsInformation == null)
            {
                throw new ArgumentNullException("analyticsInformation", "Parameter analyticsInformation cannot be null.");
            }

            // Generally, exhausive detail entries will not be included in the log.
            if (LogVerbosity == SourceLevels.All)
            {
                CallCompletion(callName, callCompletionStatus, analyticsInformation, DefaultLogEntryEventId.Verbose);
            }
        }
Пример #2
0
        /// <summary>
        ///     Test whether Visa Incoming call is authorized. To be developed.
        /// </summary>
        /// <returns>
        ///     True/False based on incoming request
        /// </returns>
        //private static bool IsAuthorized(CommerceContext context)
        //{
        //    //TODO: Lock down security once the interop is working.
        //    context.Log.Error("Still not locked the endpoint yet {0}", null, "visa endpoint");
        //    return true;
        //}

        /// <summary>
        ///     Process the Incoming Visa EndpointMessage Request
        /// </summary>
        /// <param name="context">
        ///     Commerce Context
        /// </param>
        /// <param name="callTimer">
        ///     Call Timer
        /// </param>
        /// <param name="executorInvoker">
        ///     Executor handler
        /// </param>
        /// <returns>
        ///     HttpStatus code
        /// </returns>
        private HttpStatusCode ProcessVisaEndpointMessage(CommerceContext context,
                                                          Stopwatch callTimer,
                                                          Func <ResultCode> executorInvoker)
        {
            HttpStatusCode result = HttpStatusCode.Unauthorized;

            context.Log.Information("Processing {0} call.", context.ApiCallDescription);
            CallCompletionStatus callCompletionStatus = CallCompletionStatus.Error;

#if !IntDebug && !IntRelease
            if (ValidateIP(context) == true)
#endif
            {
                try
                {
                    ResultCode resultCode = executorInvoker();
                    if (resultCode == ResultCode.Success || resultCode == ResultCode.Created ||
                        resultCode == ResultCode.DuplicateTransaction)
                    {
                        callCompletionStatus = CallCompletionStatus.Success;
                        context.Log.Information("{0} call processed successfully.", context.ApiCallDescription);
                        result = HttpStatusCode.OK;
                    }
                    else
                    {
                        //if (resultCode == ResultCode.None)
                        //TODO: need to be more discrimitive
                        result = HttpStatusCode.InternalServerError;
                        callCompletionStatus = CallCompletionStatus.SuccessWithWarnings;
                        context.Log.Warning(
                            "{0} call unsuccessfully processed.\r\n\r\nResultCode: {1}\r\n\r\nExplanation: {2}",
                            (int)resultCode, context.ApiCallDescription, resultCode,
                            ResultCodeExplanation.Get(resultCode));
                    }
                }
                catch (Exception ex)
                {
                    context.Log.Critical("{0} call ended with an error.", ex, context.ApiCallDescription);
                    result = HttpStatusCode.InternalServerError;
                }
            }

            callTimer.Stop();
            context.PerformanceInformation.Add("Total", String.Format("{0} ms", callTimer.ElapsedMilliseconds));
            context.Log.CallCompletion(context.ApiCallDescription, callCompletionStatus, context.PerformanceInformation);

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Adds a call completion entry to the log.
        /// </summary>
        /// <param name="callName">
        /// The name of the call whose completion to log.
        /// </param>
        /// <param name="callCompletionStatus">
        /// The call status at the completion of the call.
        /// </param>
        /// <param name="performanceInformation">
        /// The object through which analytics information can be added and obtained.
        /// </param>
        /// <param name="eventId">
        /// The event ID to use when logging call completion. Default value is DefaultLogEntryEventId.CallCompletion.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Parameter analyticsInformation cannot be null.
        /// </exception>
        /// <remarks>
        /// This should eventually be replaced with performance counters.
        /// </remarks>
        public void CallCompletion(string callName,
                                   CallCompletionStatus callCompletionStatus,
                                   PerformanceInformation performanceInformation,
                                   DefaultLogEntryEventId eventId = DefaultLogEntryEventId.CallCompletion)
        {
            if (performanceInformation == null)
            {
                throw new ArgumentNullException("performanceInformation", "Parameter performanceInformation cannot be null.");
            }

            if (IsLoggingDisabled() == false)
            {
                Log.Info((int)eventId, ActivityId,
                         "Call to {0} completed with result {1} and these performance measures:\r\n {2}.", callName, callCompletionStatus,
                         performanceInformation.Collate());
            }
        }
        public HttpResponseMessage OnAuthorization(Transaction request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request", "Parameter request cannot be null.");
            }

            // Build a context object to pass down the pipeline.
            Stopwatch callTimer = Stopwatch.StartNew();

            Context = new CommerceContext("Incoming MasterCard Authorization Request");
            MasterCardAuthorizationResponse response = new MasterCardAuthorizationResponse();

            Context[Key.Transaction] = request;
            Context[Key.Response]    = response;
            Context[Key.CallTimer]   = callTimer;

            // Process the call and log its result.
            Context.Log.Information("Processing {0} call.", Context.ApiCallDescription);
//TODO: If we ever circle back and switch to client certificate authentication, uncomment this and remove ValidateIP.
            //CustomIdentity clientIdentity = (CustomIdentity)Thread.CurrentPrincipal.Identity;
            //Context.Log.Verbose("Presented client certificate has subject \"{0}\" and thumbprint \"{1}\".",
            //                    clientIdentity.Name, clientIdentity.PresentedClientToken);
            ResultCode resultCode = ResultCode.Unauthorized;

            response.ResponseCode = ((int)resultCode).ToString();
            CallCompletionStatus callCompletionStatus = CallCompletionStatus.Error;

            if (ValidateIP() == true)
            {
                try
                {
                    MasterCardAuthorizationExecutor executor = new MasterCardAuthorizationExecutor(Context);
                    resultCode = executor.Execute();

                    // Determine call completion status from executor result and log it.
                    if (resultCode == ResultCode.Created || resultCode == ResultCode.DuplicateTransaction)
                    {
                        resultCode           = ResultCode.Success;
                        callCompletionStatus = CallCompletionStatus.Success;
                        Context.Log.Information("{0} call processed successfully.", Context.ApiCallDescription);
                    }
                    else
                    {
                        callCompletionStatus = CallCompletionStatus.SuccessWithWarnings;
                        Context.Log.Warning("{0} call unsuccessfully processed.\r\n\r\nResultCode: {1}\r\n\r\nExplanation: {2}",
                                            (int)resultCode, Context.ApiCallDescription, resultCode,
                                            ResultCodeExplanation.Get(resultCode));
                    }
                }
                catch (Exception ex)
                {
                    Context.Log.Critical("{0} call ended with an error.", ex, Context.ApiCallDescription);
                    resultCode           = ResultCode.UnknownError;
                    callCompletionStatus = CallCompletionStatus.Error;
                }
            }

            // Build the response.
            HttpResponseMessage message = new HttpResponseMessage(MapResultToStatusCode(resultCode));

            message.Content = new ObjectContent <MasterCardAuthorizationResponse>(response, new XmlMediaTypeFormatter());

            // Log the result of the call.
            callTimer.Stop();
            Context.PerformanceInformation.Add("Total", String.Format("{0} ms", callTimer.ElapsedMilliseconds));
            Context.Log.CallCompletion(Context.ApiCallDescription, callCompletionStatus, Context.PerformanceInformation);

            return(message);
        }
Пример #5
0
        private static void BuildResponse(CommerceContext context,
                                          Exception ex,
                                          Action <HttpResponseMessage,
                                                  HttpStatusCode,
                                                  CallCompletionStatus> completeResponse)
        {
            ResultSummary resultSummary = (ResultSummary)context[Key.ResultSummary];

            // If an exception is specified, set the result code accordingly.
            if (ex != null)
            {
                resultSummary.SetResultCode(ResultCode.UnknownError);
            }

            // Log the response.
            object response = context[Key.Response];

            context.Log.Verbose("{0} response:\r\n{1}", context.ApiCallDescription, General.SerializeJson(response));

            // Get response status code.
            ResultCode     resultCode     = resultSummary.GetResultCode();
            HttpStatusCode httpStatusCode = MapResultToStatusCode(resultCode);

            // Log the result and get call completion status.
            CallCompletionStatus callCompletionStatus = CallCompletionStatus.Success;

            if (httpStatusCode == HttpStatusCode.OK ||
                httpStatusCode == HttpStatusCode.Created ||
                httpStatusCode == HttpStatusCode.Accepted)
            {
                context.Log.Information("{0} call processed successfully.", context.ApiCallDescription);
            }
            else
            {
                if (ex != null)
                {
                    callCompletionStatus = CallCompletionStatus.Error;
                    context.Log.Critical("{0} call ended with an error.", ex, context.ApiCallDescription);
                }
                else
                {
                    callCompletionStatus = CallCompletionStatus.SuccessWithWarnings;
                    context.Log.Warning("{0} call unsuccessfully processed.\r\n\r\nResultCode: {1}\r\n\r\nExplanation: {2}",
                                        (int)resultCode, context.ApiCallDescription, resultSummary.ResultCode,
                                        resultSummary.Explanation);
                }
            }

            // Next, build the HttpResponseMessage for the call.
            HttpResponseMessage httpResponseMessage = null;

            if (context[Key.Request] != null)
            {
                HttpRequestMessage request = (HttpRequestMessage)context[Key.Request];
                httpResponseMessage = request.CreateResponse(httpStatusCode, response);
                AddPerformanceInformation(context, request.RequestUri.Query, httpResponseMessage.Headers);
            }

            // Finally, complete the response.
            completeResponse(httpResponseMessage, httpStatusCode, callCompletionStatus);
        }
        /// <summary>
        /// Pings the FirstDataService.
        /// </summary>
        /// <param name="request">
        /// The ping request.
        /// </param>
        /// <returns>
        /// A ping response.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Parameter request cannot be null.
        /// </exception>
        public pubPingResponse1 pubPing(pubPingRequest1 request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request", "Parameter request cannot be null.");
            }

            Stopwatch callTimer = Stopwatch.StartNew();

            pubPingResponse1 result = new pubPingResponse1(new pubPingResponse
            {
                reqID = request.pubPingRequest.reqID
            });

            // Build a context object to pass down the pipeline.
            CommerceContext context = new CommerceContext("First Data ping");

            context[Key.Request]  = request.pubPingRequest;
            context[Key.Response] = result.pubPingResponse;
            context.Log.Exhaustive("Processing {0} call.", context.ApiCallDescription);

            CallCompletionStatus callCompletionStatus = CallCompletionStatus.Error;
            HttpStatusCode       httpStatusCode       = HttpStatusCode.OK;

            try
            {
                HttpClientCertificate httpClientCertificate = HttpContext.Current.Request.ClientCertificate;
                context.Log.Exhaustive("Presented client certificate has serial number \"{0}\".",
                                       httpClientCertificate.SerialNumber);
                if (General.IsPresentedCertValid(httpClientCertificate,
                                                 CommerceServiceConfig.Instance.FirstDataClientCertificateSerialNumbers) == true)
                {
                    FirstDataPingExecutor firstDataPingExecutor = new FirstDataPingExecutor(context);
                    firstDataPingExecutor.Execute();
                    context.Log.Exhaustive("{0} call processed successfully.", context.ApiCallDescription);
                    callCompletionStatus = CallCompletionStatus.Success;
                }
                else
                {
                    callCompletionStatus = CallCompletionStatus.SuccessWithWarnings;
#if !IntDebug && !IntRelease
                    context.Log.Warning("{0} call unsuccessfully processed.\r\n\r\nResultCode: {1}\r\n\r\nExplanation: {2}",
                                        (int)ResultCode.InvalidClientCertificate, context.ApiCallDescription,
                                        ResultCode.InvalidClientCertificate,
                                        ResultCodeExplanation.Get(ResultCode.InvalidClientCertificate));
#endif
                    httpStatusCode = HttpStatusCode.Unauthorized;
                }
            }
            catch (Exception ex)
            {
                context.Log.Critical("{0} call ended with an error.", ex, context.ApiCallDescription);
                httpStatusCode = HttpStatusCode.InternalServerError;
            }

            callTimer.Stop();
            context.PerformanceInformation.Add("Total", String.Format("{0} ms", callTimer.ElapsedMilliseconds));
            context.Log.ExhaustiveCallCompletion(context.ApiCallDescription, callCompletionStatus, context.PerformanceInformation);

            // Throw exception if the operation did not succeed.
            if (httpStatusCode != HttpStatusCode.OK)
            {
                throw new WebFaultException <pubPingResponse1>(result, httpStatusCode);
            }

            return(result);
        }