Class: ActiveMerchant::Billing::BraintreeBlueGateway

Inherits:
Gateway
  • Object
show all
Includes:
Empty, 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 collapse

ERROR_CODES =
{
  cannot_refund_if_unsettled: 91506
}
DIRECT_BANK_ERROR =
'Direct bank account transactions are not supported. Bank accounts must be successfully stored before use.'.freeze

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods included from BraintreeCommon

included, #scrub, #supports_scrubbing

Methods inherited from Gateway

#add_field_to_post_if_present, #add_fields_to_post_if_present, #card_brand, card_brand, #generate_unique_id, inherited, #scrub, #supported_countries, supported_countries, supported_countries=, supports?, #supports_scrubbing?, #test?

Methods included from CreditCardFormatting

#expdate, #format, #strftime_yyyymm

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(options = {}) ⇒ BraintreeBlueGateway

Returns a new instance of BraintreeBlueGateway.



52
53
54
55
56
57
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 52

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

  super

  if wiredump_device.present?
    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: options[:logger] || logger
  )

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

Instance Method Details

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



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

def authorize(money, credit_card_or_vault_id, options = {})
  return Response.new(false, DIRECT_BANK_ERROR) if credit_card_or_vault_id.is_a? Check

  create_transaction(:sale, money, credit_card_or_vault_id, options)
end

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



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

def capture(money, authorization, options = {})
  if options[:partial_capture] == true
    commit do
      result = @braintree_gateway.transaction.submit_for_partial_settlement(authorization, localized_amount(money, options[:currency] || default_currency).to_s)
      response_from_result(result)
    end
  else
    commit do
      result = @braintree_gateway.transaction.submit_for_settlement(authorization, localized_amount(money, options[:currency] || default_currency).to_s)
      response_from_result(result)
    end
  end
end

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



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

def credit(money, credit_card_or_vault_id, options = {})
  return Response.new(false, DIRECT_BANK_ERROR) if credit_card_or_vault_id.is_a? Check

  create_transaction(:credit, money, credit_card_or_vault_id, options)
end

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



107
108
109
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 107

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



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

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 = localized_amount(money, options[:currency] || default_currency).to_s if money

  commit do
    response = response_from_result(@braintree_gateway.transaction.refund(transaction_id, money))

    if !response.success? && options[:force_full_refund_if_unsettled] &&
       response.message =~ /#{ERROR_CODES[:cannot_refund_if_unsettled]}/
      void(transaction_id)
    else
      response
    end
  end
end

#setup_purchase(options = {}) ⇒ Object



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

def setup_purchase(options = {})
  post = {}
  (post, options)

  commit do
    Response.new(true, 'Client token created', { client_token: @braintree_gateway.client_token.generate(post) })
  end
end

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



183
184
185
186
187
188
189
190
191
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 183

def store(payment_method, options = {})
  return Response.new(false, (payment_method, options)) if payment_method.is_a?(Check) && (payment_method, options).present?

  MultiResponse.run do |r|
    r.process { check_customer_exists(options[:customer]) }
    process_by = payment_method.is_a?(Check) ? :store_bank_account : :store_credit_card
    send process_by, payment_method, options, r
  end
end

#supports_network_tokenization?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 239

def supports_network_tokenization?
  true
end

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



227
228
229
230
231
232
233
234
235
236
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 227

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 193

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

    options[:update_existing_token] = braintree_credit_card.token
    credit_card_params = merge_credit_card_options({
      credit_card: {
        cardholder_name: creditcard.name,
        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]),
      phone: phone_from(options),
      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

#verify(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
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 141

def verify(creditcard, options = {})
  if options[:allow_card_verification] == true
    options.delete(:allow_card_verification)
    exp_month = creditcard.month.to_s
    exp_year = creditcard.year.to_s
    expiration = "#{exp_month}/#{exp_year}"
    zip = options[:billing_address].try(:[], :zip)
    address1 = options[:billing_address].try(:[], :address1)
    payload = {
      credit_card: {
        number: creditcard.number,
        expiration_date: expiration,
        cvv: creditcard.verification_value
      }
    }
    if zip || address1
      payload[:credit_card][:billing_address] = {}
      payload[:credit_card][:billing_address][:postal_code] = zip if zip
      payload[:credit_card][:billing_address][:street_address] = address1 if address1
    end

    if  = (options[:merchant_account_id] || @merchant_account_id)
      payload[:options] = { merchant_account_id:  }
    end

    commit do
      result = @braintree_gateway.verification.create(payload)
      response = Response.new(result.success?, message_from_transaction_result(result), response_options(result))
      response.cvv_result['message'] = ''
      response.cvv_result['code'] = response.params['cvv_result'] if response.params['cvv_result']
      response.avs_result['code'] = response.params['avs_result'][:code] if response.params.dig('avs_result', :code)
      response
    end

  else
    MultiResponse.run(:use_first_response) do |r|
      r.process { authorize(100, creditcard, options) }
      r.process(:ignore_result) { void(r.authorization, options) }
    end
  end
end

#verify_credentialsObject



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 243

def verify_credentials
  begin
    @braintree_gateway.transaction.find('non_existent_token')
  rescue Braintree::AuthenticationError
    return false
  rescue Braintree::NotFoundError
    return true
  end

  true
end

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



135
136
137
138
139
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 135

def void(authorization, options = {})
  commit do
    response_from_result(@braintree_gateway.transaction.void(authorization))
  end
end