lib/active_merchant/billing/gateways/braintree_blue.rb in jelaniharris-activemerchant-1.24.1 vs lib/active_merchant/billing/gateways/braintree_blue.rb in jelaniharris-activemerchant-1.29.1
- old
+ new
@@ -8,26 +8,71 @@
raise "Need braintree gem 2.x.y. Run `gem install braintree --version '~>2.0'` to get the correct version." unless Braintree::Version::Major == 2
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
+ # For more information on the Braintree Gateway please visit their
+ # {Developer Portal}[https://www.braintreepayments.com/developers]
+ #
+ # ==== About this implementation
+ #
+ # This implementation leverages the Braintree-authored ruby gem:
+ # https://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.
class BraintreeBlueGateway < Gateway
include BraintreeCommon
self.display_name = 'Braintree (Blue Platform)'
def initialize(options = {})
requires!(options, :merchant_id, :public_key, :private_key)
- @options = options
@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.logger.level = Logger::ERROR if Braintree::Configuration.logger
Braintree::Configuration.custom_user_agent = "ActiveMerchant #{ActiveMerchant::VERSION}"
- super
+
+ if wiredump_device
+ Braintree::Configuration.logger = wiredump_device
+ Braintree::Configuration.logger.level = Logger::DEBUG
+ end
end
def authorize(money, credit_card_or_vault_id, options = {})
create_transaction(:sale, money, credit_card_or_vault_id, options)
end
@@ -48,11 +93,11 @@
end
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, 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),
@@ -95,18 +140,19 @@
end
end
def update(vault_id, creditcard, options = {})
braintree_credit_card = nil
- customer_update_result = commit do
+ 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]
@@ -231,11 +277,14 @@
def customer_hash(customer)
credit_cards = customer.credit_cards.map do |cc|
{
"bin" => cc.bin,
"expiration_date" => cc.expiration_date,
- "token" => cc.token
+ "token" => cc.token,
+ "last_4" => cc.last_4,
+ "card_type" => cc.card_type,
+ "masked_number" => cc.masked_number
}
end
{
"email" => customer.email,
@@ -281,14 +330,21 @@
"locality" => transaction.shipping_details.locality,
"region" => transaction.shipping_details.region,
"postal_code" => transaction.shipping_details.postal_code,
"country_name" => transaction.shipping_details.country_name,
}
+ credit_card_details = {
+ "masked_number" => transaction.credit_card_details.masked_number,
+ "bin" => transaction.credit_card_details.bin,
+ "last_4" => transaction.credit_card_details.last_4,
+ "card_type" => transaction.credit_card_details.card_type,
+ }
{
"order_id" => transaction.order_id,
"status" => transaction.status,
+ "credit_card_details" => credit_card_details,
"customer_details" => customer_details,
"billing_details" => billing_details,
"shipping_details" => shipping_details,
"vault_customer" => vault_customer,
"merchant_account_id" => transaction.merchant_account_id
@@ -306,12 +362,18 @@
:options => {
:store_in_vault => options[:store] ? true : false,
:submit_for_settlement => options[:submit_for_settlement]
}
}
+
if merchant_account_id = (options[:merchant_account_id] || @merchant_account_id)
parameters[:merchant_account_id] = merchant_account_id
end
+
+ if options[:recurring]
+ parameters[:recurring] = true
+ end
+
if credit_card_or_vault_id.is_a?(String) || credit_card_or_vault_id.is_a?(Integer)
parameters[:customer_id] = credit_card_or_vault_id
else
parameters[:customer].merge!(
:first_name => credit_card_or_vault_id.first_name,