Class: SpookAndPay::Adapters::Braintree
- Inherits:
-
Object
- Object
- SpookAndPay::Adapters::Braintree
- Defined in:
- lib/spook_and_pay/adapters/braintree.rb
Overview
A class which wraps the existing Braintree client and lets us use it in a sane way. Specifically, it lets us have multiple sets of credentials, whereas the default behaviour in the lib is to have them global
Instance Attribute Summary collapse
-
#gateway ⇒ Object
readonly
Accessor for the Braintree::Gateway instance.
Instance Method Summary collapse
-
#capture(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Captures the funds in an authorized transaction.
-
#confirm(query_string) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Used to confirm the submission of purchase or authorize transactions via transparent redirect.
-
#credit_card(id) ⇒ Braintree::CreditCard?
Looks up credit card details from Braintree.
-
#credit_card_purchase(id, amount) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Makes a purchase using a credit token.
-
#initialize(environment, merchant_id, public_key, private_key) ⇒ Braintree
constructor
Constructs an instance of the Braintree gateway which it then acts as a proxy to.
-
#partially_refund(id, amount) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Partially refunds the funds in a settled transaction.
-
#refund(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Refunds the funds in a settled transaction.
-
#transaction(id) ⇒ nil, Braintree::Transaction
Looks up the transaction from Braintree.
-
#transaction_data(data) ⇒ Object
Generates the hash and query string that needs to be embedded inside of a form in order to interact with Braintree’s transparent redirect.
-
#transparent_redirect_url ⇒ Object
The target URL for transparent redirects.
-
#void(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Voids a transaction.
Constructor Details
#initialize(environment, merchant_id, public_key, private_key) ⇒ Braintree
Constructs an instance of the Braintree gateway which it then acts as a proxy to.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 18 def initialize(environment, merchant_id, public_key, private_key) _environment = case environment when :production then :production when :development, :test then :sandbox end config = ::Braintree::Configuration.new( :custom_user_agent => ::Braintree::Configuration.instance_variable_get(:@custom_user_agent), :endpoint => ::Braintree::Configuration.instance_variable_get(:@endpoint), :environment => _environment, :logger => ::Braintree::Configuration.logger, :merchant_id => merchant_id, :private_key => private_key, :public_key => public_key ) @gateway = ::Braintree::Gateway.new(config) end |
Instance Attribute Details
#gateway ⇒ Object (readonly)
Accessor for the Braintree::Gateway instance. In general should not be accessed externally, but is put here for debugging etc.
9 10 11 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 9 def gateway @gateway end |
Instance Method Details
#capture(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Captures the funds in an authorized transaction.
81 82 83 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 81 def capture(id) gateway.transaction.submit_for_settlement(id) end |
#confirm(query_string) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Used to confirm the submission of purchase or authorize transactions via transparent redirect.
73 74 75 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 73 def confirm(query_string) gateway.transparent_redirect.confirm(query_string) end |
#credit_card(id) ⇒ Braintree::CreditCard?
Looks up credit card details from Braintree. It squashes NotFoundError and just returns nil instead.
50 51 52 53 54 55 56 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 50 def credit_card(id) begin gateway.credit_card.find(id) rescue ::Braintree::NotFoundError => e nil end end |
#credit_card_purchase(id, amount) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Makes a purchase using a credit token.
90 91 92 93 94 95 96 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 90 def credit_card_purchase(id, amount) gateway.transaction.sale( :payment_method_token => id, :amount => amount, :options => {:submit_for_settlement => true} ) end |
#partially_refund(id, amount) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Partially refunds the funds in a settled transaction.
111 112 113 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 111 def partially_refund(id, amount) gateway.transaction.refund(id, amount) end |
#refund(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Refunds the funds in a settled transaction.
102 103 104 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 102 def refund(id) gateway.transaction.refund(id) end |
#transaction(id) ⇒ nil, Braintree::Transaction
Looks up the transaction from Braintree.
41 42 43 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 41 def transaction(id) gateway.transaction.find(id) end |
#transaction_data(data) ⇒ Object
Generates the hash and query string that needs to be embedded inside of a form in order to interact with Braintree’s transparent redirect.
64 65 66 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 64 def transaction_data(data) gateway.transparent_redirect.transaction_data(data) end |
#transparent_redirect_url ⇒ Object
The target URL for transparent redirects.
126 127 128 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 126 def transparent_redirect_url gateway.transparent_redirect.url end |
#void(id) ⇒ Braintree::SuccessfulResult, Braintree::ErrorResult
Voids a transaction.
119 120 121 |
# File 'lib/spook_and_pay/adapters/braintree.rb', line 119 def void(id) gateway.transaction.void(id) end |