Class: Gateway::BraintreeGateway

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus/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(cents, creditcard, options = {}) ⇒ Object



124
125
126
127
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 124

def authorize(cents, creditcard, options = {})
  result = braintree_gateway.transaction.sale(transaction_authorize_or_purchase_params(cents, creditcard, options))
  handle_result(result)
end

#braintree_gatewayObject



52
53
54
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 52

def braintree_gateway
  @braintree_gateway ||= ::Braintree::Gateway.new(gateway_options)
end

#cancel(response) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 182

def cancel(response)
  if voidable?(response)
    void(response)
  else
    handle_result(
      braintree_gateway.transaction.refund(response)
    )
  end
end

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



136
137
138
139
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 136

def capture(money, authorization_code, options = {})
  result = braintree_gateway.transaction.submit_for_settlement(authorization_code, amount(money))
  handle_result(result)
end

#card_code_placeholderObject



178
179
180
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 178

def card_code_placeholder
  '123'
end

#card_number_placeholderObject



170
171
172
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 170

def card_number_placeholder
  '4141 4141 4141 4141'
end

#create_profile(payment) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 64

def create_profile(payment)
  source = payment.source

  return if source.gateway_customer_profile_id.present? || payment.payment_method_nonce.nil?

  user = payment.order.user
  email = user ? user.email : payment.order.email
  address = (payment.source.address || payment.order.bill_address).try(:active_merchant_hash)

  params = {
    first_name: source.first_name,
    last_name: source.last_name,
    email: email,
    credit_card: {
      cardholder_name: source.name,
      billing_address: map_address(address),
      payment_method_nonce: payment.payment_method_nonce,
      options: {
        verify_card: true,
      },
    },
    device_data: payment.order.braintree_device_data
  }

  result = braintree_gateway.customer.create(params)

  if result.success?
    card = result.customer.payment_methods.last
    source.tap do |solidus_cc|
      if card.is_a?(::Braintree::PayPalAccount)
        solidus_cc.cc_type = 'paypal'
        data = {
          email: card.email
        }
        solidus_cc.data = data.to_json
      else
        solidus_cc.name = card.cardholder_name
        solidus_cc.cc_type = CARD_TYPE_MAPPING[card.card_type]
        solidus_cc.month = card.expiration_month
        solidus_cc.year = card.expiration_year
        solidus_cc.last_digits = card.last_4
      end
      solidus_cc.payment_method = self
      solidus_cc.gateway_customer_profile_id = result.customer.id
      solidus_cc.gateway_payment_profile_id = card.token
    end
    source.save!
  else
    raise ::Spree::Core::GatewayError, result.message
  end
end

#credit(cents, source, authorization_code, options = {}) ⇒ Object



157
158
159
160
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 157

def credit(cents, source, authorization_code, options = {})
  result = braintree_gateway.transaction.refund(authorization_code, amount(cents))
  handle_result(result)
end

#expiration_date_placeholderObject



174
175
176
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 174

def expiration_date_placeholder
  '01/2020'
end

#gateway_optionsObject



42
43
44
45
46
47
48
49
50
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 42

def gateway_options
  {
    environment: preferred_environment.to_sym,
    merchant_id: preferred_merchant_id,
    public_key: preferred_public_key,
    private_key: preferred_private_key,
    logger: ::Braintree::Configuration.logger.clone,
  }
end

#generate_client_token(options = {}) ⇒ Object



60
61
62
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 60

def generate_client_token(options = {})
  braintree_gateway.client_token.generate(options)
end

#method_typeObject



33
34
35
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 33

def method_type
  'braintree'
end

#partial_nameObject



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

def partial_name
  'braintree'
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 56

def payment_profiles_supported?
  true
end

#provider_classObject



120
121
122
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 120

def provider_class
  self
end

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



129
130
131
132
133
134
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 129

def purchase(cents, creditcard, options = {})
  params = transaction_authorize_or_purchase_params(cents, creditcard, options)
  params[:options][:submit_for_settlement] = true
  result = braintree_gateway.transaction.sale(params)
  handle_result(result)
end

#supports?(payment) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 116

def supports?(payment)
  true
end

#void(authorization_code, source = {}, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 141

def void(authorization_code, source = {}, options = {})
  # Allows voiding payments that are in a checkout state
  if authorization_code.nil?
    # Fake response since we don't need to void anything with Braintree
    ActiveMerchant::Billing::Response.new(
      true,
      "OK",
      {},
      {}
    )
  else
    result = braintree_gateway.transaction.void(authorization_code)
    handle_result(result)
  end
end

#voidable?(response_code) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'app/models/solidus/gateway/braintree_gateway.rb', line 162

def voidable?(response_code)
  transaction = braintree_gateway.transaction.find(response_code)
  [
    ::Braintree::Transaction::Status::SubmittedForSettlement,
    ::Braintree::Transaction::Status::Authorized,
  ].include?(transaction.status)
end