Class: SpookAndPay::Adapters::Braintree

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • environment (:development, :test, :production)
  • String

    merchant_id

  • String

    public_key

  • String

    private_key



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

#gatewayObject (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.

Parameters:

  • String

    id

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


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.

Parameters:

  • String

    query_string

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


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.

Parameters:

  • String

    id

Returns:

  • (Braintree::CreditCard, nil)


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.

Parameters:

  • String

    id

  • Number

    amount

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


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.

Parameters:

  • String

    id

  • Float

    amount

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


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.

Parameters:

  • String

    id

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


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.

Parameters:

  • String

    id

Returns:

  • (nil, Braintree::Transaction)


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.

Parameters:

  • Hash

    data

Returns:

  • String



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_urlObject

The target URL for transparent redirects.

Returns:

  • String



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.

Parameters:

  • String

    id

Returns:

  • (Braintree::SuccessfulResult, Braintree::ErrorResult)


119
120
121
# File 'lib/spook_and_pay/adapters/braintree.rb', line 119

def void(id)
  gateway.transaction.void(id)
end