Example #1
0
        /// <summary>
        /// Capture authorized transaction
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <returns>True if the transaction successfully captured; otherwise false. And/or errors if exist</returns>
        public PaymentResponse <bool> CaptureTransaction(string transactionId)
        {
            try
            {
                //try to get the selected location
                var selectedLocation = GetActiveLocations().FirstOrDefault(location => location.Id.Equals(_squarePaymentSettings.LocationId));
                if (selectedLocation == null)
                {
                    throw new NopException("Location is a required parameter for payment requests");
                }

                //create transaction API
                var configuration   = CreateApiConfiguration();
                var transactionsApi = new TransactionsApi(configuration);

                //capture transaction by identifier
                var captureTransactionResponse = transactionsApi.CaptureTransaction(selectedLocation.Id, transactionId);
                if (captureTransactionResponse == null)
                {
                    throw new NopException("No service response");
                }

                //check whether there are errors in the service response
                if (captureTransactionResponse.Errors?.Any() ?? false)
                {
                    var errorsMessage = string.Join(";", captureTransactionResponse.Errors.Select(error => error.ToString()));
                    throw new NopException($"There are errors in the service response. {errorsMessage}");
                }

                //if there are no errors in the response, transaction was successfully captured
                return(new PaymentResponse <bool> {
                    ResponseValue = true
                });
            }
            catch (Exception exception)
            {
                //log full error
                var errorMessage = exception.Message;
                _logger.Error($"Square payment error: {errorMessage}.", exception, _workContext.CurrentCustomer);

                if (exception is ApiException apiException)
                {
                    //try to get error details
                    var response = JsonConvert.DeserializeObject <CaptureTransactionResponse>(apiException.ErrorContent) as CaptureTransactionResponse;
                    if (response?.Errors?.Any() ?? false)
                    {
                        errorMessage = string.Join(";", response.Errors.Select(error => error.Detail));
                    }
                }

                return(new PaymentResponse <bool> {
                    Error = errorMessage
                });
            }
        }
        /// <summary>
        /// Capture authorized transaction
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <returns>True if the transaction successfully captured; otherwise false</returns>
        public bool CaptureTransaction(string transactionId)
        {
            try
            {
                //try to get the selected location
                var selectedLocation = GetActiveLocations().FirstOrDefault(location => location.Id.Equals(_squarePaymentSettings.LocationId));
                if (selectedLocation == null)
                    throw new NopException("Location is a required parameter for payment requests");

                //create transaction API
                var configuration = CreateApiConfiguration();
                var transactionsApi = new TransactionsApi(configuration);

                //capture transaction by identifier
                var captureTransactionResponse = transactionsApi.CaptureTransaction(selectedLocation.Id, transactionId);
                if (captureTransactionResponse == null)
                    throw new NopException("No service response");

                //check whether there are errors in the service response
                if (captureTransactionResponse.Errors?.Any() ?? false)
                {
                    var errorsMessage = string.Join(";", captureTransactionResponse.Errors.Select(error => error.ToString()));
                    throw new NopException($"There are errors in the service response. {errorsMessage}");
                }

                //if there are no errors in the response, transaction was successfully captured
                return true;
            }
            catch (Exception exception)
            {
                var errorMessage = $"Square payment error: {exception.Message}.";
                if (exception is ApiException apiException)
                    errorMessage = $"{errorMessage} Details: {apiException.ErrorCode} - {apiException.ErrorContent}";

                //log errors
                _logger.Error(errorMessage, exception, _workContext.CurrentCustomer);

                return false;
            }
        }