module Paysimple

Constants

VERSION

Attributes

api_endpoint[RW]
api_key[RW]
api_user[RW]
ssl_version[RW]

Public Class Methods

api_url(url='') click to toggle source
# File lib/paysimple.rb, line 82
def self.api_url(url='')
  @api_endpoint + '/v4' + url
end
authorization_header() click to toggle source
# File lib/paysimple.rb, line 118
def self.authorization_header
  utc_timestamp = Time.now.getutc.iso8601.encode(Encoding::UTF_8)
  secret_key = @api_key.encode(Encoding::UTF_8)
  hash = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_key, utc_timestamp)
  signature = Base64.encode64(hash)

  "PSSERVER accessid=#{@api_user}; timestamp=#{utc_timestamp}; signature=#{signature}"
end
execute_request(opts) click to toggle source
# File lib/paysimple.rb, line 86
def self.execute_request(opts)
  RestClient::Request.execute(opts)
end
general_api_error(rcode, rbody) click to toggle source
# File lib/paysimple.rb, line 113
def self.general_api_error(rcode, rbody)
  APIError.new("Invalid response object from API: #{rbody.inspect} " +
                   "(HTTP response code was #{rcode})", rcode, rbody)
end
handle_api_error(rcode, rbody) click to toggle source
# File lib/paysimple.rb, line 127
def self.handle_api_error(rcode, rbody)
  begin
    error_obj = rbody.empty? ? {} : Util.underscore_and_symbolize_names(JSON.parse(rbody))
  rescue JSON::ParserError
    raise general_api_error(rcode, rbody)
  end

  case rcode
    when 400, 404
      errors = error_obj[:meta][:errors][:error_messages].collect { |e| e[:message]}
      raise InvalidRequestError.new(errors, rcode, rbody, error_obj)
    when 401
      error = "Error getting an API token. Check your authentication credentials and resubmit."
      raise AuthenticationError.new(error, rcode, rbody, error_obj)
    else
      error = "There was an error processing the request."
      raise APIError.new(error, rcode, rbody, error_obj)
  end
end
handle_restclient_error(e, api_base_url=nil) click to toggle source
# File lib/paysimple.rb, line 147
def self.handle_restclient_error(e, api_base_url=nil)
  connection_message = 'Please check your internet connection and try again.'

  case e
    when RestClient::RequestTimeout
      message = "Could not connect to Paysimple (#{@api_endpoint}). #{connection_message}"
    when RestClient::ServerBrokeConnection
      message = "The connection to the server (#{@api_endpoint}) broke before the " \
      "request completed. #{connection_message}"
    when SocketError
      message = 'Unexpected error communicating when trying to connect to Paysimple. ' \
      'You may be seeing this message because your DNS is not working. '
    else
      message = 'Unexpected error communicating with Paysimple. '
  end

  raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})")
end
parse(response) click to toggle source
# File lib/paysimple.rb, line 102
def self.parse(response)
  if response.body.empty?
    {}
  else
    response = JSON.parse(response.body)
    Util.underscore_and_symbolize_names(response)[:response]
  end
rescue JSON::ParserError
  raise general_api_error(response.code, response.body)
end
request(method, url, params={}) click to toggle source
# File lib/paysimple.rb, line 46
def self.request(method, url, params={})

  raise AuthenticationError.new('API key is not provided.') unless @api_key
  raise AuthenticationError.new('API user is not provided.') unless @api_user

  url = api_url(url)
  case method
    when :get, :head
      url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
      payload = nil
    when :delete
      payload = nil
    else
      payload = Util.camelize_and_symbolize_keys(params).to_json
  end

  request_opts = { headers: request_headers, method: method, open_timeout: 30,
                   payload: payload, url: url, timeout: 80, ssl_version: ssl_version }

  begin
    response = execute_request(request_opts)
  rescue SocketError => e
    handle_restclient_error(e)
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    handle_restclient_error(e)
  end

  parse(response)
end
request_headers() click to toggle source
# File lib/paysimple.rb, line 90
def self.request_headers
  {
      authorization: authorization_header,
      content_type: :json,
      accept: :json
  }
end
uri_encode(params) click to toggle source
# File lib/paysimple.rb, line 98
def self.uri_encode(params)
  Util.flatten_params(params).map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&')
end