class Borika::Request

Constants

CURRENCIES
PROTOCOL_VERSIONS
TRANSACTION_TYPES

Attributes

amount[R]
currency[R]
language[R]
one_time_ticket[R]
order_id[R]
order_summary[R]
process_no[R]
protocol_version[R]
request_type[R]
terminal_id[R]
time[R]

Public Class Methods

new(order_id, amount, order_summary: "Order click to toggle source
# File lib/borika/request.rb, line 33
def initialize(order_id,
               amount,
               order_summary: "Order #{order_id}",
               time: Time.now,
               language: 'EN',
               protocol_version: '1.0',
               currency: 'EUR',
               process_no: 10,
               request_type: Borika.config.request_type,
               terminal_id: Borika.config.borika_terminal_id,
               one_time_ticket: nil)
  @process_no = validate(process_no.to_i, of: TRANSACTION_TYPES)
  @time = time
  @amount = validate_cents(amount).to_s
  @terminal_id = terminal_id
  @request_type = request_type
  @order_id = validate_integer(order_id).to_s
  @order_summary = order_summary
  @language = language.to_s.upcase
  @protocol_version = validate(protocol_version.to_s, of: PROTOCOL_VERSIONS)
  @currency = validate(currency.to_s.upcase, of: CURRENCIES)
  @one_time_ticket = one_time_ticket
end

Public Instance Methods

sign_data(unsigned_content) click to toggle source
# File lib/borika/request.rb, line 57
def sign_data(unsigned_content)
  pkeyid = OpenSSL::PKey::RSA.new(Borika.config.private_key, Borika.config.private_key_password)
  signed_str = pkeyid.sign(OpenSSL::Digest::SHA1.new, unsigned_content)
end
url() click to toggle source
# File lib/borika/request.rb, line 66
def url
  url = "#{Borika.config.borika_url}#{request_type}?eBorica=#{url_param}"
end
url_param() click to toggle source
# File lib/borika/request.rb, line 62
def url_param
  CGI.escape Base64.strict_encode64(unsigned_content + sign_data(unsigned_content))
end

Private Instance Methods

fill(object, length, char: ' ', right: false) click to toggle source
# File lib/borika/request.rb, line 100
def fill(object, length, char: ' ', right: false)
  truncated = object.to_s[0...length]

  if right
    truncated.rjust(length, char)
  else
    truncated.ljust(length, char)
  end
end
unsigned_content() click to toggle source
# File lib/borika/request.rb, line 110
def unsigned_content
  @unsigned_content ||= [
    fill(process_no, 2),
    fill(time.strftime('%Y%m%d%H%M%S'), 14),
    fill(amount, 12, char: '0', right: true),
    fill(terminal_id, 8),
    fill(order_id, 15),
    fill(order_summary, 125),
    fill(language, 2),
    fill(protocol_version, 3),
    (fill(currency, 3) if protocol_version > '1.0'),
    (one_time_ticket if protocol_version == '2.0')
  ].compact.join
end
validate(value, of:) click to toggle source
# File lib/borika/request.rb, line 72
def validate(value, of:)
  unless of.include?(value)
    raise ArgumentError, "Expected one of #{of.inspect}, got: #{value.inspect}"
  end
  value
end
validate_cents(value) click to toggle source
# File lib/borika/request.rb, line 79
def validate_cents(value)
  unless value.is_a? Integer || value >= 100
    raise ArgumentError, "Expected integer value with cents, got: #{value.inspect}"
  end
  value
end
validate_integer(value) click to toggle source
# File lib/borika/request.rb, line 86
def validate_integer(value)
  if !value.is_a? Integer
    raise ArgumentError, "Value must be an Integer, got: #{value.inspect}"
  end
  value
end
validate_presence(value) click to toggle source
# File lib/borika/request.rb, line 93
def validate_presence(value)
  if !value.is_a? Numeric || value.blank?
    raise ArgumentError, "Expected not blank or nil value, got: #{value.inspect}"
  end
  value
end