module Payfort

Constants

VERSION

Attributes

api_key[RW]

Public Class Methods

api_url(url='') click to toggle source
# File lib/payfort.rb, line 16
def self.api_url(url='')
  @api_base + url
end
get(url, query={}) click to toggle source
# File lib/payfort.rb, line 55
def self.get(url, query={})
  options = {basic_auth: {username: api_key, password: ''}}
  options.merge!({query: query})
  response = HTTParty.get(url, options)
  JSON.parse(response.body);
end
handle_response(response) click to toggle source
# File lib/payfort.rb, line 20
def self.handle_response(response)
  body = JSON.parse(response.body);

  if response.code.between?(200, 299) and !body.key?('error')
    # The request was successful
    return body
  end

  # There was an error .. check the response
  case body['error']['type']
  when 'banking'
    raise Payfort::BankingError.new(body['error']['message'], body['error']['code'], response.code)

  when 'authentication'
    raise Payfort::AuthenticationError.new(body['error']['message'], body['error']['code'], response.code)

  when 'processing'
    raise Payfort::ProcessingError.new(body['error']['message'], body['error']['code'], response.code)

  when 'request'
    raise Payfort::RequestError.new(body['error']['message'], body['error']['code'], response.code)
  end

  # Otherwise, raise a General error
  raise Payfort::PayfortError.new(body['error']['message'], body['error']['code'], response.code)
end
post(url, body={}) click to toggle source
# File lib/payfort.rb, line 47
def self.post(url, body={})
  options = {basic_auth: {username: api_key, password: ''}}
  options.merge!({body: body})
  response = HTTParty.post(url, options)
  self.handle_response(response)

end