class Bitkassa::Request

Generic HTTP Request

Attributes

authenticator[W]

Overrides the default Bitkassa::Authentication to sign the authentication

responder[W]

Override the class that is initialized to capture the perform response. Defaults to Bitkassa::PaymentResponse.

Public Class Methods

new(attributes = {}) click to toggle source
Initalize a request
  • attributes Hash

# File lib/bitkassa/request.rb, line 15
def initialize(attributes = {})
  @initialized_at = Time.now.to_i
  assign_attributes(attributes)
end

Public Instance Methods

attributes() click to toggle source

Defines the attributes to be serialised in the payload merchant_id and action will be merged by default.

# File lib/bitkassa/request.rb, line 41
def attributes
  {}
end
can_perform?() click to toggle source
# File lib/bitkassa/request.rb, line 51
def can_perform?
  return false if Bitkassa.config.merchant_id.nil?
  return false if Bitkassa.config.merchant_id.empty?
  return false if Bitkassa.config.secret_api_key.nil?
  return false if Bitkassa.config.secret_api_key.empty?
  true
end
payload_action() click to toggle source

Defines the action to insert into the payload.

# File lib/bitkassa/request.rb, line 47
def payload_action
  ""
end
perform() click to toggle source

Make the request.

returns Bitkassa::PaymentResponse Regardless of the response, this this PaymentResponse is initialized.

When a payment cannot be made because of incorrect or missing data, a Bitkassa::Exception is raised.

# File lib/bitkassa/request.rb, line 28
def perform
  if can_perform?
    response = HTTPI.post(uri, params_string)
    responder.from_json(response.body)
  else
    fail Bitkassa::Exception,
         "Your merchant_id or merchant_key are not set"
  end
end

Protected Instance Methods

assign_attributes(attributes) click to toggle source
# File lib/bitkassa/request.rb, line 68
def assign_attributes(attributes)
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end
authenticator() click to toggle source
# File lib/bitkassa/request.rb, line 78
def authenticator
  @authenticator ||= Authentication
end
json_payload() click to toggle source
# File lib/bitkassa/request.rb, line 61
def json_payload
  attributes.merge(
    action: payload_action,
    merchant_id: Bitkassa.config.merchant_id
  ).to_json
end
responder() click to toggle source
# File lib/bitkassa/request.rb, line 74
def responder
  @responder ||= Bitkassa::PaymentResponse
end

Private Instance Methods

authentication() click to toggle source
# File lib/bitkassa/request.rb, line 92
def authentication
  authenticator.sign(json_payload, @initialized_at)
end
params() click to toggle source
# File lib/bitkassa/request.rb, line 88
def params
  { p: payload, a: authentication }
end
params_string() click to toggle source
# File lib/bitkassa/request.rb, line 100
def params_string
  params.map { |k, v| [k, "=", v].join }.join("&")
end
payload() click to toggle source
# File lib/bitkassa/request.rb, line 96
def payload
  Base64.urlsafe_encode64(json_payload)
end
uri() click to toggle source
# File lib/bitkassa/request.rb, line 84
def uri
  Bitkassa.config.base_uri
end