예제 #1
0
        /// <summary>
        /// Logs the authorization response parameters sent to MasterCard.
        /// </summary>
        /// <param name="response">
        /// The authorization response object.
        /// </param>
        private void LogAuthorizationResponseParameters(MasterCardAuthorizationResponse response)
        {
            StringBuilder parameters = new StringBuilder("ResponseCode: ");

            parameters.Append(response.ResponseCode);
            Context.Log.Verbose("MasterCard Authorization response parameters:\r\n{0}", parameters.ToString());
        }
예제 #2
0
        /// <summary>
        /// Construct response for MasterCard Authorization Calls.
        /// </summary>
        /// <remarks>
        /// MasterCard only pays attention to the HTTP status code, but does record information sent back to them.
        /// Because of this, the result code's int value will be sent for bookkeeping purposes.
        /// </remarks>
        public void BuildAuthorizationResponse()
        {
            MasterCardAuthorizationResponse response = (MasterCardAuthorizationResponse)Context[Key.Response];
            ResultCode resultCode = (ResultCode)Context[Key.ResultCode];

            response.ResponseCode = ((int)resultCode).ToString();
            LogAuthorizationResponseParameters(response);
        }
        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);
        }