Class: Freemium::Gateways::BrainTree

Inherits:
Base
  • Object
show all
Defined in:
lib/freemium/gateways/brain_tree.rb

Overview

Setup and Configuration

In your config/initializers/freemium.rb, configure Freemium to use BrainTree:

Freemium.gateway = Freemium::Gateways::BrainTree.new
Freemium.gateway.username = "my_username"
Freemium.gateway.password = "my_password"

Note that if you want to use demo/password credentials when not in production mode, this is the place.

Data Structures

All amounts should use the Money class (from eponymous gem). All credit cards should use Freemium::CreditCard class (currently just an alias for ActiveMerchant::Billing::CreditCard). All addresses should use Freemium::Address class.

For Testing

The URL does not change. If your account is in test mode, no charges will be processed. Otherwise, configure the username and password to be “demo” and “password”, respectively.

Defined Under Namespace

Classes: Post

Constant Summary collapse

URL =
'https://secure.braintreepaymentgateway.com/api/transact.php'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#transactions

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/freemium/gateways/brain_tree.rb', line 24

def password
  @password
end

#usernameObject

Returns the value of attribute username.



24
25
26
# File 'lib/freemium/gateways/brain_tree.rb', line 24

def username
  @username
end

Instance Method Details

#cancel(vault_id) ⇒ Object

Removes a card from SecureVault. Called automatically when the subscription expires.



71
72
73
74
75
76
77
78
79
80
# File 'lib/freemium/gateways/brain_tree.rb', line 71

def cancel(vault_id)
  p = Post.new(URL, {
    :username => self.username,
    :password => self.password,
    :customer_vault => 'delete_customer',
    :customer_vault_id => vault_id
  })
  p.commit(open_timeout, read_timeout)
  return p.response
end

#charge(vault_id, amount) ⇒ Object

Manually charges a card in SecureVault. Called automatically as part of manual billing process.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/freemium/gateways/brain_tree.rb', line 57

def charge(vault_id, amount)
  p = Post.new(URL, {
    :username => self.username,
    :password => self.password,
    :customer_vault_id => vault_id,
    :amount => sprintf("%.2f", amount.cents.to_f / 100)
  })
  p.commit(open_timeout, read_timeout)
  transaction = AccountTransaction.new(:billing_key => vault_id, :amount => amount, :success => p.response.success?)
  transaction.response = p.response if transaction.respond_to?(:response=)
  return transaction
end

#store(credit_card, address = nil) ⇒ Object

Stores a card in SecureVault.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/freemium/gateways/brain_tree.rb', line 30

def store(credit_card, address = nil)
  p = Post.new(URL, {
    :username => self.username,
    :password => self.password,
    :customer_vault => "add_customer"
  })
  p.params.merge! params_for_credit_card(credit_card)
  p.params.merge! params_for_address(address)         if address
  p.commit(open_timeout, read_timeout)
  return p.response
end

#update(vault_id, credit_card = nil, address = nil) ⇒ Object

Updates a card in SecureVault.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/freemium/gateways/brain_tree.rb', line 43

def update(vault_id, credit_card = nil, address = nil)
  p = Post.new(URL, {
    :username => self.username,
    :password => self.password,
    :customer_vault => "update_customer",
    :customer_vault_id => vault_id
  })
  p.params.merge! params_for_credit_card(credit_card) if credit_card
  p.params.merge! params_for_address(address)         if address
  p.commit(open_timeout, read_timeout)
  return p.response
end

#validate(credit_card, address = nil) ⇒ Object

Validates the card.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/freemium/gateways/brain_tree.rb', line 83

def validate(credit_card, address = nil)
  # Assume we validated if we're using a demo account
  return Freemium::Response.new(true) if self.username == 'demo'

  p = Post.new(URL, {
    :username => self.username,
    :password => self.password,
    :type => 'validate'
  })
  p.params.merge! params_for_credit_card(credit_card)
  if address
    p.params.merge! params_for_address(address)
  end

  p.commit(open_timeout, read_timeout)
  return p.response
end