mpower 1.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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.DS_Store
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mpower_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alfred Robert Rowe
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,29 @@
1
+ # MpowerRuby
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mpower'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mpower
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,131 @@
1
+ module MPower
2
+ module Checkout
3
+ class Invoice < MPower::Checkout::Core
4
+
5
+ attr_accessor :items, :total_amount, :taxes, :description, :currency, :store
6
+ attr_accessor :customer, :custom_data, :cancel_url, :return_url, :invoice_url, :receipt_url
7
+
8
+ def initialize
9
+ @items = {}
10
+ @taxes = {}
11
+ @custom_data = {}
12
+ @customer = {}
13
+ @total_amount = 0.0
14
+ @currency = "ghs"
15
+ @store = MPower::Checkout::Store
16
+ @return_url = @store.return_url
17
+ @cancel_url = @store.cancel_url
18
+ end
19
+
20
+ # Adds invoice items to the @items hash, the idea is to allow this function to be used in a loop
21
+ def add_item(name,quantity,unit_price,total_price)
22
+ @items.merge!({
23
+ :"item_#{@items.size}" => {
24
+ :name => name,
25
+ :quantity => quantity,
26
+ :unit_price => unit_price,
27
+ :total_price => total_price
28
+ }
29
+ })
30
+ end
31
+
32
+ # Adds invoice tax to the @taxes hash, the idea is to allow this function to be used in a loop
33
+ def add_tax(name,amount)
34
+ @taxes.merge!({
35
+ :"tax_#{@taxes.size}" => {
36
+ :name => name,
37
+ :amount => amount
38
+ }
39
+ })
40
+ end
41
+
42
+ def get_items
43
+ hash_to_json @items
44
+ end
45
+
46
+ def get_taxes
47
+ hash_to_json @taxes
48
+ end
49
+
50
+ def get_customer_info(key)
51
+ @customer["#{key}"]
52
+ end
53
+
54
+ def get_custom_data(key)
55
+ @custom_data["#{name}"]
56
+ end
57
+
58
+ def confirm(token)
59
+ result = http_get_request("#{MPower::Setup.checkout_confirm_base_url}#{token}");
60
+ if result.size > 0
61
+ case result['status']
62
+ when 'completed'
63
+ @status = result['status']
64
+ @customer = result['customer']
65
+ @items = result['invoice']['items']
66
+ @taxes = result['invoice']['taxes']
67
+ @description = result['invoice']['description']
68
+ @custom_data = result['custom_data']
69
+ @total_amount = result['invoice']['total_amount']
70
+ @receipt_url = result['receipt_url']
71
+ true
72
+ else
73
+ @status = result['status']
74
+ @items = result['invoice']['items']
75
+ @taxes = result['invoice']['taxes']
76
+ @description = result['invoice']['description']
77
+ @custom_data = result['custom_data']
78
+ @total_amount = result['invoice']['total_amount']
79
+ @response_text = "Invoice status is #{result['status'].upcase}"
80
+ false
81
+ end
82
+ else
83
+ @response_text = "Invoice Not Found"
84
+ @response_code = 1002
85
+ @status = MPower::FAIL
86
+ false
87
+ end
88
+ end
89
+
90
+ def create
91
+ checkout_payload = {
92
+ :invoice => {
93
+ :items => @items,
94
+ :taxes => @taxes,
95
+ :total_amount => @total_amount,
96
+ :description => description
97
+ },
98
+ :store => {
99
+ :name => @store.name,
100
+ :tagline => @store.tagline,
101
+ :postal_address => @store.postal_address,
102
+ :phone => @store.phone_number,
103
+ :logo_url => @store.logo_url,
104
+ :website_url => @store.website_url
105
+ },
106
+ :actions => {
107
+ :cancel_url => @cancel_url,
108
+ :return_url => @return_url
109
+ }
110
+ }
111
+
112
+ result = http_json_request(MPower::Setup.checkout_base_url,checkout_payload)
113
+ case result["response_code"]
114
+ when "00"
115
+ @response_text = result["response_description"]
116
+ @response_code = result["response_code"]
117
+ @invoice_url = result["response_text"]
118
+ @status = MPower::SUCCESS
119
+ true
120
+ else
121
+ @response_text = result["response_text"]
122
+ @response_code = result["response_code"]
123
+ @invoice_url = nil
124
+ @status = MPower::FAIL
125
+ false
126
+ end
127
+ end
128
+
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,31 @@
1
+ module MPower
2
+ module Checkout
3
+ module Store
4
+ @@name = "Untitled Store"
5
+ @@tagline = "Put your online store's tagline here."
6
+ @@postal_address = nil
7
+ @@phone_number = nil
8
+ @@website_url = nil
9
+ @@logo_url = nil
10
+ @@cancel_url = nil
11
+ @@return_url = nil
12
+
13
+ def self.name=(name); @@name = name; end
14
+ def self.name; @@name; end
15
+ def self.tagline=(tagline); @@tagline = tagline; end
16
+ def self.tagline; @@tagline; end
17
+ def self.postal_address=(postal_address); @@postal_address = postal_address; end
18
+ def self.postal_address; @@postal_address; end
19
+ def self.phone_number=(phone_number); @@phone_number = phone_number; end
20
+ def self.phone_number; @@phone_number; end
21
+ def self.website_url=(website_url); @@website_url = website_url; end
22
+ def self.website_url; @@website_url; end
23
+ def self.logo_url=(logo_url); @@logo_url = logo_url; end
24
+ def self.logo_url; @@logo_url; end
25
+ def self.cancel_url=(cancel_url); @@cancel_url = cancel_url; end
26
+ def self.cancel_url; @@cancel_url; end
27
+ def self.return_url=(return_url); @@return_url = return_url; end
28
+ def self.return_url; @@return_url; end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module MPower
2
+ module Checkout
3
+ class Core
4
+ include MPower::Utilities
5
+
6
+ attr_accessor :status, :response_text, :response_code
7
+ end
8
+ end
9
+ end
10
+
11
+ %w(checkout/invoice
12
+ checkout/store
13
+ ).each do |lib|
14
+ require File.join(File.dirname(__FILE__), lib)
15
+ end
@@ -0,0 +1,34 @@
1
+ module MPower
2
+ module Setup
3
+ @@master_key = nil
4
+ @@private_key = nil
5
+ @@public_key = nil
6
+ @@token = nil
7
+ @@mode = "test"
8
+
9
+ LIVE_CHECKOUT_INVOICE_BASE_URL = "https://app.mpowerpayments.com/api/v1/checkout-invoice/create";
10
+ TEST_CHECKOUT_INVOICE_BASE_URL = "https://app.mpowerpayments.com/sandbox-api/v1/checkout-invoice/create";
11
+ LIVE_CHECKOUT_CONFIRM_BASE_URL = "https://app.mpowerpayments.com/api/v1/checkout-invoice/confirm/";
12
+ TEST_CHECKOUT_CONFIRM_BASE_URL = "https://app.mpowerpayments.com/sandbox-api/v1/checkout-invoice/confirm/";
13
+
14
+ def self.master_key=(master_key); @@master_key = master_key; end
15
+ def self.master_key; @@master_key; end
16
+ def self.private_key=(private_key); @@private_key = private_key; end
17
+ def self.private_key; @@private_key; end
18
+ def self.public_key=(public_key); @@public_key = public_key; end
19
+ def self.public_key; @@public_key; end
20
+ def self.token=(token); @@token = token; end
21
+ def self.token; @@token; end
22
+
23
+ def self.mode=(mode); @@mode = mode; end
24
+ def self.mode; @@mode; end
25
+
26
+ def self.checkout_base_url
27
+ @@mode == "live" ? LIVE_CHECKOUT_INVOICE_BASE_URL : TEST_CHECKOUT_INVOICE_BASE_URL
28
+ end
29
+
30
+ def self.checkout_confirm_base_url
31
+ @@mode == "live" ? LIVE_CHECKOUT_CONFIRM_BASE_URL : TEST_CHECKOUT_CONFIRM_BASE_URL
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ module MPower
2
+ module Utilities
3
+ def http_json_request(baseurl,payload={})
4
+ conn = Faraday.new(:url => baseurl, :ssl => {:verify => false}) do |faraday|
5
+ faraday.request :json
6
+ faraday.adapter Faraday.default_adapter
7
+ end
8
+
9
+ result = conn.post do |req|
10
+ req.headers["User-Agent"] = "MPower Checkout API Ruby client v1 aka Don Nigalon"
11
+ req.headers['MP-Public-Key'] = MPower::Setup.public_key
12
+ req.headers['MP-Private-Key'] = MPower::Setup.private_key
13
+ req.headers['MP-Master-Key'] = MPower::Setup.master_key
14
+ req.headers['MP-Token'] = MPower::Setup.token
15
+ req.headers['MP-Mode'] = MPower::Setup.mode
16
+ req.body = hash_to_json payload
17
+ end
18
+ json_to_hash(result.body)
19
+ end
20
+
21
+ def http_get_request(baseurl)
22
+ conn = Faraday.new(:url => baseurl, :ssl => {:verify => false})
23
+
24
+ result = conn.get do |req|
25
+ req.headers["User-Agent"] = "MPower Checkout API Ruby client v1 aka Don Nigalon"
26
+ req.headers['MP-Public-Key'] = MPower::Setup.public_key
27
+ req.headers['MP-Private-Key'] = MPower::Setup.private_key
28
+ req.headers['MP-Master-Key'] = MPower::Setup.master_key
29
+ req.headers['MP-Token'] = MPower::Setup.token
30
+ req.headers['MP-Mode'] = MPower::Setup.mode
31
+ end
32
+
33
+ json_to_hash(result.body)
34
+ end
35
+
36
+ def hash_to_json(params={})
37
+ MultiJson.dump params
38
+ end
39
+
40
+ def json_to_hash(params={})
41
+ MultiJson.load params
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module MPower
2
+ VERSION = "1.0.2"
3
+ end
data/lib/mpower.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'multi_json'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require "mpower/version"
5
+ require "mpower/setup"
6
+ require "mpower/utilities"
7
+ require "mpower/checkout"
8
+
9
+ module MPower
10
+ SUCCESS = "success"
11
+ FAIL = "fail"
12
+ PENDING = "pending"
13
+ end
data/mpower.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mpower/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mpower"
8
+ gem.version = MPower::VERSION
9
+ gem.authors = ["Alfred Rowe"]
10
+ gem.email = ["alfred@ncodedevlabs.com"]
11
+ gem.description = %q{Ruby library for integrating with the MPower Gateway}
12
+ gem.summary = %q{Ruby client bindings for the MPower API}
13
+ gem.homepage = "https://github.com/nukturnal/mpower_ruby"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency('rest-client', '~> 1.4')
20
+ gem.add_dependency('multi_json', '~> 1.1')
21
+ gem.add_dependency('faraday')
22
+ gem.add_dependency('faraday_middleware')
23
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mpower
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alfred Rowe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.1'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday_middleware
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby library for integrating with the MPower Gateway
79
+ email:
80
+ - alfred@ncodedevlabs.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/mpower.rb
91
+ - lib/mpower/checkout.rb
92
+ - lib/mpower/checkout/invoice.rb
93
+ - lib/mpower/checkout/store.rb
94
+ - lib/mpower/setup.rb
95
+ - lib/mpower/utilities.rb
96
+ - lib/mpower/version.rb
97
+ - mpower.gemspec
98
+ homepage: https://github.com/nukturnal/mpower_ruby
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Ruby client bindings for the MPower API
122
+ test_files: []
123
+ has_rdoc: