Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Fix ambiguous behavior in generate_tokenn method
Browse files Browse the repository at this point in the history
The `SolidusPaypalBraintree::Gateway#generate_token` method is ambiguous
on success and failure. Both results will return a string. To be more
explicit, this method should raise an error which can be handled by the
app.
  • Loading branch information
skukx committed Feb 4, 2019
1 parent dde562b commit 40b99a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.error error
nil
end
end
end
13 changes: 9 additions & 4 deletions app/models/solidus_paypal_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module SolidusPaypalBraintree
class Gateway < ::Spree::PaymentMethod
include RequestProtection

class TokenGenerationDisabledError < StandardError; 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/

Expand Down Expand Up @@ -213,12 +215,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
# <script>
# var token = #{Spree::Braintree::Gateway.first!.generate_token}
Expand All @@ -233,7 +235,10 @@ def create_profile(payment)
# );
# </script>
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

Expand Down
2 changes: 1 addition & 1 deletion spec/models/solidus_paypal_braintree/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 40b99a9

Please sign in to comment.