Class: Gateway::Braintree

Inherits:
Gateway show all
Defined in:
app/models/gateway/braintree.rb

Constant Summary

Constants inherited from PaymentMethod

PaymentMethod::DISPLAY

Instance Method Summary collapse

Methods inherited from Gateway

current, #method_missing, #method_type, #options, #payment_source_class, #provider

Methods inherited from PaymentMethod

active?, available, current, #destroy, find_with_destroyed, #method_type, #payment_source_class, providers, register, #source_required?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Gateway

Instance Method Details

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



10
11
12
13
14
# File 'app/models/gateway/braintree.rb', line 10

def authorize(money, creditcard, options = {})
  adjust_options_for_braintree(creditcard, options)
  payment_method = creditcard.gateway_customer_profile_id || creditcard
  provider.authorize(money, payment_method, options)
end

#capture(authorization, ignored_creditcard, ignored_options) ⇒ Object



16
17
18
19
# File 'app/models/gateway/braintree.rb', line 16

def capture(authorization, ignored_creditcard, ignored_options)
  amount = (authorization.amount * 100).to_i
  provider.capture(amount, authorization.response_code)
end

#create_profile(payment) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'app/models/gateway/braintree.rb', line 21

def create_profile(payment)
  if payment.source.gateway_customer_profile_id.nil?
    response = provider.store(payment.source)
    if response.success?
      payment.source.update_attributes!(:gateway_customer_profile_id => response.params["customer_vault_id"])
    else
      payment.source.gateway_error response.message
    end
  end
end

#credit(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/models/gateway/braintree.rb', line 32

def credit(*args)
  if args.size == 4
    credit_with_payment_profiles(*args)
  elsif args.size == 3
    credit_without_payment_profiles(*args)
  else
    raise ArgumentError, "Expected 3 or 4 arguments, received #{args.size}"
  end
end

#credit_with_payment_profiles(amount, payment, response_code, option) ⇒ Object



42
43
44
# File 'app/models/gateway/braintree.rb', line 42

def credit_with_payment_profiles(amount, payment, response_code, option)
  provider.credit(amount, payment)
end

#credit_without_payment_profiles(amount, response_code, options) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/models/gateway/braintree.rb', line 46

def credit_without_payment_profiles(amount, response_code, options)
  transaction = ::Braintree::Transaction.find(response_code)
  if BigDecimal.new(amount.to_s) == (transaction.amount * 100)
    provider.refund(response_code)
  else
    raise NotImplementedError
  end
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/gateway/braintree.rb', line 55

def payment_profiles_supported?
  true
end

#provider_classObject



6
7
8
# File 'app/models/gateway/braintree.rb', line 6

def provider_class
  ActiveMerchant::Billing::BraintreeGateway
end

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



59
60
61
# File 'app/models/gateway/braintree.rb', line 59

def purchase(money, creditcard, options = {})
  authorize(money, creditcard, options.merge(:submit_for_settlement => true))
end

#void(response_code, ignored_options) ⇒ Object



63
64
65
# File 'app/models/gateway/braintree.rb', line 63

def void(response_code, ignored_options)
  provider.void(response_code)
end