Skip to content

Commit

Permalink
Merge pull request solidusio#9 from adammathys/transaction-options
Browse files Browse the repository at this point in the history
Add option to send extra options to Braintree
  • Loading branch information
stewart authored Sep 6, 2016
2 parents 11e2efb + e3224a9 commit ef11a9c
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 ef11a9c

Please sign in to comment.