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.) The ActiveMerchant implementation doesn’t mess with the Braintree::Configuration globals at all, so there won’t be any side effects outside Active Merchant.

If no wiredump_device is set, the logger in Braintree::Configuration.logger will be cloned and the log level set to WARN.

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 43

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

  super

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

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

  @braintree_gateway = Braintree::Gateway.new( @configuration )
end

Instance Method Details

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



69
70
71
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 69

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



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

def capture(money, authorization, options = {})
  commit do
    result = @braintree_gateway.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



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

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



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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 88

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_gateway.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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 113

def store(creditcard, options = {})
  if options[:customer].present?
    MultiResponse.new.tap do |r|
      customer_exists_response = nil
      r.process{customer_exists_response = check_customer_exists(options[:customer])}
      r.process do
        if customer_exists_response.params["exists"]
          add_credit_card_to_customer(creditcard, options)
        else
          add_customer_with_credit_card(creditcard, options)
        end
      end
    end
  else
    add_customer_with_credit_card(creditcard, options)
  end
end

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



160
161
162
163
164
165
166
167
168
169
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 160

def unstore(customer_vault_id, options = {})
  commit do
    if(!customer_vault_id && options[:credit_card_token])
      @braintree_gateway.credit_card.delete(options[:credit_card_token])
    else
      @braintree_gateway.customer.delete(customer_vault_id)
    end
    Response.new(true, "OK")
  end
end

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 131

def update(vault_id, creditcard, options = {})
  braintree_credit_card = nil
  commit do
    braintree_credit_card = @braintree_gateway.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_gateway.customer.update(vault_id,
      :first_name => creditcard.first_name,
      :last_name => creditcard.last_name,
      :email => scrub_email(options[:email]),
      :credit_card => credit_card_params
    )
    Response.new(result.success?, message_from_result(result),
      :braintree_customer => (customer_hash(@braintree_gateway.customer.find(vault_id), :include_credit_cards) if result.success?),
      :customer_vault_id => (result.customer.id if result.success?)
    )
  end
end

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



103
104
105
106
107
108
109
110
111
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 103

def void(authorization, options = {})
  commit do
    result = @braintree_gateway.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