class SpookAndPay::Adapters::Braintree

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

Attributes

gateway[R]

Accessor for the Braintree::Gateway instance. In general should not be accessed externally, but is put here for debugging etc.

Public Class Methods

new(environment, merchant_id, public_key, private_key) click to toggle source

Constructs an instance of the Braintree gateway which it then acts as a proxy to.

@param [:development, :test, :production] environment @param String merchant_id @param String public_key @param String private_key

# 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

Public Instance Methods

capture(id) click to toggle source

Captures the funds in an authorized transaction.

@param String id @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# File lib/spook_and_pay/adapters/braintree.rb, line 81
def capture(id)
  gateway.transaction.submit_for_settlement(id)
end
confirm(query_string) click to toggle source

Used to confirm the submission of purchase or authorize transactions via transparent redirect.

@param String query_string @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# File lib/spook_and_pay/adapters/braintree.rb, line 73
def confirm(query_string)
  gateway.transparent_redirect.confirm(query_string)
end
credit_card(id) click to toggle source

Looks up credit card details from Braintree. It squashes NotFoundError and just returns nil instead.

@param String id @return [Braintree::CreditCard, nil]

# 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) click to toggle source

Makes a purchase using a credit token.

@param String id @param Number amount @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# 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) click to toggle source

Partially refunds the funds in a settled transaction.

@param String id @param Float amount @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# File lib/spook_and_pay/adapters/braintree.rb, line 111
def partially_refund(id, amount)
  gateway.transaction.refund(id, amount)
end
refund(id) click to toggle source

Refunds the funds in a settled transaction.

@param String id @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# File lib/spook_and_pay/adapters/braintree.rb, line 102
def refund(id)
  gateway.transaction.refund(id)
end
transaction(id) click to toggle source

Looks up the transaction from Braintree.

@param String id @return [nil, Braintree::Transaction]

# File lib/spook_and_pay/adapters/braintree.rb, line 41
def transaction(id)
  gateway.transaction.find(id)
end
transaction_data(data) click to toggle source

Generates the hash and query string that needs to be embedded inside of a form in order to interact with Braintree's transparent redirect.

@param Hash data

@return String

# File lib/spook_and_pay/adapters/braintree.rb, line 64
def transaction_data(data)
  gateway.transparent_redirect.transaction_data(data)
end
transparent_redirect_url() click to toggle source

The target URL for transparent redirects.

@return String

# File lib/spook_and_pay/adapters/braintree.rb, line 126
def transparent_redirect_url
  gateway.transparent_redirect.url
end
void(id) click to toggle source

Voids a transaction.

@param String id @return [Braintree::SuccessfulResult, Braintree::ErrorResult]

# File lib/spook_and_pay/adapters/braintree.rb, line 119
def void(id)
  gateway.transaction.void(id)
end