Example #1
0
        /// <summary>
        /// Create a refund of the transaction
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <param name="refundRequest">Request parameters to create refund</param>
        /// <returns>Refund and/or errors if exist</returns>
        public PaymentResponse <Refund> CreateRefund(string transactionId, CreateRefundRequest refundRequest)
        {
            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);

                //create refund
                var createRefundResponse = transactionsApi.CreateRefund(selectedLocation.Id, transactionId, refundRequest);
                if (createRefundResponse == null)
                {
                    throw new NopException("No service response");
                }

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

                return(new PaymentResponse <Refund> {
                    ResponseValue = createRefundResponse.Refund
                });
            }
            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 <CreateRefundResponse>(apiException.ErrorContent) as CreateRefundResponse;
                    if (response?.Errors?.Any() ?? false)
                    {
                        errorMessage = string.Join(";", response.Errors.Select(error => error.Detail));
                    }
                }

                return(new PaymentResponse <Refund> {
                    Error = errorMessage
                });
            }
        }
        /// <summary>
        /// Create a refund of the transaction
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <param name="refundRequest">Request parameters to create refund</param>
        /// <returns>Refund</returns>
        public Refund CreateRefund(string transactionId, CreateRefundRequest refundRequest)
        {
            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);

                //create refund
                var createRefundResponse = transactionsApi.CreateRefund(selectedLocation.Id, transactionId, refundRequest);
                if (createRefundResponse == null)
                    throw new NopException("No service response");

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

                return createRefundResponse.Refund;
            }
            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 null;
            }
        }