class Selcom::SendMoney

Constants

NUMBER_PREFIXES
SELCOM_UTILITY_CODES
XMLRPC_METHOD
XMLRPC_URI

Attributes

amount[RW]
customer_name[RW]
mobile_number[RW]
reference[RW]
response[RW]
status[RW]
status_code[RW]
status_description[RW]
success[RW]
telco_id[RW]

Public Class Methods

new(args) click to toggle source
# File lib/selcom.rb, line 40
def initialize(args)
  self.telco_id       = args[:telco_id]
  self.mobile_number  = args[:mobile_number]
  self.amount         = args[:amount]
end

Public Instance Methods

send!() click to toggle source
# File lib/selcom.rb, line 46
def send!
  self.response = make_rpc(self.to_params)
  parse_response(response)

  return self.success
end
to_params() click to toggle source
# File lib/selcom.rb, line 53
def to_params
  return HashWithIndifferentAccess.new(
    :amount         => self.amount,
    :mobile_number  => self.mobile_number,
    :telco_id       => self.telco_id,
    :vendor_id      => Selcom.config.vendor_id,
    :vendor_pin     => Selcom.config.vendor_pin
  )
end

Private Instance Methods

call_rpc_server(xmlrpc_client, request_params) click to toggle source
# File lib/selcom.rb, line 136
def call_rpc_server(xmlrpc_client, request_params)
  # make call to xmlrpc server at selcom
  begin
    xmlrpc_client.call(XMLRPC_METHOD, request_params)
  rescue RuntimeError => e
    raise MalformedRequestError, (
      "Invalid request parameters: #{request_params.inspect}"
    )
  end

  return xmlrpc_client.http_last_response.body
end
convert_xml_to_hash(xml) click to toggle source
# File lib/selcom.rb, line 112
def convert_xml_to_hash(xml)
  content_hash  = Hash.from_xml(xml)
  resultHash    = HashWithIndifferentAccess.new
  if content_hash
    content_hash['struct']['member'].map {|member|
      result.store(
        member['name'], member['value'].first[1]
      )
    }
    end

  return resultHash
end
create_request_params(args) click to toggle source
# File lib/selcom.rb, line 81
def create_request_params(args)
  amount            = args["amount"]
  receipient_number = args["mobile_number"]
  telco_id          = NUMBER_PREFIXES[receipient_number[0..2]] || :vodacom_tz
  utility_code      = SELCOM_UTILITY_CODES[telco_id]
  unique_token      = SecureRandom.urlsafe_base64(nil, false)

  return {
    "vendor"      => Selcom.config.vendor_id,
    "pin"         => Selcom.config.vendor_pin,
    "utilitycode" => utility_code,
    "utilityref"  => receipient_number,
    "amount"      => amount,
    "transid"     => unique_token,
    "msisdn"      => receipient_number
  }
end
create_rpc_client() click to toggle source
# File lib/selcom.rb, line 126
def create_rpc_client()
  client     = XMLRPC::Client.new2(XMLRPC_URI)
  client.http_header_extra = {
    'Content-Type'  => 'text/xml; charset=utf-8',
    'Accept'        => 'text/html'
  }

  return client
end
extract_xml(xml_rpc_response) click to toggle source
# File lib/selcom.rb, line 105
def extract_xml(xml_rpc_response)
  xml_doc = Nokogiri::XML(xml_rpc_response)
  xml_doc_content = xml_doc.xpath('/methodResponse/params/param/value/struct')

  return xml_doc_content.to_xml
end
make_rpc(args) click to toggle source
# File lib/selcom.rb, line 65
def make_rpc(args)
  request_params  = create_request_params(args)
  xmlrpc_client   = create_rpc_client()
  xml_response    = call_rpc_server(xmlrpc_client, request_params)

  return parse_xml_into_hash(xml_response)
end
parse_response(response) click to toggle source
# File lib/selcom.rb, line 73
def parse_response(response)
  self.reference          = response[:reference]
  self.status             = response[:result]
  self.status_code        = response[:resultcode]
  self.status_description = response[:message]
  self.success            = ('SUCCESS' == self.status)
end
parse_xml_into_hash(xml_from_rpc) click to toggle source
# File lib/selcom.rb, line 99
def parse_xml_into_hash(xml_from_rpc)
  xml = extract_xml(xml_from_rpc)

  return convert_xml_to_hash(xml)
end