diff --git a/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb b/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb index 94a85f70..dc961d82 100644 --- a/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb +++ b/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb @@ -5,7 +5,7 @@ class ClientTokensController < Spree::Api::BaseController before_action :load_gateway def create - render json: { client_token: @gateway.generate_token, payment_method_id: @gateway.id } + render json: { client_token: generate_token, payment_method_id: @gateway.id } end private @@ -18,5 +18,12 @@ def load_gateway @gateway = ::SolidusPaypalBraintree::Gateway.where(active: true).merge(store_payment_methods_scope).first! end end + + def generate_token + @gateway.generate_token + rescue ::SolidusPaypalBraintree::Gateway::TokenGenerationDisabledError => error + Rails.logger.warn error + nil + end end end diff --git a/app/models/solidus_paypal_braintree/gateway.rb b/app/models/solidus_paypal_braintree/gateway.rb index 2c0e2f17..040fd320 100644 --- a/app/models/solidus_paypal_braintree/gateway.rb +++ b/app/models/solidus_paypal_braintree/gateway.rb @@ -4,6 +4,9 @@ module SolidusPaypalBraintree class Gateway < ::Spree::PaymentMethod include RequestProtection + class Error < StandardError; end + class TokenGenerationDisabledError < Error; end + # Error message from Braintree that gets returned by a non voidable transaction NON_VOIDABLE_STATUS_ERROR_REGEXP = /can only be voided if status is authorized/ @@ -213,12 +216,12 @@ def create_profile(payment) end end + # @raise [TokenGenerationDisabledError] + # If `preferred_token_generation_enabled` is false + # # @return [String] # The token that should be used along with the Braintree js-client sdk. # - # returns an error message if `preferred_token_generation_enabled` is - # set to false. - # # @example # def generate_token - return TOKEN_GENERATION_DISABLED_MESSAGE unless preferred_token_generation_enabled + unless preferred_token_generation_enabled + raise TokenGenerationDisabledError, TOKEN_GENERATION_DISABLED_MESSAGE + end + braintree.client_token.generate end diff --git a/spec/models/solidus_paypal_braintree/gateway_spec.rb b/spec/models/solidus_paypal_braintree/gateway_spec.rb index 7eb3a27d..fbd89b00 100644 --- a/spec/models/solidus_paypal_braintree/gateway_spec.rb +++ b/spec/models/solidus_paypal_braintree/gateway_spec.rb @@ -617,7 +617,7 @@ gateway end - it { is_expected.to match(/Token generation is disabled/) } + it { expect { subject }.to raise_error SolidusPaypalBraintree::Gateway::TokenGenerationDisabledError } end end end