platbamobilom 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30e89a5b45a51ba40cdd6376daf7be1688e4f7a6
4
+ data.tar.gz: 26c377da04745e555fd3d64b8867d26b6c3f9dc9
5
+ SHA512:
6
+ metadata.gz: e15a1b4a84a0271aeb6e406c108d24bf704c49a800f32d47072d213227577569d1ee1275de596565de8f07503d5684da64f3b3e106607f9d2b6f5a2c89d97a04
7
+ data.tar.gz: 247015ba56de82f9aaf7550d7232b6455e41414f751e26c56ccd9bec9cf1d77a42206936f20cb413d2fa75618b04237c2ab15ff008334c3576dbd041e9955a97
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in platbamobilom.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ahmed Al Hafoudh
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.
@@ -0,0 +1,71 @@
1
+ # platbamobilom [![Build Status](https://travis-ci.org/alhafoudh/platbamobilom.svg?branch=develop)](https://travis-ci.org/alhafoudh/platbamobilom)
2
+
3
+ This gem provides a set of helpers to generate and verify signatures of [platbamobilom.sk](http://www.platbamobilom.sk) service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'platbamobilom'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install platbamobilom
20
+
21
+ ## Usage
22
+
23
+
24
+ ```ruby
25
+ # Generate signature
26
+
27
+ secret = 'test'
28
+
29
+ redirect = Platbamobilom::Redirect.new(
30
+ pid: '1',
31
+ id: '445566',
32
+ desc: 'some description',
33
+ price: '3.10',
34
+ url: 'http://redirect.url',
35
+ email: 'sales@example.com'
36
+ )
37
+
38
+ signature = redirect.sign(secret) # => 'AF7E772D957225E8A40E5D77801D095AFD82FBBE600BC9CB9DE06A514621D02F'
39
+
40
+ # Generate signed URL
41
+
42
+ redirect.signed_url(signature) # => 'https://pay.platbamobilom.sk/test/?PID=1&ID=445566&DESC=some+description&PRICE=3.10&URL=http%3A%2F%2Fredirect.url&EMAIL=sales%40example.com&SIGN=AF7E772D957225E8A40E5D77801D095AFD82FBBE600BC9CB9DE06A514621D02F'
43
+
44
+ # Verify signature
45
+
46
+ verification = Platbamobilom::Verification.new(
47
+ id: '445566',
48
+ result: 'OK',
49
+ phone: '421903000000',
50
+ signature: '2FD327601CC033ED60A610EAF1239C5E958774426F6CC1D6B48DE422336784A3'
51
+ )
52
+
53
+ verification.verify(secret) # => true
54
+
55
+ verification = Platbamobilom::Verification.new(
56
+ id: '445566',
57
+ result: 'FAIL',
58
+ phone: '421903000000',
59
+ signature: '2FD327601CC033ED60A610EAF1239C5E958774426F6CC1D6B48DE422336784A3'
60
+ )
61
+
62
+ verification.verify(secret) # => false
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( https://github.com/alhafoudh/platbamobilom/fork )
68
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
71
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
@@ -0,0 +1,17 @@
1
+ require 'rack/utils'
2
+
3
+ require 'platbamobilom/version'
4
+ require 'platbamobilom/redirect'
5
+ require 'platbamobilom/verification'
6
+
7
+ module Platbamobilom
8
+ class << self
9
+ attr_accessor :test_mode
10
+
11
+ def reset!
12
+ self.test_mode = true
13
+ end
14
+ end
15
+ end
16
+
17
+ Platbamobilom.reset!
@@ -0,0 +1,50 @@
1
+ module Platbamobilom
2
+ class Redirect
3
+ PRODUCTION_URL = 'https://pay.platbamobilom.sk/pay/'.freeze
4
+ TEST_URL = 'https://pay.platbamobilom.sk/test/'.freeze
5
+
6
+ def self.production_url; PRODUCTION_URL end
7
+ def self.test_url; TEST_URL end
8
+ def self.url; (Platbamobilom.test_mode ? test_url : production_url) end
9
+
10
+ attr_reader :pid
11
+ attr_reader :id
12
+ attr_reader :desc
13
+ attr_reader :price
14
+ attr_reader :url
15
+ attr_reader :email
16
+
17
+ def initialize(pid:, id:, desc:, price:, url:, email:)
18
+ @pid = pid
19
+ @id = id
20
+ @desc = desc
21
+ @price = price
22
+ @url = url
23
+ @email = email
24
+ end
25
+
26
+ def sign(secret)
27
+ signing_data = [pid, id, desc, price, url, email].join
28
+ digest = OpenSSL::Digest.new('sha256')
29
+ hmac = OpenSSL::HMAC.digest(digest, secret, signing_data)
30
+ signature = hmac.unpack('H*').join.upcase
31
+ end
32
+
33
+ def signed_query_string(signature)
34
+ Rack::Utils.build_query({
35
+ PID: pid,
36
+ ID: id,
37
+ DESC: desc,
38
+ PRICE: price,
39
+ URL: url,
40
+ EMAIL: email,
41
+ SIGN: signature
42
+ })
43
+ end
44
+
45
+ def signed_url(signature)
46
+ '%s?%s' % [self.class.url, signed_query_string(signature)]
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+ module Platbamobilom
2
+ class Verification
3
+ attr_reader :id
4
+ attr_reader :result
5
+ attr_reader :phone
6
+ attr_reader :signature
7
+
8
+ def initialize(id:, result:, phone:, signature:)
9
+ @id = id
10
+ @result = result
11
+ @phone = phone
12
+ @signature = signature
13
+ end
14
+
15
+ def verify(secret)
16
+ signing_data = [id, result, phone].join
17
+ digest = OpenSSL::Digest.new('sha256')
18
+ hmac = OpenSSL::HMAC.digest(digest, secret, signing_data)
19
+ our_signature = hmac.unpack('H*').join.upcase
20
+
21
+ our_signature == signature
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Platbamobilom
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'platbamobilom/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'platbamobilom'
8
+ spec.version = Platbamobilom::VERSION
9
+ spec.authors = ['Ahmed Al Hafoudh']
10
+ spec.email = ['alhafoudh@freevision.sk']
11
+ spec.summary = %q{Gem for platbamobilom.sk signature generation and verification}
12
+ spec.description = %q{This gem provides a set of helpers to generate and verify signatures of http://www.platbamobilom.sk service.}
13
+ spec.homepage = 'https://github.com/alhafoudh/platbamobilom'
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_development_dependency 'bundler', '>= 1.3.0', '< 2.0'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'minitest', '~> 5.4.2'
24
+ spec.add_development_dependency 'pry', '~> 0.9.12.6'
25
+
26
+ spec.add_dependency 'rack', '~> 1.5.2'
27
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'platbamobilom'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,23 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Platbamobilom do
4
+ before(:each) do
5
+ Platbamobilom.reset!
6
+ end
7
+
8
+ it 'has a version number' do
9
+ refute_nil ::Platbamobilom::VERSION
10
+ end
11
+
12
+ it 'is in test mode as default' do
13
+ assert_equal true, Platbamobilom.test_mode
14
+ end
15
+
16
+ it 'can disable test mode' do
17
+ assert_equal true, Platbamobilom.test_mode
18
+
19
+ Platbamobilom.test_mode = false
20
+
21
+ assert_equal false, Platbamobilom.test_mode
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Platbamobilom::Redirect do
4
+ before(:each) do
5
+ Platbamobilom.test_mode = true
6
+ end
7
+
8
+ let(:described_class) { Platbamobilom::Redirect }
9
+ let(:subject) do
10
+ described_class.new(
11
+ pid: '1',
12
+ id: '445566',
13
+ desc: 'some description',
14
+ price: '3.10',
15
+ url: 'http://redirect.url',
16
+ email: 'sales@example.com'
17
+ )
18
+ end
19
+ let(:secret) { 'test' }
20
+ let(:signature) { 'AF7E772D957225E8A40E5D77801D095AFD82FBBE600BC9CB9DE06A514621D02F' }
21
+ let(:signed_query_string) { 'PID=1&ID=445566&DESC=some+description&PRICE=3.10&URL=http%3A%2F%2Fredirect.url&EMAIL=sales%40example.com&SIGN=AF7E772D957225E8A40E5D77801D095AFD82FBBE600BC9CB9DE06A514621D02F' }
22
+ let(:signed_url) { 'https://pay.platbamobilom.sk/test/?PID=1&ID=445566&DESC=some+description&PRICE=3.10&URL=http%3A%2F%2Fredirect.url&EMAIL=sales%40example.com&SIGN=AF7E772D957225E8A40E5D77801D095AFD82FBBE600BC9CB9DE06A514621D02F' }
23
+
24
+ it 'has redirect url for specific environment' do
25
+ assert_equal 'https://pay.platbamobilom.sk/pay/', described_class.production_url
26
+ assert_equal 'https://pay.platbamobilom.sk/test/', described_class.test_url
27
+ end
28
+
29
+ it 'initializes with all required parameters' do
30
+ assert_equal subject.pid, '1'
31
+ assert_equal subject.id, '445566'
32
+ assert_equal subject.desc, 'some description'
33
+ assert_equal subject.price, '3.10'
34
+ assert_equal subject.url, 'http://redirect.url'
35
+ assert_equal subject.email, 'sales@example.com'
36
+ end
37
+
38
+ it 'signes the parameters correctly' do
39
+ assert_equal signature, subject.sign(secret)
40
+ end
41
+
42
+ it 'generates signed query string' do
43
+ assert_equal signed_query_string, subject.signed_query_string(signature)
44
+ end
45
+
46
+ it 'generates signed url' do
47
+ assert_equal signed_url, subject.signed_url(signature)
48
+ end
49
+
50
+ it 'has different url based on test mode' do
51
+ Platbamobilom.test_mode = true
52
+ assert_equal described_class.test_url, described_class.url
53
+
54
+ Platbamobilom.test_mode = false
55
+ assert_equal described_class.production_url, described_class.url
56
+ end
57
+ end
@@ -0,0 +1,34 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Platbamobilom::Verification do
4
+ let(:described_class) { Platbamobilom::Verification }
5
+ let(:subject) do
6
+ described_class.new(
7
+ id: '445566',
8
+ result: 'OK',
9
+ phone: '421903000000',
10
+ signature: '2FD327601CC033ED60A610EAF1239C5E958774426F6CC1D6B48DE422336784A3'
11
+ )
12
+ end
13
+ let(:bad_subject) do
14
+ described_class.new(
15
+ id: '445566',
16
+ result: 'FAIL',
17
+ phone: '421903000000',
18
+ signature: '2FD327601CC033ED60A610EAF1239C5E958774426F6CC1D6B48DE422336784A3'
19
+ )
20
+ end
21
+ let(:secret) { 'test' }
22
+
23
+ it 'initializes with all required parameters' do
24
+ assert_equal subject.id, '445566'
25
+ assert_equal subject.result, 'OK'
26
+ assert_equal subject.phone, '421903000000'
27
+ assert_equal subject.signature, '2FD327601CC033ED60A610EAF1239C5E958774426F6CC1D6B48DE422336784A3'
28
+ end
29
+
30
+ it 'verifies signature correctly' do
31
+ assert_equal true, subject.verify(secret)
32
+ assert_equal false, bad_subject.verify(secret)
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: platbamobilom
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ahmed Al Hafoudh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-02 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.3.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '10.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '10.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 5.4.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 5.4.2
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.9.12.6
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.12.6
75
+ - !ruby/object:Gem::Dependency
76
+ name: rack
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 1.5.2
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 1.5.2
89
+ description: This gem provides a set of helpers to generate and verify signatures
90
+ of http://www.platbamobilom.sk service.
91
+ email:
92
+ - alhafoudh@freevision.sk
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - lib/platbamobilom.rb
104
+ - lib/platbamobilom/redirect.rb
105
+ - lib/platbamobilom/verification.rb
106
+ - lib/platbamobilom/version.rb
107
+ - platbamobilom.gemspec
108
+ - test/minitest_helper.rb
109
+ - test/test_platbamobilom.rb
110
+ - test/test_redirect.rb
111
+ - test/test_verification.rb
112
+ homepage: https://github.com/alhafoudh/platbamobilom
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.2.2
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Gem for platbamobilom.sk signature generation and verification
136
+ test_files:
137
+ - test/minitest_helper.rb
138
+ - test/test_platbamobilom.rb
139
+ - test/test_redirect.rb
140
+ - test/test_verification.rb