public async Task <SquareCustomer> GetCustomerByIdAsync(string customerId, CancellationToken token, Mark mark)
        {
            Condition.Requires(customerId, "customerId").IsNotNullOrWhiteSpace();

            if (token.IsCancellationRequested)
            {
                var exceptionDetails = CreateMethodCallInfo(SquareEndPoint.RetrieveCustomerByIdUrl, mark, additionalInfo: this.AdditionalLogInfo());
                var squareException  = new SquareException(string.Format("{0}. Get customer by id request was cancelled", exceptionDetails));
                SquareLogger.LogTraceException(squareException);
                throw squareException;
            }

            var response = await base.ThrottleRequest(SquareEndPoint.RetrieveCustomerByIdUrl, customerId.ToJson(), mark, (_) =>
            {
                return(Task.FromResult(_customersApi.RetrieveCustomer(customerId)));
            }, token).ConfigureAwait(false);

            var errors = response.Errors;

            if (errors != null && errors.Any())
            {
                var methodCallInfo  = CreateMethodCallInfo(SquareEndPoint.RetrieveCustomerByIdUrl, mark, additionalInfo: this.AdditionalLogInfo(), errors: errors.ToJson());
                var squareException = new SquareException(string.Format("{0}. Get customer returned errors", methodCallInfo));
                SquareLogger.LogTraceException(squareException);
                throw squareException;
            }

            return(response.Customer.ToSvCustomer());
        }
예제 #2
0
        /// <summary>
        /// Get customer by identifier
        /// </summary>
        /// <param name="customerId">Customer ID</param>
        /// <returns>Customer</returns>
        public Customer GetCustomer(string customerId)
        {
            try
            {
                //whether passed customer identifier exists
                if (string.IsNullOrEmpty(customerId))
                {
                    return(null);
                }

                //create customer API
                var configuration = CreateApiConfiguration();
                var customersApi  = new CustomersApi(configuration);

                //get customer by identifier
                var retrieveCustomerResponse = customersApi.RetrieveCustomer(customerId);
                if (retrieveCustomerResponse == null)
                {
                    throw new NopException("No service response");
                }

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

                return(retrieveCustomerResponse.Customer);
            }
            catch (Exception exception)
            {
                //log full error
                _logger.Error($"Square payment error: {exception.Message}.", exception, _workContext.CurrentCustomer);

                return(null);
            }
        }