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
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 58

def initialize(options = {})
  requires!(options, :merchant_id, :public_key, :private_key)
  @options = options
  @merchant_account_id = options[:merchant_account_id]
  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 = wiredump_device
    Braintree::Configuration.logger.level = Logger::DEBUG
  end
  super
end

Instance Method Details

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



74
75
76
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 74

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



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

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



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

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



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

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 93

def refund(*args)
  # legacy signature: #refund(transaction_id, options = {})
  # new signature: #refund(money, transaction_id, options = {})
  money, transaction_id, options = 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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 118

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?)
      }
    )
  end
end

#unstore(customer_vault_id) ⇒ Object Also known as: delete



170
171
172
173
174
175
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 170

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

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 141

def update(vault_id, creditcard, options = {})
  braintree_credit_card = nil
  customer_update_result = 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



108
109
110
111
112
113
114
115
116
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 108

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