Class: ActiveMerchant::Billing::BraintreeBlueGateway

Inherits:
Gateway
  • Object
show all
Includes:
BraintreeCommon
Defined in:
lib/active_merchant/billing/gateways/braintree_blue.rb

Overview

For more information on the Braintree Gateway please visit their Developer Portal

About this implementation

This implementation leverages the Braintree-authored ruby gem: github.com/braintree/braintree_ruby

Debugging Information

Setting an ActiveMerchant wiredump_device will automatically configure the Braintree logger (via the Braintree gem’s configuration) when the BraintreeBlueGateway is instantiated. Additionally, the log level will be set to DEBUG. Therefore, all you have to do is set the wiredump_device and you’ll get your debug output from your HTTP interactions with the remote gateway. (Don’t enable this in production.)

For example:

   ActiveMerchant::Billing::BraintreeBlueGateway.wiredump_device = Logger.new(STDOUT)
   # => #<Logger:0x107d385f8 ...>

   Braintree::Configuration.logger
   # => (some other logger, created by default by the gem)

   Braintree::Configuration.logger.level
   # => 1 (INFO)

   ActiveMerchant::Billing::BraintreeBlueGateway.new(:merchant_id => 'x', :public_key => 'x', :private_key => 'x')

   Braintree::Configuration.logger
   # => #<Logger:0x107d385f8 ...>

   Braintree::Configuration.logger.level
   # => 0 (DEBUG)

Alternatively, you can avoid setting the +wiredump_device+
and set +Braintree::Configuration.logger+ and/or
+Braintree::Configuration.logger.level+ directly.

Constant Summary

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::CURRENCIES_WITHOUT_FRACTIONS, Gateway::DEBIT_CARDS

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods included from BraintreeCommon

included

Methods inherited from Gateway

#card_brand, card_brand, inherited, supports?, #test?

Methods included from CreditCardFormatting

#format

Constructor Details

#initialize(options = {}) ⇒ BraintreeBlueGateway

Returns a new instance of BraintreeBlueGateway.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 58

def initialize(options = {})
  requires!(options, :merchant_id, :public_key, :private_key)
  @merchant_account_id = options[:merchant_account_id]

  super

  Braintree::Configuration.merchant_id = options[:merchant_id]
  Braintree::Configuration.public_key = options[:public_key]
  Braintree::Configuration.private_key = options[:private_key]
  Braintree::Configuration.environment = (options[:environment] || (test? ? :sandbox : :production)).to_sym
  Braintree::Configuration.custom_user_agent = "ActiveMerchant #{ActiveMerchant::VERSION}"

  if wiredump_device
    Braintree::Configuration.logger = ((Logger === wiredump_device) ? wiredump_device : Logger.new(wiredump_device))
    Braintree::Configuration.logger.level = Logger::DEBUG
  else
    Braintree::Configuration.logger.level = Logger::WARN
  end
end

Instance Method Details

#authorize(money, credit_card_or_vault_id, options = {}) ⇒ Object



78
79
80
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 78

def authorize(money, credit_card_or_vault_id, options = {})
  create_transaction(:sale, money, credit_card_or_vault_id, options)
end

#capture(money, authorization, options = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 82

def capture(money, authorization, options = {})
  commit do
    result = Braintree::Transaction.submit_for_settlement(authorization, amount(money).to_s)
    Response.new(result.success?, message_from_result(result))
  end
end

#credit(money, credit_card_or_vault_id, options = {}) ⇒ Object



93
94
95
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 93

def credit(money, credit_card_or_vault_id, options = {})
  create_transaction(:credit, money, credit_card_or_vault_id, options)
end

#purchase(money, credit_card_or_vault_id, options = {}) ⇒ Object



89
90
91
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 89

def purchase(money, credit_card_or_vault_id, options = {})
  authorize(money, credit_card_or_vault_id, options.merge(:submit_for_settlement => true))
end

#refund(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 97

def refund(*args)
  # legacy signature: #refund(transaction_id, options = {})
  # new signature: #refund(money, transaction_id, options = {})
  money, transaction_id, _ = extract_refund_args(args)
  money = amount(money).to_s if money

  commit do
    result = Braintree::Transaction.refund(transaction_id, money)
    Response.new(result.success?, message_from_result(result),
      {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)},
      {:authorization => (result.transaction.id if result.success?)}
     )
  end
end

#store(creditcard, options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 122

def store(creditcard, options = {})
  commit do
    parameters = {
      :first_name => creditcard.first_name,
      :last_name => creditcard.last_name,
      :email => options[:email],
      :credit_card => {
        :number => creditcard.number,
        :cvv => creditcard.verification_value,
        :expiration_month => creditcard.month.to_s.rjust(2, "0"),
        :expiration_year => creditcard.year.to_s
      }
    }
    result = Braintree::Customer.create(merge_credit_card_options(parameters, options))
    Response.new(result.success?, message_from_result(result),
      {
        :braintree_customer => (customer_hash(result.customer) if result.success?),
        :customer_vault_id => (result.customer.id if result.success?)
      },
      :authorization => (result.customer.id if result.success?)
    )
  end
end

#unstore(customer_vault_id, options = {}) ⇒ Object Also known as: delete



175
176
177
178
179
180
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 175

def unstore(customer_vault_id, options = {})
  commit do
    Braintree::Customer.delete(customer_vault_id)
    Response.new(true, "OK")
  end
end

#update(vault_id, creditcard, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 146

def update(vault_id, creditcard, options = {})
  braintree_credit_card = nil
  commit do
    braintree_credit_card = Braintree::Customer.find(vault_id).credit_cards.detect { |cc| cc.default? }
    return Response.new(false, 'Braintree::NotFoundError') if braintree_credit_card.nil?

    options.merge!(:update_existing_token => braintree_credit_card.token)
    credit_card_params = merge_credit_card_options({
      :credit_card => {
        :number => creditcard.number,
        :cvv => creditcard.verification_value,
        :expiration_month => creditcard.month.to_s.rjust(2, "0"),
        :expiration_year => creditcard.year.to_s
      }
    }, options)[:credit_card]

    result = Braintree::Customer.update(vault_id,
      :first_name => creditcard.first_name,
      :last_name => creditcard.last_name,
      :email => options[:email],
      :credit_card => credit_card_params
    )
    Response.new(result.success?, message_from_result(result),
      :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?),
      :customer_vault_id => (result.customer.id if result.success?)
    )
  end
end

#void(authorization, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 112

def void(authorization, options = {})
  commit do
    result = Braintree::Transaction.void(authorization)
    Response.new(result.success?, message_from_result(result),
      {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)},
      {:authorization => (result.transaction.id if result.success?)}
    )
  end
end