Class: ActiveMerchant::Billing::BraintreeBlueGateway

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

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.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 18

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.logger.level = Logger::ERROR if Braintree::Configuration.logger
  Braintree::Configuration.custom_user_agent = "ActiveMerchant #{ActiveMerchant::VERSION}"
  super
end

Instance Method Details

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



31
32
33
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 31

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



35
36
37
38
39
40
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 35

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



46
47
48
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 46

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



42
43
44
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 42

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 50

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 75

def store(creditcard, options = {})
  commit do
    result = Braintree::Customer.create(
      :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
      }
    )
    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



124
125
126
127
128
129
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 124

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 97

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?
    result = Braintree::Customer.update(vault_id,
      :first_name => creditcard.first_name,
      :last_name => creditcard.last_name,
      :email => options[:email]
    )
    Response.new(result.success?, message_from_result(result),
      :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?)
    )
  end
  return customer_update_result unless customer_update_result.success?
  credit_card_update_result = commit do
    result = Braintree::CreditCard.update(braintree_credit_card.token,
        :number => creditcard.number,
        :expiration_month => creditcard.month.to_s.rjust(2, "0"),
        :expiration_year => creditcard.year.to_s
    )
    Response.new(result.success?, message_from_result(result),
      :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?)
    )
  end
end

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



65
66
67
68
69
70
71
72
73
# File 'lib/active_merchant/billing/gateways/braintree_blue.rb', line 65

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