mpayer_ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff3b1dc5a85b2cd5f27f7f46f0c69c6211de3cea
4
+ data.tar.gz: c71a7cfd33f5c045638edad4742b52a5712324c3
5
+ SHA512:
6
+ metadata.gz: dae9eb16c3bb90e017469c391782e1a25cb521ea493c97a668d8627f7e8cac60b1fbd4874b7e04bd055f4a043952a6730b3ab0c4148fc717bdbf1688d8e5f5c9
7
+ data.tar.gz: 66b5c9747ce9f8b49b084670333c92a678d5e368649b7653de63bb2846288511b4b82cd0ed33897cf64cd7e707c2c5830918b9568191f47136414aa323093233
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ cache: bundler
5
+
6
+ script: 'bundle exec rake'
7
+
8
+ notifications:
9
+ email:
10
+ recipients:
11
+ - kgathi2@gmail.com
12
+ on_failure: change
13
+ on_success: never
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mpayer.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,50 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ guard :minitest do
27
+ # with Minitest::Unit
28
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
29
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
30
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
31
+
32
+ # with Minitest::Spec
33
+ # watch(%r{^spec/(.*)_spec\.rb$})
34
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
35
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
36
+
37
+ # Rails 4
38
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
39
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
40
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
42
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
43
+ # watch(%r{^test/.+_test\.rb$})
44
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
45
+
46
+ # Rails < 4
47
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
48
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
49
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
50
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kariuki Gathitu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,225 @@
1
+ # Mpayer
2
+
3
+ [![Build Status](https://travis-ci.org/kgathi2/mpayer_ruby.svg)](https://travis-ci.org/kgathi2/mpayer_ruby)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/sinderella.png)](http://badge.fury.io/rb/mpayer_ruby)
6
+
7
+ [![Coverage Status](https://coveralls.io/repos/kgathi2/mpayer_ruby/badge.svg)](https://coveralls.io/r/kgathi2/mpayer_ruby)
8
+
9
+ http://www.smashingmagazine.com/2014/04/08/how-to-build-a-ruby-gem-with-bundler-test-driven-development-travis-ci-and-coveralls-oh-my/
10
+
11
+ http://blakewilliams.me/blog/developing-gems-with-tdd-and-minitest-pt-1
12
+
13
+ Mpayer Ruby is a gem that makes it easy to integrate to [Mpayer payment gateway](http://mpayer.co.ke). It allows for railsesque way of interacting with Mpayer objects.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'mpayer_ruby'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install mpayer_ruby
30
+
31
+ ## Usage
32
+
33
+ First thing is to get the credentials of an Mpayer teller. This are the stored as environment variables. Add the following to your `~/.bash_profile` file or wherever else you load ENV variables
34
+
35
+ ```sh
36
+ export MPAYER_USER=<Your User name>
37
+ export MPAYER_TOKEN=<Your token>
38
+ ```
39
+
40
+ ## Configurations
41
+ To configure credentials locally on you code use config block below. For Rails, put this in `config/initializers/mpayer.rb`
42
+
43
+ ```ruby
44
+ Mpayer.setup do |config|
45
+ config.user_no = <Your User name>
46
+ config.token = <Your token>
47
+ end
48
+ ```
49
+
50
+ Versioning is not currently supported.
51
+
52
+ ## Quick Example
53
+
54
+ ```ruby
55
+ require "rubygems"
56
+ require "mpayer_ruby"
57
+
58
+ Mpayer.setup do |config|
59
+ config.user_no = <USER>
60
+ config.token = <TOKEN>
61
+ end
62
+
63
+ #Get all clients in your organisation
64
+ clients = Mpayer::Client.all(per:1,per_page:100)
65
+
66
+ #Get one client in your organisation
67
+ clients = Mpayer::Client.find(123)
68
+
69
+ #Instantiate without http call, obviously if you know that the client exists
70
+ clients = Mpayer::Client.find(123, fetch:false)
71
+ ```
72
+
73
+ ## Endpoints
74
+ Mpayer gem currently supports a few endpoint below
75
+
76
+ https://app.mpayer.co.ke/api/client
77
+ https://app.mpayer.co.ke/api/accounts
78
+ https://app.mpayer.co.ke/api/transactions
79
+ https://app.mpayer.co.ke/api/payables
80
+
81
+ All responses are objects and can be accessed with dot notation
82
+ e.g
83
+ ```ruby
84
+ clients = Mpayer::Client.all(per:1,per_page:100)
85
+ client = client.first
86
+ client.name #=> "CLark Kent"
87
+ ```
88
+
89
+ ### Client
90
+ ```ruby
91
+ clients = Mpayer::Client.all
92
+
93
+ client = Mpayer::Client.find(id) #Instantiates and hits the api
94
+ client = Mpayer::Client.find(id, fetch:false) #Instantiates only
95
+
96
+ client_attributes = {client: { client_name: "Kiki Lolo", client_birthday: Time.now.iso8601, client_type: "ext", ac_type: "cu",client_mobile: '0733222222', client_email: 'lolo@kiki.com',currency: "kes", mandate:"s", sub_type: "od" }}
97
+ client = Mpayer::Client.create(client_attributes)
98
+
99
+ client = Mpayer::Client.find(id:20284,fetch:false).account(account_id) # Get clients account with id
100
+ client = Mpayer::Client.find(id:20284,fetch:false)
101
+ account = client.account(account_id) # Get clients account with id
102
+
103
+ client_accounts = Mpayer::Client.find(id:20284,fetch:false).accounts(per:1,per_page:100)
104
+ client_payables = Mpayer::Client.find(id:20284,fetch:false).payables(per:1,per_page:100)
105
+ client_transactions = Mpayer::Client.find(id:20284,fetch:false).transactions(account_id, per:1,per_page:100)
106
+
107
+ ```
108
+
109
+ ### Account
110
+
111
+ ```ruby
112
+ accounts = Mpayer::Account.all
113
+ account = Mpayer::Account.find(1) #Instantiates and hits the api
114
+ account = Mpayer::Account.find(1, fetch:false) #Instantiates only
115
+
116
+ account = Mpayer::Account.find(1, fetch:false)
117
+ account.update(name:'Lolo Kiki')
118
+
119
+ options = {from_date: Time.now - (86400*365),to_date:nil, dr_cr:nil, ac_type:nil, category:nil}
120
+ accounts = Mpayer::Account.aggregates(options)
121
+
122
+ members = Mpayer::Account.find(25735, fetch:false).members
123
+
124
+ payable_items = Mpayer::Account.find(25735, fetch:false).payable_items
125
+
126
+ ```
127
+
128
+ ### Payable
129
+
130
+
131
+ ```ruby
132
+ payables = Mpayer::Payable.all
133
+ payable = Mpayer::Payable.find(id) #Instantiates and hits the api
134
+ payable = Mpayer::Payable.find(id,fetch:false) #Instantiates only
135
+
136
+ Mpayer::Payable.where(ref_id:"Ksdfsfsdf000411") #Search for payable. only ref_id supported currently
137
+
138
+ payable_items = []
139
+ [*0..5].each do |n|
140
+ payable_items << {
141
+ payment_party: 'pz_test' ,
142
+ terminal_ac: 'pz_test2' ,
143
+ details: "paying to test account" ,
144
+ amount: 10,
145
+ price: 10,
146
+ unit_measure: 1.0
147
+ }
148
+ end
149
+ options = {
150
+ payment: {
151
+ payable_no: rand(10000),
152
+ note: "payable to pay something",
153
+ ref_id:rand(10000),
154
+ client_id: client_id,
155
+ status: status,
156
+ payable_type: payable_type,
157
+ due_date: Time.now+(86400*31),
158
+ pay: payable_items,
159
+ tags: tags,
160
+ flags: flags,
161
+ infos: infos ,
162
+ sync_lag:sync_lag
163
+ }
164
+ }
165
+ payable = Mpayer::Payable.create(options)
166
+
167
+ payable = Mpayer::Payable.find(8818,fetch:false)
168
+ payable.destroy# Delete a payable
169
+ ```
170
+
171
+ ### Transactions
172
+
173
+ ```ruby
174
+ # Note: cr_party is the recieving (credited) account while dr_party is paying (debited) account
175
+
176
+ transactions = Mpayer::Transaction.all(from: Time.now - (86400*400))
177
+
178
+ transaction = Mpayer::Transaction.where(ref_id:"KT00410000402")# Only ref_id supported currently
179
+
180
+ body = {particulars:"particulars",amount:1000, cr_party: "account_alias"}
181
+ deposit = Mpayer::Transaction.deposit(body)
182
+
183
+ body = {particulars:"particulars",amount:10, dr_party: "01202320320"}
184
+ withdrawal = Mpayer::Transaction.withdraw(body)
185
+
186
+ body = {particulars:"particulars",amount:10, cr_party: "02304304", dr_party:"alias" }
187
+ transfer = Mpayer::Transaction.transfer(body)
188
+
189
+ ```
190
+
191
+ ### Fetch
192
+ ```ruby
193
+ # Used to interact with https://app.mpayer.co.ke/api
194
+
195
+ Mpayer.setup do |config|
196
+ config.user_no = 'GLOBAL_USER'
197
+ config.token = 'GLOBAL_TOKEN'
198
+ end
199
+
200
+ url = "/clients/all_clients"
201
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page})
202
+ Mpayer::Fetch.post(url,body:{client:{name:'Kiki Lolo'}}.to_json)
203
+ Mpayer::Fetch.put(url,body:{client:{name:'Kiki Lolo'}}.to_json)
204
+ Mpayer::Fetch.delete(url)
205
+
206
+ ```
207
+
208
+ ## Development
209
+
210
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
211
+
212
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
213
+
214
+ ## Todo
215
+ 1. Add Webmock for faster / localised test or sinatra app method
216
+ 2. Add versioning
217
+
218
+
219
+ ## Contributing
220
+
221
+ 1. Fork it ( https://github.com/[my-github-username]/mpayer/fork )
222
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
223
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
224
+ 4. Push to the branch (`git push origin my-new-feature`)
225
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << %w(test lib)
6
+ task.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mpayer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'mpayer_ruby'
@@ -0,0 +1,20 @@
1
+ module Mpayer
2
+ class Configuration
3
+ attr_accessor :user_no, :token, :base_url
4
+
5
+ def initialize(user_no:nil,token:nil)
6
+ @base_url = 'https://app.mpayer.co.ke/api/'
7
+ @user_no ||= user_no
8
+ @token ||= token
9
+ end
10
+
11
+ def auth
12
+ WSSE::header(user_no, token) unless user_no.nil? and token.nil?
13
+ end
14
+
15
+ def header
16
+ {'Content-Type'=> 'application/json', 'Accept' => 'application/json', 'X-WSSE' => auth }
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,57 @@
1
+ module Mpayer
2
+ class Account < Mpayer::Endpoint
3
+ attr_writer :members, :payable_items
4
+
5
+ class << self
6
+
7
+ # Mpayer::Account.all()
8
+ def all(per:1,per_page:100)
9
+ url = "/accounts/all_accounts"
10
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page})
11
+ end
12
+
13
+ # options = {from_date:nil,to_date:nil, dr_cr:nil, ac_type:nil, category:nil}
14
+ # Mpayer::Account.aggregate(options)
15
+ def aggregates(per:1,per_page:100, **options)
16
+ url = "/accounts/aggregates"
17
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page}.merge!(options))
18
+ end
19
+
20
+ # Mpayer::Account.find(account_id)
21
+ def find(account_id,fetch:true)
22
+ url = "/accounts/#{account_id}"
23
+ response = Mpayer::Fetch.get(url) if fetch
24
+ account = new(id:account_id,response:response)
25
+ end
26
+
27
+ end
28
+
29
+ # account.update(name:new_name)
30
+ def update(options)
31
+ url = "/accounts/#{self.id}"
32
+ response = Mpayer::Fetch.put(url,body: options.to_json)
33
+ self.response = response
34
+ end
35
+
36
+ def enroll
37
+
38
+ end
39
+
40
+ # Mpayer::Account.find(26, fetch:false).members(per:1,per_page:100)
41
+ def members(per:1,per_page:100)
42
+ find_all(per:per,per_page:per_page)
43
+ end
44
+
45
+ # Mpayer::Account.find(26, fetch:false).payable_items(per:1,per_page:100)
46
+ def payable_items(per:1,per_page:100)
47
+ find_all(per:per,per_page:per_page)
48
+ end
49
+
50
+ protected
51
+
52
+ def after_initialize
53
+ @endpoint = 'accounts'
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,72 @@
1
+ module Mpayer
2
+ class Client < Mpayer::Endpoint
3
+ attr_writer :accounts, :account, :payables, :transactions
4
+
5
+ class << self
6
+
7
+ # Mpayer::Client.all
8
+ def all(per:1,per_page:100)
9
+ url = "/clients/all_clients"
10
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page})
11
+ end
12
+
13
+ # Mpayer::Client.find(id)
14
+ def find(client_id,fetch: true)
15
+ url = "/clients/#{client_id}"
16
+ response = Mpayer::Fetch.get(url) if fetch
17
+ client = new(id:client_id,response:response)
18
+ end
19
+
20
+ # client_attributes = {client: { client_name: "Kiki Lolo", client_birthday: Time.now.iso8601, client_type: "ext", ac_type: "cu",client_mobile: '073373932', client_email: 'lolo@kiki.com',currency: "kes", mandate:"s", sub_type: "od" }}
21
+ # Mpayer::Client.create(client_attributes)
22
+ def create(options={})
23
+ url = "/clients"
24
+ response = Mpayer::Fetch.post(url,body: options.to_json)
25
+ client = new(options.merge!(id:response.id ,response:response))
26
+ end
27
+
28
+ end
29
+
30
+ # Mpayer::Client.find(id:20284).account(accountsid)
31
+ def account(account_id=nil,client_id=id)
32
+ account_id ||= @account.id rescue nil
33
+ raise ArgumentError if client_id.nil? or account_id.nil?
34
+ url = "/clients/#{client_id}/accounts/#{account_id}"
35
+ if (@account.id == account_id rescue false)
36
+ @account ||= Mpayer::Fetch.get(url)
37
+ else
38
+ @account = Mpayer::Fetch.get(url)
39
+ end
40
+ end
41
+
42
+ # options = {account:{name: name, ac_type: ac_type, mandate: mandate, tags_attributes:@tags, infos_attributes:@infos}}
43
+ # Mpayer::Client.find(id:20284).create_account(options)
44
+ def create_account(options={})
45
+ url = "/clients/#{self.id}/accounts/new"
46
+ @account = Mpayer::Fetch.post(url,body: options.to_json)
47
+ end
48
+
49
+ # Mpayer::Client.find(26, fetch:false).accounts(per:1,per_page:100)
50
+ def accounts(per:1,per_page:100)
51
+ find_all(per:per,per_page:per_page)
52
+ end
53
+
54
+ # Mpayer::Client.find(26, fetch:false).payables(per:1,per_page:100)
55
+ def payables(per:1,per_page:100)
56
+ find_all(per:per,per_page:per_page)
57
+ end
58
+
59
+ # Mpayer::Client.find(26, fetch:false).transactions(account_id, per:1,per_page:100)
60
+ def transactions(account_id,per:1,per_page:100)
61
+ find_all(per:per,per_page:per_page,account_id:account_id)
62
+ end
63
+
64
+ protected
65
+
66
+ def after_initialize
67
+ @endpoint = 'clients'
68
+ end
69
+
70
+
71
+ end
72
+ end
@@ -0,0 +1,89 @@
1
+ module Mpayer
2
+ class Endpoint
3
+ attr_accessor :config, :response, :pagination, :endpoint, :attributes
4
+ attr_accessor :id
5
+
6
+ # Mpayer::Model.new(options={})
7
+ def initialize(config:nil,**keyword_args)
8
+ @attributes = Hashie::Mash.new(keyword_args)
9
+ keyword_args.each do |k,v|
10
+ self.send("#{k.to_s}=",v) rescue nil
11
+ end
12
+ @config = set_config(config)
13
+ # @is_new ||= true
14
+ @pagination ||= {}
15
+ after_initialize
16
+ end
17
+
18
+ protected
19
+
20
+ # after_initialize{ @endpoint = 'clients'}
21
+ # class << self
22
+ # # Used to instantiate new objects instead of using def initialize
23
+ # def after_initialize#(&block)
24
+ # # block.call
25
+ # # yield if block_given?
26
+ # yield
27
+ # end
28
+ # end
29
+
30
+ def after_initialize
31
+
32
+ end
33
+
34
+ # Use this methon in the association reader method witht the same name
35
+ def find_all(per:1,per_page:100,**options)
36
+ # gets caller methods as association
37
+ association = caller_locations(1,1)[0].label
38
+ url = assoc_link(association.to_sym,options)
39
+ if self.pagination[association]=={per_page:per_page,per:per}
40
+ self.send("#{association}=", Mpayer::Fetch.get(url,query:{per:per,per_page:per_page}) ) if instance_variable_get("@#{association}").nil?
41
+ else
42
+ self.send("#{association}=", Mpayer::Fetch.get(url,query:{per:per,per_page:per_page}) )
43
+ end
44
+ self.pagination.merge!({"#{association}":{per:per,per_page:per_page}})
45
+ return instance_variable_get("@#{association}")
46
+ end
47
+
48
+ def assoc_link(association,**options)
49
+ case association
50
+ when :transactions
51
+ raise ArgumentError if options[:account_id].nil?
52
+ "/#{self.endpoint}/#{self.id}/accounts/#{options[:account_id]}/#{association}"
53
+ else # :accounts, :payables, :members
54
+ "/#{self.endpoint}/#{self.id}/#{association}"
55
+ end
56
+ end
57
+
58
+ # def self.has_associations(*attr_names)
59
+ # attr_names.each do |attr_name|
60
+ # # writers
61
+ # define_method("#{attr_name}=") do |value|
62
+ # instance_variable_set("@"+attr_name.to_s, value)
63
+ # end
64
+ # #readers
65
+ # define_method(attr_name) do
66
+ # instance_variable_get("@"+attr_name.to_s)
67
+ # end
68
+ # end
69
+ # end
70
+
71
+ def set_config(configuration)
72
+ config = Mpayer.configuration.clone
73
+ unless configuration.nil?
74
+ config.user_no = configuration[:user_no]
75
+ config.token = configuration[:token]
76
+ end
77
+ config
78
+ end
79
+
80
+ def method_missing(m, *args, &block)
81
+ if @response.is_a?(Hash)
82
+ @response.keys.include?(m.to_s) ? @response.send(m) : super
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,48 @@
1
+ module Mpayer
2
+ class Payable < Mpayer::Endpoint
3
+
4
+ class << self
5
+
6
+ # Mpayer::Payable.all
7
+ def all(per:1,per_page:100)
8
+ url = "/payables/all"
9
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page})
10
+ end
11
+
12
+ # Mpayer::Payable.find(id)
13
+ def find(payable_id,fetch:true)
14
+ url = "/payables/#{payable_id}"
15
+ response = Mpayer::Fetch.get(url) if fetch
16
+ payable = new(id:payable_id,response:response)
17
+ end
18
+
19
+ # Mpayer::Payable.where(ref_id:"KT0041[P]-010000402")
20
+ def where(ref_id:,fetch:true)
21
+ url = "/payables/search/#{CGI.escape(ref_id)}"
22
+ response = Mpayer::Fetch.get(url) if fetch
23
+ payable = new(id:response.id,response:response) unless (response.id rescue nil).nil?
24
+ end
25
+
26
+ # Mpayer::Payable.create(options)
27
+ def create(options={})
28
+ url = "/payables"
29
+ response = Mpayer::Fetch.post(url,body: options.to_json)
30
+ payable_id = response.id
31
+ payable = new(options.merge!(id:payable_id,response:response))
32
+ end
33
+ end
34
+
35
+ def destroy
36
+ url = "/payables/#{self.id}"
37
+ self.response = Mpayer::Fetch.delete(url)
38
+ self.id, self.attributes = nil, nil
39
+ end
40
+
41
+ protected
42
+
43
+ def after_initialize
44
+ @endpoint = 'payables'
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,58 @@
1
+ module Mpayer
2
+ class Transaction < Mpayer::Endpoint
3
+
4
+ class << self
5
+
6
+ # Mpayer::Transaction.all(from: Time.now - (86400*400))
7
+ def all(per:1,per_page:100,from: nil,to: nil)
8
+ url = "/transactions/all"
9
+ from = from.strftime("%F %T") unless from.nil?
10
+ to = to.strftime("%F %T") unless to.nil?
11
+ Mpayer::Fetch.get(url,query:{per:per,per_page:per_page,from:from,to:to})
12
+ end
13
+
14
+ # Mpayer::Transaction.where(ref_id:"KT0041[P]-010000402")
15
+ def where(ref_id:,fetch:true)
16
+ url = "/transactions/#{CGI.escape(ref_id)}"
17
+ response = Mpayer::Fetch.get(url) if fetch
18
+ transaction = new(id:response.id,response:response)
19
+ end
20
+
21
+ # body = {particulars:particulars,ref_id:mpayer_ref_id,amount:amount, cr_party: cr_party}
22
+ # Mpayer::Transaction.deposit(body)
23
+ def deposit(**options)
24
+ url = "/transactions/deposit"
25
+ response = Mpayer::Fetch.put(url,body: mpayer_refs.merge!(options).to_json)
26
+ transaction = new(id:response.id,response:response)
27
+ end
28
+
29
+ # body = {particulars:particulars,ref_id:mpayer_ref_id,amount:amount,dr_party: dr_party}
30
+ # Mpayer::Transaction.withdraw(body)
31
+ def withdraw(**options)
32
+ url = "/transactions/withdraw"
33
+ response = Mpayer::Fetch.delete(url,body: mpayer_refs.merge!(options).to_json)
34
+ transaction = new(id:response.id,response:response)
35
+ end
36
+
37
+ # body = {particulars:particulars,ref_id:mpayer_ref_id,amount:amount,dr_party: dr_party, cr_party: cr_party}
38
+ # Mpayer::Transaction.transfer(body)
39
+ def transfer(**options)
40
+ url = "/transactions/transfer"
41
+ response = Mpayer::Fetch.post(url,body: mpayer_refs.merge!(options).to_json)
42
+ transaction = new(id:response.id,response:response)
43
+ end
44
+
45
+ # Integrating app should get this from their db or just random unique
46
+ def mpayer_refs
47
+ {ref_id: SecureRandom.uuid}
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def after_initialize
54
+ @endpoint = 'transactions'
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module Mpayer
2
+ class Fetch
3
+ include HTTParty
4
+ # base_uri "http://beta.json-generator.com"
5
+ base_uri "https://app.mpayer.co.ke/api/"
6
+ parser proc {|data| Hashie::Mash.new(response: (JSON.parse(data) rescue {data:data}.to_json) ).response}
7
+ format :json
8
+ headers
9
+
10
+ class << self
11
+ def headers
12
+ Mpayer.configuration.header.merge!(super)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Mpayer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "wsse"
2
+ require "httparty"
3
+ require "hashie"
4
+ require "mpayer/version"
5
+ require "mpayer/configuration"
6
+ require "mpayer/fetch"
7
+ require "mpayer/endpoints/endpoint"
8
+ require "mpayer/endpoints/client"
9
+ require "mpayer/endpoints/transaction"
10
+ require "mpayer/endpoints/account"
11
+ require "mpayer/endpoints/payable"
12
+
13
+ # begin
14
+ # require "pry"
15
+ # require "pry-alias"
16
+ # rescue LoadError
17
+ # end
18
+
19
+ module Mpayer
20
+ class << self
21
+ attr_writer :configuration
22
+ end
23
+
24
+ def self.configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+
28
+ def self.reset
29
+ @configuration = Configuration.new
30
+ end
31
+
32
+ def self.setup
33
+ yield(configuration)
34
+ end
35
+
36
+ def self.login
37
+ Mpayer::Fetch.post('/login',post: {user_no: ENV['MPAYER_USER'],password:ENV['MPAYER_PASSWORD']}.to_json)
38
+ end
39
+
40
+ end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mpayer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mpayer_ruby"
8
+ spec.version = Mpayer::VERSION
9
+ spec.authors = ["Kariuki Gathitu"]
10
+ spec.email = ["kgathi2@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby client for interfacing with http://app.mpayer.co.ke .}
13
+ spec.description = %q{Interfaces with Mpayer payment gateway api }
14
+ spec.homepage = "https://github.com/kgathi2/mpayer_ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "guard", "~> 2.12"
33
+ spec.add_development_dependency "minitest", "~> 5.6"
34
+ spec.add_development_dependency "guard-minitest", "~> 2.4"
35
+ spec.add_development_dependency "minitest-reporters", "~> 1.0"
36
+ spec.add_development_dependency "pry", "~> 0.10"
37
+ spec.add_development_dependency "pry-nav", "~> 0.2"
38
+ spec.add_development_dependency "pry-alias", "~> 0.0"
39
+ spec.add_development_dependency "coveralls", '~> 0'
40
+
41
+ spec.add_dependency 'httparty', "~> 0.13"
42
+ spec.add_dependency 'wsse', "~> 0.0"
43
+ spec.add_dependency 'hashie', "~> 3.4"
44
+ end
metadata ADDED
@@ -0,0 +1,248 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mpayer_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kariuki Gathitu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-nav
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-alias
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: coveralls
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: httparty
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.13'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.13'
167
+ - !ruby/object:Gem::Dependency
168
+ name: wsse
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: hashie
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '3.4'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '3.4'
195
+ description: 'Interfaces with Mpayer payment gateway api '
196
+ email:
197
+ - kgathi2@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".coveralls.yml"
203
+ - ".gitignore"
204
+ - ".travis.yml"
205
+ - CODE_OF_CONDUCT.md
206
+ - Gemfile
207
+ - Guardfile
208
+ - LICENSE.txt
209
+ - README.md
210
+ - Rakefile
211
+ - bin/console
212
+ - bin/setup
213
+ - lib/mpayer-ruby.rb
214
+ - lib/mpayer/configuration.rb
215
+ - lib/mpayer/endpoints/account.rb
216
+ - lib/mpayer/endpoints/client.rb
217
+ - lib/mpayer/endpoints/endpoint.rb
218
+ - lib/mpayer/endpoints/payable.rb
219
+ - lib/mpayer/endpoints/transaction.rb
220
+ - lib/mpayer/fetch.rb
221
+ - lib/mpayer/version.rb
222
+ - lib/mpayer_ruby.rb
223
+ - mpayer_ruby.gemspec
224
+ homepage: https://github.com/kgathi2/mpayer_ruby
225
+ licenses:
226
+ - MIT
227
+ metadata: {}
228
+ post_install_message:
229
+ rdoc_options: []
230
+ require_paths:
231
+ - lib
232
+ required_ruby_version: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ required_rubygems_version: !ruby/object:Gem::Requirement
238
+ requirements:
239
+ - - ">="
240
+ - !ruby/object:Gem::Version
241
+ version: '0'
242
+ requirements: []
243
+ rubyforge_project:
244
+ rubygems_version: 2.4.5
245
+ signing_key:
246
+ specification_version: 4
247
+ summary: Ruby client for interfacing with http://app.mpayer.co.ke .
248
+ test_files: []