selcom 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ac82861a11c5267af9ac8b8416a596d993450ee
4
+ data.tar.gz: 3bfe6adee31a931b7ee07d470d7670c4cfaeb3bf
5
+ SHA512:
6
+ metadata.gz: 96a75b4860e890c7b131562c0e3e5b3e808daf41003f16fe5be2ee3f5c7e5b242cf3da77fd94633b9ba48027d30d394681f54c2b5cf48e663aaa07da78e4f31f
7
+ data.tar.gz: e04f2d0b8ef464376db3359cbdb92dfc688d065b68e65107948656c9e866a01df23d21d981643a55285e899dd22bd061b586960fd9c384fb8b7953699460ab19
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ Guardfile
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in selcom.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'guard'
8
+ gem 'guard-rspec', require: false
9
+ gem 'rb-readline'
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mawuli Adzoe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Selcom [![Code Climate](https://codeclimate.com/github/wallclockbuilder/selcom/badges/gpa.svg)](https://codeclimate.com/github/wallclockbuilder/selcom)
2
+
3
+ This gem makes it possible to use the Selcom API without having to deal with XML RPC.
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'selcom'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install selcom
19
+
20
+ ## Usage
21
+
22
+ set your API credentials:
23
+
24
+ ```ruby
25
+ require 'selcom'
26
+ Selcom.configure do |config|
27
+ config.api_key = '...'
28
+ config.api_signature = '...'
29
+ end
30
+ ```
31
+ Send money:
32
+
33
+ ```ruby
34
+
35
+ selcom = Selcom::SendMoney.new(:mobile_number => "...", :amount => 500, :telco_id => '...')
36
+ if send_money.send!
37
+ puts selcom.reference
38
+ puts selcom.sent_amount
39
+ puts selcom.success
40
+ puts selcom.status_code
41
+ puts selcom.status_description
42
+ else
43
+ puts selcom.response
44
+ end
45
+
46
+ ```
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/wallclockbuilder/selcom )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/selcom.rb ADDED
@@ -0,0 +1,149 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/hash/conversions'
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'xmlrpc/client'
5
+ require 'nokogiri'
6
+
7
+ module Selcom
8
+ include ActiveSupport::Configurable
9
+ class MalformedRequestError < StandardError
10
+ end
11
+
12
+ class SendMoney
13
+ attr_accessor(
14
+ :amount, :status,
15
+ :success, :telco_id,
16
+ :response, :reference,
17
+ :status_code, :customer_name,
18
+ :mobile_number, :status_description
19
+ )
20
+
21
+ XMLRPC_URI = "https://paypoint.selcommobile.com/api/selcom.pos.server.php"
22
+ XMLRPC_METHOD = 'SELCOM.utilityPayment'
23
+
24
+ SELCOM_UTILITY_CODES = {
25
+ :airtel_tz => 'AMCASHIN',
26
+ :tigo_tz => 'TPCASHIN',
27
+ :vodacom_tz => 'VMCASHIN',
28
+ :zantel_tz => 'EZCASHIN'
29
+ }
30
+ NUMBER_PREFIXES = {
31
+ '068' => :airtel_tz,
32
+ '078' => :airtel_tz,
33
+ '065' => :tigo_tz,
34
+ '071' => :tigo_tz,
35
+ '075' => :vodacom_tz,
36
+ '076' => :vodacom_tz,
37
+ '077' => :zantel_tz
38
+ }
39
+
40
+ def initialize(args)
41
+ self.telco_id = args[:telco_id]
42
+ self.mobile_number = args[:mobile_number]
43
+ self.amount = args[:amount]
44
+ end
45
+
46
+ def send!
47
+ self.response = make_rpc(self.to_params)
48
+ parse_response(response)
49
+
50
+ return self.success
51
+ end
52
+
53
+ def to_params
54
+ return HashWithIndifferentAccess.new(
55
+ :amount => self.amount,
56
+ :mobile_number => self.mobile_number,
57
+ :telco_id => self.telco_id,
58
+ :vendor_id => Selcom.config.vendor_id,
59
+ :vendor_pin => Selcom.config.vendor_pin
60
+ )
61
+ end
62
+
63
+ private
64
+
65
+ def make_rpc(args)
66
+ request_params = create_request_params(args)
67
+ xmlrpc_client = create_rpc_client()
68
+ xml_response = call_rpc_server(xmlrpc_client, request_params)
69
+
70
+ return parse_xml_into_hash(xml_response)
71
+ end
72
+
73
+ def parse_response(response)
74
+ self.reference = response[:reference]
75
+ self.status = response[:result]
76
+ self.status_code = response[:resultcode]
77
+ self.status_description = response[:message]
78
+ self.success = ('SUCCESS' == self.status)
79
+ end
80
+
81
+ def create_request_params(args)
82
+ amount = args["amount"]
83
+ receipient_number = args["mobile_number"]
84
+ telco_id = NUMBER_PREFIXES[receipient_number[0..2]] || :vodacom_tz
85
+ utility_code = SELCOM_UTILITY_CODES[telco_id]
86
+ unique_token = SecureRandom.urlsafe_base64(nil, false)
87
+
88
+ return {
89
+ "vendor" => Selcom.config.vendor_id,
90
+ "pin" => Selcom.config.vendor_pin,
91
+ "utilitycode" => utility_code,
92
+ "utilityref" => receipient_number,
93
+ "amount" => amount,
94
+ "transid" => unique_token,
95
+ "msisdn" => receipient_number
96
+ }
97
+ end
98
+
99
+ def parse_xml_into_hash(xml_from_rpc)
100
+ xml = extract_xml(xml_from_rpc)
101
+
102
+ return convert_xml_to_hash(xml)
103
+ end
104
+
105
+ def extract_xml(xml_rpc_response)
106
+ xml_doc = Nokogiri::XML(xml_rpc_response)
107
+ xml_doc_content = xml_doc.xpath('/methodResponse/params/param/value/struct')
108
+
109
+ return xml_doc_content.to_xml
110
+ end
111
+
112
+ def convert_xml_to_hash(xml)
113
+ content_hash = Hash.from_xml(xml)
114
+ resultHash = HashWithIndifferentAccess.new
115
+ if content_hash
116
+ content_hash['struct']['member'].map {|member|
117
+ result.store(
118
+ member['name'], member['value'].first[1]
119
+ )
120
+ }
121
+ end
122
+
123
+ return resultHash
124
+ end
125
+
126
+ def create_rpc_client()
127
+ client = XMLRPC::Client.new2(XMLRPC_URI)
128
+ client.http_header_extra = {
129
+ 'Content-Type' => 'text/xml; charset=utf-8',
130
+ 'Accept' => 'text/html'
131
+ }
132
+
133
+ return client
134
+ end
135
+
136
+ def call_rpc_server(xmlrpc_client, request_params)
137
+ # make call to xmlrpc server at selcom
138
+ begin
139
+ xmlrpc_client.call(XMLRPC_METHOD, request_params)
140
+ rescue RuntimeError => e
141
+ raise MalformedRequestError, (
142
+ "Invalid request parameters: #{request_params.inspect}"
143
+ )
144
+ end
145
+
146
+ return xmlrpc_client.http_last_response.body
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,3 @@
1
+ module Selcom
2
+ VERSION = "0.0.2"
3
+ end
data/selcom.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'selcom/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "selcom"
8
+ spec.version = Selcom::VERSION
9
+ spec.authors = ["Mawuli Adzoe"]
10
+ spec.email = ["mawuli.kofi.mawuli@gmail.com"]
11
+ spec.summary = %q{Ruby API wrapper for the Selcom XMLRPC API}
12
+ spec.description = %q{Use this gem so you don't have to interact with the Selcome XML RPC API directly.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport'
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "nokogiri"
26
+
27
+
28
+
29
+ end
@@ -0,0 +1,118 @@
1
+ require 'Selcom'
2
+
3
+
4
+ describe Selcom::SendMoney do
5
+ subject {
6
+ Selcom::SendMoney.new({
7
+ mobile_number: '+255701234567',
8
+ amount: '543',
9
+ telco_id: 'abc'
10
+ })
11
+ }
12
+
13
+ it { expect(subject.telco_id).to eql('abc') }
14
+ it { expect(subject.mobile_number).to eql('+255701234567') }
15
+ it { expect(subject.amount).to eql('543') }
16
+
17
+ it { should respond_to(:telco_id) }
18
+ it { should respond_to(:mobile_number) }
19
+ it { should respond_to(:amount) }
20
+ it { should respond_to(:response) }
21
+ it { should respond_to(:reference) }
22
+ it { should respond_to(:success) }
23
+ it { should respond_to(:customer_name) }
24
+ it { should respond_to(:status) }
25
+ it { should respond_to(:status_description) }
26
+ it { should respond_to(:status_code) }
27
+
28
+ describe 'request params' do
29
+ before do
30
+ Selcom.configure do |c|
31
+ c.vendor_id = 'def'
32
+ c.vendor_pin = '789'
33
+ end
34
+ end
35
+
36
+ subject do
37
+ Selcom::SendMoney.new({
38
+ amount: '543',
39
+ mobile_number: '+255701234567',
40
+ telco_id: 'abc'
41
+ })
42
+ end
43
+
44
+ it do
45
+ expect(subject.to_params).to eql(
46
+ HashWithIndifferentAccess.new(
47
+ "amount" => '543',
48
+ "mobile_number" => '+255701234567',
49
+ "telco_id" => 'abc',
50
+ "vendor_id" => 'def',
51
+ "vendor_pin" => '789'
52
+ )
53
+ )
54
+ end
55
+
56
+
57
+ end
58
+
59
+ describe 'sending money' do
60
+
61
+ describe "invalid request" do
62
+ let(:response) do
63
+ double(
64
+ HashWithIndifferentAccess.new(
65
+ :body => {
66
+ "transid" => "mwliid12345",
67
+ "reference" => "4655259721",
68
+ "message" => "Airtel Money Cash-in",
69
+ "resultcode" => "000",
70
+ "result" => "FAIL"
71
+ }
72
+ )
73
+ )
74
+ end
75
+
76
+ subject { Selcom::SendMoney.new(:amount => 543, :mobile_number => '', :telco_id =>'') }
77
+ #before{ expect(subject).to receive(:connection).with(subject.to_params).and_return(response) }
78
+ before { subject.should_receive(:connection).with(subject.to_params).and_return(response) }
79
+ it 'parses the response and sets accessors' do
80
+ subject.send!
81
+ expect(subject.status).to eql("FAIL")
82
+ expect(subject.status_code).to eql("000")
83
+ expect(subject.status_description).to eql('Airtel Money Cash-in')
84
+ expect(subject.success).to eql(false)
85
+ end
86
+ end
87
+
88
+ describe "success" do
89
+ let(:response) do
90
+ double(
91
+ HashWithIndifferentAccess.new(
92
+ :body =>
93
+ {"transid"=>"mwliid12345",
94
+ "reference"=>"4655259721",
95
+ "message"=>"Airtel Money Cash-in",
96
+ "resultcode"=>"000",
97
+ "result"=>"SUCCESS"
98
+ }
99
+ )
100
+ )
101
+ end
102
+
103
+ subject { Selcom::SendMoney.new(:amount => 1234, :mobile_number => '+255701234567', :telco_id => 'qwert') }
104
+ #before{ expect(subject).to receive(:connection).with(subject.to_params).and_return(response) }
105
+ before { subject.should_receive(:connection).with(subject.to_params).and_return(response) }
106
+
107
+ it 'parses the response and sets accessors' do
108
+ subject.send!
109
+ expect(subject.reference).to eql('4655259721')
110
+ expect(subject.status).to eql("SUCCESS")
111
+ expect(subject.status_code).to eql('000')
112
+ expect(subject.status_description).to eql("Airtel Money Cash-in")
113
+ expect(subject.success).to eql(true)
114
+ end
115
+ end
116
+ end
117
+ end
118
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selcom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mawuli Adzoe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Use this gem so you don't have to interact with the Selcome XML RPC API
84
+ directly.
85
+ email:
86
+ - mawuli.kofi.mawuli@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/selcom.rb
97
+ - lib/selcom/version.rb
98
+ - selcom.gemspec
99
+ - spec/send_money_spec.rb
100
+ homepage: ''
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Ruby API wrapper for the Selcom XMLRPC API
124
+ test_files:
125
+ - spec/send_money_spec.rb