Class: Braintree::TransparentRedirectGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/braintree/transparent_redirect_gateway.rb

Overview

:nodoc

Constant Summary

TransparentRedirectKeys =

:nodoc:

[:redirect_url]
CreateCustomerSignature =

:nodoc:

TransparentRedirectKeys + [{:customer => CustomerGateway._create_signature}]
UpdateCustomerSignature =

:nodoc:

TransparentRedirectKeys + [:customer_id, {:customer => CustomerGateway._update_signature}]
TransactionSignature =

:nodoc:

TransparentRedirectKeys + [{:transaction => TransactionGateway._create_signature}]
CreateCreditCardSignature =

:nodoc:

TransparentRedirectKeys + [{:credit_card => CreditCardGateway._create_signature}]
UpdateCreditCardSignature =

:nodoc:

TransparentRedirectKeys + [:payment_method_token, {:credit_card => CreditCardGateway._update_signature}]

Instance Method Summary (collapse)

Constructor Details

- (TransparentRedirectGateway) initialize(gateway)



10
11
12
13
# File 'lib/braintree/transparent_redirect_gateway.rb', line 10

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
end

Instance Method Details

- (Object) _data(params)

:nodoc:

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
# File 'lib/braintree/transparent_redirect_gateway.rb', line 93

def _data(params) # :nodoc:
  raise ArgumentError, "expected params to contain :redirect_url" unless params[:redirect_url]
  tr_data_segment = Util.hash_to_query_string(params.merge(
    :api_version => @config.api_version,
    :time => Time.now.utc.strftime("%Y%m%d%H%M%S"),
    :public_key => @config.public_key
  ))
  tr_data_hash = _hash(tr_data_segment)
  "#{tr_data_hash}|#{tr_data_segment}"
end

- (Object) _hash(string)

:nodoc:



104
105
106
# File 'lib/braintree/transparent_redirect_gateway.rb', line 104

def _hash(string) # :nodoc:
  ::Braintree::Digest.hexdigest(@config.private_key, string)
end

- (Object) confirm(query_string)



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/braintree/transparent_redirect_gateway.rb', line 15

def confirm(query_string)
  params = @gateway.transparent_redirect.parse_and_validate_query_string query_string
  confirmation_gateway = {
    TransparentRedirect::Kind::CreateCustomer => :customer,
    TransparentRedirect::Kind::UpdateCustomer => :customer,
    TransparentRedirect::Kind::CreatePaymentMethod => :credit_card,
    TransparentRedirect::Kind::UpdatePaymentMethod => :credit_card,
    TransparentRedirect::Kind::CreateTransaction => :transaction
  }[params[:kind]]

  @gateway.send(confirmation_gateway)._do_create("/transparent_redirect_requests/#{params[:id]}/confirm")
end

- (Object) create_credit_card_data(params)



28
29
30
31
32
# File 'lib/braintree/transparent_redirect_gateway.rb', line 28

def create_credit_card_data(params)
  Util.verify_keys(CreateCreditCardSignature, params)
  params[:kind] = TransparentRedirect::Kind::CreatePaymentMethod
  _data(params)
end

- (Object) create_customer_data(params)



34
35
36
37
38
# File 'lib/braintree/transparent_redirect_gateway.rb', line 34

def create_customer_data(params)
  Util.verify_keys(CreateCustomerSignature, params)
  params[:kind] = TransparentRedirect::Kind::CreateCustomer
  _data(params)
end

- (Object) parse_and_validate_query_string(query_string)

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/braintree/transparent_redirect_gateway.rb', line 40

def parse_and_validate_query_string(query_string) # :nodoc:
  params = Util.symbolize_keys(Util.parse_query_string(query_string))
  query_string_without_hash = query_string.split("&").reject{|param| param =~ /\Ahash=/}.join("&")
  decoded_query_string_without_hash = Util.url_decode(query_string_without_hash)
  encoded_query_string_without_hash = Util.url_encode(query_string_without_hash)

  if params[:http_status] == nil
    raise UnexpectedError, "expected query string to have an http_status param"
  elsif params[:http_status] != '200'
    Util.raise_exception_for_status_code(params[:http_status], params[:bt_message])
  end

  query_strings_without_hash = [query_string_without_hash, encoded_query_string_without_hash, decoded_query_string_without_hash]

  if query_strings_without_hash.any? { |query_string| _hash(query_string) == params[:hash] }
    params
  else
    raise ForgedQueryString
  end
end

- (Object) transaction_data(params)



61
62
63
64
65
66
67
68
69
# File 'lib/braintree/transparent_redirect_gateway.rb', line 61

def transaction_data(params)
  Util.verify_keys(TransactionSignature, params)
  params[:kind] = TransparentRedirect::Kind::CreateTransaction
  transaction_type = params[:transaction] && params[:transaction][:type]
  unless %w[sale credit].include?(transaction_type)
    raise ArgumentError, "expected transaction[type] of sale or credit, was: #{transaction_type.inspect}"
  end
  _data(params)
end

- (Object) update_credit_card_data(params)



71
72
73
74
75
76
77
78
# File 'lib/braintree/transparent_redirect_gateway.rb', line 71

def update_credit_card_data(params)
  Util.verify_keys(UpdateCreditCardSignature, params)
  unless params[:payment_method_token]
    raise ArgumentError, "expected params to contain :payment_method_token of payment method to update"
  end
  params[:kind] = TransparentRedirect::Kind::UpdatePaymentMethod
  _data(params)
end

- (Object) update_customer_data(params)



80
81
82
83
84
85
86
87
# File 'lib/braintree/transparent_redirect_gateway.rb', line 80

def update_customer_data(params)
  Util.verify_keys(UpdateCustomerSignature, params)
  unless params[:customer_id]
    raise ArgumentError, "expected params to contain :customer_id of customer to update"
  end
  params[:kind] = TransparentRedirect::Kind::UpdateCustomer
  _data(params)
end

- (Object) url



89
90
91
# File 'lib/braintree/transparent_redirect_gateway.rb', line 89

def url
  "#{@config.base_merchant_url}/transparent_redirect_requests"
end