Class: ActiveMerchant::Billing::TokenNonce

Inherits:
Object
  • Object
show all
Includes:
PostsData
Defined in:
lib/active_merchant/billing/gateways/braintree/token_nonce.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(gateway, options = {}) ⇒ TokenNonce

Returns a new instance of TokenNonce.



11
12
13
14
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 11

def initialize(gateway, options = {})
  @braintree_gateway = gateway
  @options = options
end

Instance Attribute Details

#braintree_gatewayObject (readonly)

This class emulates the behavior of the front-end js library to create token nonce for a bank account base on the docs: developer.paypal.com/braintree/docs/guides/ach/client-side



9
10
11
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 9

def braintree_gateway
  @braintree_gateway
end

#optionsObject (readonly)

This class emulates the behavior of the front-end js library to create token nonce for a bank account base on the docs: developer.paypal.com/braintree/docs/guides/ach/client-side



9
10
11
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 9

def options
  @options
end

Instance Method Details

#client_token(options = {}) ⇒ Object



37
38
39
40
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 37

def client_token(options = {})
  base64_token = @braintree_gateway.client_token.generate({ merchant_account_id: options[:merchant_account_id] || @options[:merchant_account_id] }.compact)
  JSON.parse(Base64.decode64(base64_token))
end

#create_token_nonce_for_payment_method(payment_method, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 21

def create_token_nonce_for_payment_method(payment_method, options = {})
  headers = {
    'Accept' => 'application/json',
    'Authorization' => "Bearer #{client_token(options)['authorizationFingerprint']}",
    'Content-Type' => 'application/json',
    'Braintree-Version' => '2018-05-10'
  }
  resp = ssl_post(url, build_nonce_request(payment_method), headers)
  json_response = JSON.parse(resp)

  message = json_response['errors'].map { |err| err['message'] }.join("\n") if json_response['errors'].present?
  token = token_from(payment_method, json_response)

  return token, message
end

#urlObject



16
17
18
19
# File 'lib/active_merchant/billing/gateways/braintree/token_nonce.rb', line 16

def url
  sandbox = @braintree_gateway.config.environment == :sandbox
  "https://payments#{'.sandbox' if sandbox}.braintree-api.com/graphql"
end