Skip to content

Commit

Permalink
Add option to send extra options to Braintree
Browse files Browse the repository at this point in the history
We'll be able to use this to send along the device data for fraud
prevention. (As well as slowly build it out to support other Braintree
options.)
  • Loading branch information
adammathys committed Sep 6, 2016
1 parent 11e2efb commit e3224a9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 448 deletions.
33 changes: 27 additions & 6 deletions app/models/solidus_paypal_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Gateway < ::Spree::PaymentMethod
store_in_vault_on_success: true
}.freeze

ALLOWED_BRAINTREE_OPTIONS = [
:device_data,
:device_session_id,
:merchant_account_id,
:order_id
]

# This is useful in feature tests to avoid rate limited requests from
# Braintree
preference(:client_sdk_enabled, :boolean, default: true)
Expand All @@ -32,12 +39,14 @@ def payment_source_class
# @api public
# @param money_cents [Number, String] amount to authorize
# @param source [Source] payment source
# @params gateway_options [Hash]
# extra options to send along. e.g.: device data for fraud prevention
# @return [Response]
def purchase(money_cents, source, _gateway_options)
def purchase(money_cents, source, gateway_options)
result = ::Braintree::Transaction.sale(
amount: dollars(money_cents),
payment_method_nonce: source.nonce,
options: PAYPAL_OPTIONS
options: PAYPAL_OPTIONS,
**transaction_options(source, gateway_options)
)

Response.build(result)
Expand All @@ -48,12 +57,14 @@ def purchase(money_cents, source, _gateway_options)
# @api public
# @param money_cents [Number, String] amount to authorize
# @param source [Source] payment source
# @params gateway_options [Hash]
# extra options to send along. e.g.: device data for fraud prevention
# @return [Response]
def authorize(money_cents, source, _gateway_options)
def authorize(money_cents, source, gateway_options)
result = ::Braintree::Transaction.sale(
amount: dollars(money_cents),
payment_method_nonce: source.nonce,
options: PAYPAL_AUTHORIZE_OPTIONS
options: PAYPAL_AUTHORIZE_OPTIONS,
**transaction_options(source, gateway_options)
)

Response.build(result)
Expand Down Expand Up @@ -134,5 +145,15 @@ def payment_profiles_supported?
def dollars(cents)
Money.new(cents).dollars
end

def transaction_options(source, options)
params = options.select do |key, _|
ALLOWED_BRAINTREE_OPTIONS.include?(key)
end

params[:payment_method_nonce] = source.nonce

params
end
end
end
183 changes: 40 additions & 143 deletions spec/fixtures/cassettes/braintree/authorize.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3224a9

Please sign in to comment.