Class: Gateway::BraintreeGateway

Inherits:
Gateway
  • Object
show all
Defined in:
app/models/spree/gateway/braintree_gateway.rb

Constant Summary collapse

CARD_TYPE_MAPPING =
{
  'American Express' => 'american_express',
  'Diners Club' => 'diners_club',
  'Discover' => 'discover',
  'JCB' => 'jcb',
  'Laser' => 'laser',
  'Maestro' => 'maestro',
  'MasterCard' => 'master',
  'Solo' => 'solo',
  'Switch' => 'switch',
  'Visa' => 'visa'
}

Instance Method Summary collapse

Instance Method Details

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



41
42
43
44
45
# File 'app/models/spree/gateway/braintree_gateway.rb', line 41

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



47
48
49
50
# File 'app/models/spree/gateway/braintree_gateway.rb', line 47

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

#create_profile(payment) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/spree/gateway/braintree_gateway.rb', line 52

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'])
      cc = response.params['braintree_customer'].fetch('credit_cards',[]).first
      update_card_number(payment.source, cc) if cc
    else
      payment.send(:gateway_error, response.message)
    end
  end
end

#credit(*args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/spree/gateway/braintree_gateway.rb', line 73

def credit(*args)
  if args.size == 4
    # enables ability to refund instead of credit
    args.slice!(1,1)
    credit_without_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

Braintree now disables credits by default, see www.braintreepayments.com/docs/ruby/transactions/credit



86
87
88
# File 'app/models/spree/gateway/braintree_gateway.rb', line 86

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

#credit_without_payment_profiles(amount, response_code, options) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/spree/gateway/braintree_gateway.rb', line 90

def credit_without_payment_profiles(amount, response_code, options)
  provider # braintree provider needs to be called here to properly configure braintree gem.
  transaction = ::Braintree::Transaction.find(response_code)
  if BigDecimal.new(amount.to_s) == (transaction.amount * 100)
    provider.refund(response_code)
  elsif BigDecimal.new(amount.to_s) < (transaction.amount * 100) # support partial refunds
    provider.refund(amount, response_code)
  else
    raise NotImplementedError
  end
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/spree/gateway/braintree_gateway.rb', line 102

def payment_profiles_supported?
  true
end

#preferencesObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/spree/gateway/braintree_gateway.rb', line 114

def preferences
  preferences = super.slice(:merchant_id,
                            :merchant_account_id,
                            :public_key,
                            :private_key,
                            :client_side_encryption_key,
                            :environment)

  # Must be either :production or :sandbox, not their string equivalents.
  # Thanks to the Braintree gem.
  preferences[:environment] = preferences[:environment].try(:to_sym) || :sandbox
  preferences
end

#providerObject



26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree/gateway/braintree_gateway.rb', line 26

def provider
  provider_instance = super
  Braintree::Configuration.custom_user_agent = "Spree #{Spree.version}"
  Braintree::Configuration.environment = preferred_environment.to_sym
  Braintree::Configuration.merchant_id = preferred_merchant_id
  Braintree::Configuration.public_key = preferred_public_key
  Braintree::Configuration.private_key = preferred_private_key

  provider_instance
end

#provider_classObject



37
38
39
# File 'app/models/spree/gateway/braintree_gateway.rb', line 37

def provider_class
  ActiveMerchant::Billing::BraintreeBlueGateway
end

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



106
107
108
# File 'app/models/spree/gateway/braintree_gateway.rb', line 106

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

#update_card_number(source, cc) ⇒ Object



65
66
67
68
69
70
71
# File 'app/models/spree/gateway/braintree_gateway.rb', line 65

def update_card_number(source, cc)
  last_4 = cc['last_4']
  source.last_digits = last_4 if last_4
  source.gateway_payment_profile_id = cc['token']
  source.cc_type = CARD_TYPE_MAPPING[cc['card_type']] if cc['card_type']
  source.save!
end

#void(response_code, *ignored_options) ⇒ Object



110
111
112
# File 'app/models/spree/gateway/braintree_gateway.rb', line 110

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