parichki_bg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parichki_bg.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Toma
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
+ # ParichkiBg
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'parichki_bg'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install parichki_bg
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,46 @@
1
+ require "parichki_bg/version"
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require "base64"
5
+ require 'uri'
6
+ require 'active_support'
7
+ require 'json'
8
+
9
+
10
+ require "parichki_bg/request/base"
11
+ require "parichki_bg/request/query"
12
+ require "parichki_bg/request/transaction"
13
+ require "parichki_bg/railtie" if defined? Rails
14
+
15
+ module ParichkiBg
16
+
17
+ class << self
18
+
19
+
20
+ def vendor_id
21
+ @@vendor_id
22
+ end
23
+
24
+ def secret_key
25
+ @@secret_key
26
+ end
27
+
28
+ def vendor_id=(vendor_id)
29
+ @@vendor_id = vendor_id
30
+ end
31
+
32
+ def secret_key=(secret_key)
33
+ @@secret_key = secret_key
34
+ end
35
+
36
+ def main_api_url
37
+ "https://api.dir.bg"
38
+ end
39
+
40
+ def hmac(string)
41
+ OpenSSL::HMAC.hexdigest("sha256", secret_key, string)
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,9 @@
1
+ module ParichkiBg
2
+ class Railtie < Rails::Railtie
3
+ initializer "ParichkiBg.add_configurations" do
4
+ config = YAML.load_file Rails.root.join("config", "parichki_bg.yml")
5
+ ParichkiBg::secret_key = config[Rails.env]["secret_key"]
6
+ ParichkiBg::vendor_id = config[Rails.env]["vendor_id"]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ module ParichkiBg
2
+ module Request
3
+ class Base
4
+
5
+ attr_reader :amount, :pin, :user_id
6
+
7
+ def initialize(attributes = {})
8
+ @amount = attributes.fetch :amount
9
+ @pin = attributes.fetch :pin
10
+ @user_id = attributes.fetch :user_id
11
+ end
12
+
13
+ def https
14
+ @http ||= Net::HTTP.new uri.host, uri.port
15
+ @http.use_ssl = true
16
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
17
+ @http
18
+ end
19
+
20
+ def send
21
+ @response = https.get uri.request_uri
22
+ end
23
+
24
+ def signed_request
25
+ encoded_checksum + "." + encoded_data
26
+ end
27
+
28
+ def checksum
29
+ ParichkiBg.hmac to_json
30
+ end
31
+
32
+ def encoded_checksum
33
+ Base64.strict_encode64 checksum
34
+ end
35
+
36
+ def encoded_data
37
+ Base64.strict_encode64 to_json
38
+ end
39
+
40
+ def result
41
+ if success?
42
+ ActiveSupport::JSON.decode @response.body
43
+ else
44
+ {"error" => "UserNotFound"}
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,65 @@
1
+ module ParichkiBg
2
+ module Request
3
+
4
+ class UsedRequestId < StandardError
5
+ end
6
+
7
+ class Query < ParichkiBg::Request::Base
8
+ attr_reader :status
9
+
10
+ #status 0 unused request_id
11
+ #status 1 used request_id
12
+
13
+ def initialize(attributes = {})
14
+ super
15
+ @pin = attributes[:pin]
16
+ @status = 0
17
+ end
18
+
19
+ def uri
20
+ URI "#{ParichkiBg::main_api_url}/#{user_id}/data?vendor_id=#{ParichkiBg::vendor_id}&signed_request=#{signed_request}"
21
+ end
22
+
23
+ def send
24
+ @status = 0
25
+ super
26
+ end
27
+
28
+ def to_json
29
+ {
30
+ amount: "#{amount}"
31
+ }.to_json
32
+ end
33
+
34
+ def can_pay?
35
+ !!result["allow_virtual"]
36
+ end
37
+
38
+ def success?
39
+ !!( @response.code == "200" && @response.body.match(/user_id/) )
40
+ end
41
+
42
+ def build_transaction
43
+ raise UsedRequestId if @status == 1
44
+ used_request_id
45
+ Transaction.new(amount: amount, request_id: result["request_id"], user_id: user_id, pin: pin)
46
+ end
47
+
48
+ def make_cash_transaction
49
+ build_transaction.tap {|x| x.pay_with_cash}
50
+ end
51
+
52
+ def make_point_transaction
53
+ build_transaction.tap {|x| x.pay_with_points}
54
+ end
55
+
56
+ private
57
+
58
+ def used_request_id
59
+ @status = 1
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,46 @@
1
+ module ParichkiBg
2
+ module Request
3
+ class Transaction < ParichkiBg::Request::Base
4
+
5
+ attr_reader :request_id
6
+ attr_accessor :type
7
+
8
+ def initialize(attributes = {})
9
+ super
10
+ @request_id = attributes.fetch :request_id
11
+ end
12
+
13
+ def uri
14
+ URI "#{ParichkiBg::main_api_url}/#{user_id}/transactions?vendor_id=#{ParichkiBg::vendor_id}&signed_request=#{signed_request}"
15
+ end
16
+
17
+ def to_json
18
+ {
19
+ amount: amount.to_s,
20
+ request_id: request_id.to_s,
21
+ type: type,
22
+ pin: pin
23
+ }.to_json
24
+ end
25
+
26
+ def pay_with_cash
27
+ @type = "cash"
28
+ send
29
+ end
30
+
31
+ def pay_with_points
32
+ @type = "points"
33
+ send
34
+ end
35
+
36
+ def success?
37
+ @response.code == "200" && !@response.body.match(/error/)
38
+ end
39
+
40
+ def paid?
41
+ result["error"].nil?
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module ParichkiBg
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parichki_bg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parichki_bg"
8
+ spec.version = ParichkiBg::VERSION
9
+ spec.authors = ["Toma"]
10
+ spec.email = ["t0ma.popov.90@gmail.com"]
11
+ spec.description = "Simple gem for communication with Dir.bg 'parichki' service. Allows to make request for getting the current user status and for making transactions to Parichki.bg"
12
+ spec.summary = "Connect to Dir.bg 'Parichki' service"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activesupport"
24
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parichki_bg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Toma
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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
+ description: Simple gem for communication with Dir.bg 'parichki' service. Allows to
63
+ make request for getting the current user status and for making transactions to
64
+ Parichki.bg
65
+ email:
66
+ - t0ma.popov.90@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/parichki_bg.rb
77
+ - lib/parichki_bg/railtie.rb
78
+ - lib/parichki_bg/request/base.rb
79
+ - lib/parichki_bg/request/query.rb
80
+ - lib/parichki_bg/request/transaction.rb
81
+ - lib/parichki_bg/version.rb
82
+ - parichki_bg.gemspec
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 1329468548347980228
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 1329468548347980228
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.25
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Connect to Dir.bg 'Parichki' service
114
+ test_files: []