class MostOts::Service

Most Money HTTP Service Wrapper

Constants

ENCRYPTED_FIELDS

Fields that needs to be encrypted

PROTOCOL_VERSION

Current Protocol Version

Attributes

api_host[RW]

Most API Endpoint @return [String] MOST Service HOST

base_params[RW]

Base Parameters @return [Hash]

certificate[RW]

Most Provided Public Key @return [File] or [String] certificate_string

cipher_iv[RW]

@return [Array] cipher IV binary

cipher_key[RW]

@return [String] 16byte key string

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

# File lib/most_ots/service.rb, line 35
def initialize(options = {})
  logger.debug('MostOts Service API Initializing')
  setup_base_params(options)
  self.certificate = options.fetch(:most_cert_file)
  self.api_host    = options.fetch(:host) { 'http://202.131.242.165:9089' }
  self.cipher_key  = options[:cipher_key]
  self.cipher_iv   = options.fetch(:cipher_iv)
end

Public Instance Methods

check_qr_payment(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 79
def check_qr_payment(params)
  logger.debug('MostOts Service Check QR Payment Called')
  # Mandatory Fields
  mf = %i[srcInstId channel lang traceNo qrCode payeeId posNo billId isCheckQr]
  # Optional Fields
  of = %i[deviceIP deviceMac deviceName]
  api_request('/api/mapi/TT3065', configure_params(params, mf, of))
end
check_qr_payment_batch(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 90
def check_qr_payment_batch(params)
  logger.debug('MostOts Service Check QR Payment Called')
  # Mandatory Fields
  mf = %i[srcInstId channel lang traceNo qrCode payeeId posNo isCheckQr]
  # Optional Fields
  of = %i[deviceIP deviceMac deviceName]
  api_request('/api/mapi/TT3066', configure_params(params, mf, of))
end
purchase_qr(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 46
def purchase_qr(params)
  logger.debug('MostOts Service Purchase QR Called')
  # Mandatory Fields
  mf = %i[srcInstId channel lang traceNo payeeId posNo tranAmount tranCur tranDesc qrPaidLimit]
  # Optional Fields
  of = %i[billId deviceIP deviceMac deviceName]
  api_request('/api/mapi/TT3051', configure_params(params, mf, of))
end
purchase_tan(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 101
def purchase_tan(params)
  logger.debug('MostOts Service Purchase Tan Called')
  # Mandatory Fields
  mf = %i[srcInstId channel lang traceNo payeeId posNo srcMsisdn tan tranAmount tranCur]
  # Optional Fields
  of = %i[billId tranDesc deviceIP deviceMac deviceName]
  api_request('/api/mapi/TT1608', configure_params(params, mf, of))
end
transfer_qr(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 57
def transfer_qr(params)
  logger.debug('MostOts Service Transfer QR Called')
  # Mandatory Fields
  mf = %i[srcInstId channel lang traceNo qrBankCode qrAccountName qrAccountNumber tranAmount tranCur]
  # Optional Fields
  of = %i[tranDesc]
  api_request('/api/mapi/TT3061', configure_params(params, mf, of))
end
wait_payment_response(params) click to toggle source

@param [Hash] params @return [JSON] response

# File lib/most_ots/service.rb, line 68
def wait_payment_response(params)
  logger.debug('MostOts Service Waiting Payment Response')
  # Mandatory Fields
  mf = %i[srcInstId channel lang qrBankCode qrAccountName qrAccountNumber tranAmount tranCur]
  # Optional Fields
  of = %i[tranDesc]
  api_request('/api/mapi/TT3064', configure_params(params, mf, of))
end

Private Instance Methods

api_request(path, params = nil) click to toggle source

@param [String] path @param [Hash] params @return [Object] response

# File lib/most_ots/service.rb, line 131
def api_request(path, params = nil)
  params  = encrypt_params(params)
  headers = {
    'Content-Type'   => 'application/json',
    :Accept          => 'application/json',
    'Accept-Charset' => 'utf-8',
    :TT              => path.split('TT').last,
    :RS              => '00',
    'User-Agent'     => user_agent,
    :PV              => PROTOCOL_VERSION
  }
  logger.debug("Service Called With endpoint #{api_host}#{path} params: #{params}")
  req = HTTParty.post("#{api_host}#{path}", body: params, headers: headers)
  JSON.parse(req.body)
end
cipher_key_string() click to toggle source

@return [String] 16 byte key string

# File lib/most_ots/service.rb, line 190
def cipher_key_string
  if cipher_key.nil?
    cipher = OpenSSL::Cipher.new('AES-128-CBC')
    cipher.encrypt
    self.cipher_key = cipher.random_key
  end
  cipher_key
end
configure_params(params, mandatory_fields, optional_fields = []) click to toggle source
# File lib/most_ots/service.rb, line 147
def configure_params(params, mandatory_fields, optional_fields = [])
  params           = base_params.merge(params)
  processed_params = {}
  required         = mandatory_fields.select { |mk| params[mk].nil? }
  if required.any?
    raise "Following mandatory Fields cannot be nil: #{required.join('.')}"
  end
  params.each do |k, v|
    processed_params[k] = v if (mandatory_fields + optional_fields).include?(k) && !v.nil?
  end
  processed_params
end
encrypt(data) click to toggle source

Encrypts Hash into MOST AES128 encryption @param [Hash or String] data @return [Hash] encrypted hash with signature

# File lib/most_ots/service.rb, line 115
def encrypt(data)
  cipher = OpenSSL::Cipher.new('AES-128-CBC')
  cipher.encrypt
  cipher.iv  = cipher_iv.pack('c*')
  rsa        = OpenSSL::PKey::RSA.new certificate
  cipher.key = cipher_key_string
  {
    SD: Base64.encode64("#{cipher.update(data)}#{cipher.final}"),
    EK: Base64.encode64(rsa.public_encrypt(cipher_key_string)),
    SG: Digest::SHA1.base64digest(data)
  }
end
encrypt_params(params) click to toggle source

@param [Hash] params @return [String] json_string

# File lib/most_ots/service.rb, line 162
def encrypt_params(params)
  to_encrypt = {}
  ENCRYPTED_FIELDS.each do |encrypted_key|
    to_encrypt[encrypted_key] = params.delete(encrypted_key) if params.key?(encrypted_key)
  end
  JSON.generate(params.merge(encrypt(JSON.generate(to_encrypt))))
end
setup_base_params(options) click to toggle source

@param [Hash] options @return [Hash] base_params

# File lib/most_ots/service.rb, line 172
def setup_base_params(options)
  self.base_params = {
    lang:            options.fetch(:lang) { '0' },
    srcInstId:       options.fetch(:src_inst_id) { ENV['SRC_INST_ID'] },
    channel:         options[:channel],
    deviceMac:       options[:device_mac],
    deviceIp:        options[:device_ip],
    deviceName:      options[:device_name],
    payeeId:         options[:payee_id],
    qrBankCode:      options[:qr_bank_code],
    qrAccountName:   options[:qr_account_name],
    qrAccountNumber: options[:qr_account_number],
    tranCur:         options[:tran_cur],
    posNo:           options[:pos_no]
  }
end
user_agent() click to toggle source
# File lib/most_ots/service.rb, line 199
def user_agent
  "most_ots/#{VERSION} ruby/#{RUBY_VERSION}"
end