/// <summary> /// Void authorized transaction /// </summary> /// <param name="transactionId">Transaction ID</param> /// <returns>True if the transaction successfully voided; otherwise false. And/or errors if exist</returns> public PaymentResponse <bool> VoidTransaction(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); //void transaction by identifier var voidTransactionResponse = transactionsApi.VoidTransaction(selectedLocation.Id, transactionId); if (voidTransactionResponse == null) { throw new NopException("No service response"); } //check whether there are errors in the service response if (voidTransactionResponse.Errors?.Any() ?? false) { var errorsMessage = string.Join(";", voidTransactionResponse.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 voided 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 <VoidTransactionResponse>(apiException.ErrorContent) as VoidTransactionResponse; if (response?.Errors?.Any() ?? false) { errorMessage = string.Join(";", response.Errors.Select(error => error.Detail)); } } return(new PaymentResponse <bool> { Error = errorMessage }); } }
/// <summary> /// Void authorized transaction /// </summary> /// <param name="transactionId">Transaction ID</param> /// <returns>True if the transaction successfully voided; otherwise false</returns> public bool VoidTransaction(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); //void transaction by identifier var voidTransactionResponse = transactionsApi.VoidTransaction(selectedLocation.Id, transactionId); if (voidTransactionResponse == null) throw new NopException("No service response"); //check whether there are errors in the service response if (voidTransactionResponse.Errors?.Any() ?? false) { var errorsMessage = string.Join(";", voidTransactionResponse.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 voided 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; } }