class Berbix::Client

Public Class Methods

new(opts={}) click to toggle source
# 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

Public Instance Methods

create_hosted_transaction(opts={}) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# File lib/berbix.rb, line 142
def delete_transaction(tokens)
  token_auth_request(:delete, tokens, '/v0/transactions')
end
fetch_transaction(tokens) click to toggle source
# File lib/berbix.rb, line 138
def fetch_transaction(tokens)
  token_auth_request(:get, tokens, '/v0/transactions')
end
override_transaction(tokens, opts={}) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

api_host(opts) click to toggle source
# File lib/berbix.rb, line 222
def api_host(opts)
  opts[:api_host].nil? ? 'https://api.berbix.com' : opts[:api_host]
end
auth() click to toggle source
# File lib/berbix.rb, line 218
def auth
  { user: @api_secret, pass: '' }
end
fetch_tokens(path, payload) click to toggle source
# File lib/berbix.rb, line 198
def fetch_tokens(path, payload)
  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => 'BerbixRuby/' + SDK_VERSION,
  }
  result = @http_client.request(
    :post,
    @api_host + path,
    headers,
    data: payload,
    auth: auth())
  Tokens.new(
    result['refresh_token'],
    result['access_token'],
    result['client_token'],
    Time.now + result['expires_in'],
    result['transaction_id'],
    result)
end
refresh_if_necessary!(tokens) click to toggle source
# File lib/berbix.rb, line 181
def refresh_if_necessary!(tokens)
  if tokens.needs_refresh?
    refreshed = refresh_tokens(tokens)
    tokens.refresh!(refreshed.access_token, refreshed.client_token, refreshed.expiry, refreshed.transaction_id)
  end
end
token_auth_request(method, tokens, path, opts={}) click to toggle source
# File lib/berbix.rb, line 188
def token_auth_request(method, tokens, path, opts={})
  refresh_if_necessary!(tokens)
  headers = {
    'Authorization' => 'Bearer ' + tokens.access_token,
    'Content-Type' => 'application/json',
    'User-Agent' => 'BerbixRuby/' + SDK_VERSION,
  }
  @http_client.request(method, @api_host + path, headers, opts)
end