From 35ceeae46a0eb136be395ef67410990d6b61b722 Mon Sep 17 00:00:00 2001 From: Alexey Zalyotov Date: Tue, 23 Nov 2021 15:53:39 +0300 Subject: [PATCH 1/2] Check if transaction is present before creating that --- app/models/spree/taxjar.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/spree/taxjar.rb b/app/models/spree/taxjar.rb index 0628f7a..fe5e43e 100644 --- a/app/models/spree/taxjar.rb +++ b/app/models/spree/taxjar.rb @@ -24,6 +24,10 @@ def create_transaction_for_order if has_nexus? api_params = transaction_parameters SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_params: api_params}) if SpreeTaxjar::Logger.logger_enabled? + taxjar_order = @client.show_order(@order.number) + return taxjar_order if taxjar_order.present? + + SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: api_response}) if SpreeTaxjar::Logger.logger_enabled? api_response = @client.create_order(api_params) SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: api_response}) if SpreeTaxjar::Logger.logger_enabled? api_response From 3c685ab0f95b8b90b8aea1f4614a5f25615b4ed0 Mon Sep 17 00:00:00 2001 From: Alexey Zalyotov Date: Tue, 23 Nov 2021 23:14:47 +0300 Subject: [PATCH 2/2] Catch an exception if the response contain an error --- app/models/spree/taxjar.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/spree/taxjar.rb b/app/models/spree/taxjar.rb index fe5e43e..19e211d 100644 --- a/app/models/spree/taxjar.rb +++ b/app/models/spree/taxjar.rb @@ -24,13 +24,13 @@ def create_transaction_for_order if has_nexus? api_params = transaction_parameters SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_params: api_params}) if SpreeTaxjar::Logger.logger_enabled? - taxjar_order = @client.show_order(@order.number) - return taxjar_order if taxjar_order.present? - - SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: api_response}) if SpreeTaxjar::Logger.logger_enabled? - api_response = @client.create_order(api_params) - SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: api_response}) if SpreeTaxjar::Logger.logger_enabled? - api_response + begin + api_response = @client.create_order(api_params) + SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: api_response}) if SpreeTaxjar::Logger.logger_enabled? + api_response + rescue + SpreeTaxjar::Logger.log(__method__, {order: {id: @order.id, number: @order.number}, api_response: 'Response with an error'}) if SpreeTaxjar::Logger.logger_enabled? + end end end