Class: Berbix::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



97
98
99
100
101
102
103
104
105
# File 'lib/berbix.rb', line 97

def initialize(opts={})
  @api_secret = opts[:api_secret] || opts[:client_secret]
  @api_host = api_host(opts)
  @http_client = opts[:http_client] || NetHTTPClient.new

  if @api_secret.nil?
    raise(Berbix::BerbixError, ':api_secret must be provided when instantiating Berbix client')
  end
end

Instance Method Details

#create_hosted_transaction(opts = {}) ⇒ Object



125
126
127
128
129
# File 'lib/berbix.rb', line 125

def create_hosted_transaction(opts={})
  opts[:hosted_options] = {} if opts[:hosted_options].nil?
  tokens = create_transaction(opts)
  HostedTransactionResponse.new(tokens, tokens.response["hosted_url"])
end

#create_transaction(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/berbix.rb', line 107

def create_transaction(opts={})
  payload = {}
  payload[:email] = opts[:email] unless opts[:email].nil?
  payload[:phone] = opts[:phone] unless opts[:phone].nil?
  if opts[:customer_uid].nil?
    raise(Berbix::BerbixError, ':customer_uid must be provided when creating a transaction')
  else
    payload[:customer_uid] = opts[:customer_uid].to_s 
  end
  if opts[:template_key].nil?
    raise(Berbix::BerbixError, ':template_key must be provided when creating a transaction')
  else
    payload[:template_key] = opts[:template_key]
  end
  payload[:hosted_options] = opts[:hosted_options] unless opts[:hosted_options].nil?
  fetch_tokens('/v0/transactions', payload)
end

#delete_transaction(tokens) ⇒ Object



142
143
144
# File 'lib/berbix.rb', line 142

def delete_transaction(tokens)
  token_auth_request(:delete, tokens, '/v0/transactions')
end

#fetch_transaction(tokens) ⇒ Object



138
139
140
# File 'lib/berbix.rb', line 138

def fetch_transaction(tokens)
  token_auth_request(:get, tokens, '/v0/transactions')
end

#override_transaction(tokens, opts = {}) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/berbix.rb', line 153

def override_transaction(tokens, opts={})
  payload = {}
  payload[:response_payload] = opts[:response_payload] unless opts[:response_payload].nil?
  payload[:flags] = opts[:flags] unless opts[:flags].nil?
  payload[:override_fields] = opts[:override_fields] unless opts[:override_fields].nil?
  token_auth_request(:patch, tokens, '/v0/transactions/override', data: payload)
end

#refresh_tokens(tokens) ⇒ Object



131
132
133
134
135
136
# File 'lib/berbix.rb', line 131

def refresh_tokens(tokens)
  fetch_tokens('/v0/tokens', {
    'refresh_token' => tokens.refresh_token,
    'grant_type' => 'refresh_token',
  })
end

#update_transaction(tokens, opts = {}) ⇒ Object



146
147
148
149
150
151
# File 'lib/berbix.rb', line 146

def update_transaction(tokens, opts={})
  payload = {}
  payload[:action] = opts[:action] unless opts[:action].nil?
  payload[:note] = opts[:note] unless opts[:note].nil?
  token_auth_request(:patch, tokens, '/v0/transactions', data: payload)
end

#validate_signature(secret, body, header) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/berbix.rb', line 161

def validate_signature(secret, body, header)
  parts = header.split(',')
  # Version (parts[0]) is currently unused
  timestamp = parts[1]
  signature = parts[2]
  if timestamp.to_i < Time.now.to_i - CLOCK_DRIFT
    return false
  end
  digest = OpenSSL::Digest::SHA256.new
  hmac = OpenSSL::HMAC.new(secret, digest)
  hmac << timestamp
  hmac << ','
  hmac << secret
  hmac << ','
  hmac << body
  hmac.hexdigest == signature
end