Beispiel #1
0
        /// <summary>
        /// Get transaction by transaction identifier
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <returns>Transaction and/or errors if exist</returns>
        public PaymentResponse <Transaction> GetTransaction(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);

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

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

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

                return(new PaymentResponse <Transaction> {
                    Error = errorMessage
                });
            }
        }
        /// <summary>
        /// Get transaction by transaction identifier
        /// </summary>
        /// <param name="transactionId">Transaction ID</param>
        /// <returns>Transaction</returns>
        public Transaction GetTransaction(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);

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

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

                return retrieveTransactionResponse.Transaction;
            }
            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;
            }
        }