payture-cheques 0.1.0

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: 9a1650598deeda99267fcb89839eff8a4fed1cd0
4
+ data.tar.gz: 3b49b35dc96a4550a5d76b8d8e03154651f1324c
5
+ SHA512:
6
+ metadata.gz: af0a1b36a2bfc8fe1f6b840e1ea63f7fdb47fb9b6c3448a88cb02a08d8ca93515bd156d686e0df26e918ae173ccecddbc9fe2a7a86f0b99c878a8d894470a353
7
+ data.tar.gz: bac86af0856cce8def1f34ff8e1e7b1e634753b9fd1e1b065f5b52794073c9adc1d4cbc9aa79da43249d4dd39c3599c5977912f6bc6b657e7fc96188f929f5ff
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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in payture-cheques.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alexander Sviridov
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,41 @@
1
+ # Payture::Cheques
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/payture/cheques`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'payture-cheques'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install payture-cheques
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/payture-cheques.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "payture/cheques"
5
+ require "json"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ require 'faraday'
3
+ require 'json'
4
+
5
+ require "payture/cheques/version"
6
+
7
+ require 'payture/cheques/client'
8
+ require 'payture/cheques/config'
9
+ require 'payture/cheques/methods/base'
10
+ require 'payture/cheques/methods/create'
11
+ require 'payture/cheques/methods/status'
12
+ require 'payture/cheques/responses/base'
13
+ require 'payture/cheques/responses/create'
14
+ require 'payture/cheques/responses/status'
15
+ require 'payture/cheques/responses/cheque_status'
16
+
17
+ module Payture::Cheques
18
+ class Error < StandardError
19
+ end
20
+ end
21
+
22
+ module Payture
23
+ module Cheques
24
+ def self.client(**args)
25
+ Client.new(**args)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ class Client
5
+ attr_reader :config
6
+
7
+ def initialize(**options)
8
+ @config = Config.new(**options)
9
+ end
10
+
11
+ def create(**args)
12
+ Methods::Create.new(config).call(**args)
13
+ end
14
+
15
+ def status(**args)
16
+ Methods::Status.new(config).call(**args)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ class Config
5
+ # required
6
+ attr_reader :host
7
+ attr_reader :merchant_id
8
+ attr_reader :password
9
+
10
+ # optional
11
+ attr_reader :logger
12
+ attr_reader :timeout
13
+ attr_reader :open_timeout
14
+
15
+ def initialize(**options)
16
+ @host = options[:host]
17
+ @merchant_id = options[:merchant_id]
18
+ @password = options[:password]
19
+ @logger = options[:logger]
20
+ @timeout = options[:timeout]
21
+ @open_timeout = options[:open_timeout]
22
+
23
+ check_required_fields!
24
+ end
25
+
26
+ def base_url
27
+ "https://#{host}/apicheque"
28
+ end
29
+
30
+ private
31
+
32
+ def check_required_fields!
33
+ unless host && merchant_id && password
34
+ raise ArgumentError,
35
+ 'Required options: host, merchant_id, password'
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Methods
5
+ class Base
6
+ attr_reader :config
7
+
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def call(**args)
13
+ http_response = make_request(url, params(**args))
14
+ build_response(http_response)
15
+ end
16
+
17
+ private
18
+
19
+ def url
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def params
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def response_class
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def make_request(url, params)
32
+ conn = Faraday.new do |builder|
33
+ builder.adapter Faraday.default_adapter
34
+ builder.request :url_encoded
35
+ builder.response(:logger, config.logger) if config.logger
36
+ end
37
+ conn.post(url) do |req|
38
+ req.headers['Content-Type'] = 'application/json'
39
+ req.body = compact_hash(params).to_json
40
+ config.logger.debug(req.body) if config.logger
41
+ req.options.timeout = config.timeout if config.timeout
42
+ req.options.open_timeout = config.open_timeout if config.open_timeout
43
+ end
44
+ rescue Faraday::Error => e
45
+ raise Error, "#{e.class}: #{e.message}"
46
+ end
47
+
48
+ def build_response(http_response)
49
+ unless http_response.success?
50
+ raise Error, "Invalid http status: #{http_response.status}"
51
+ end
52
+
53
+ body =
54
+ begin
55
+ JSON.parse(http_response.body)
56
+ rescue JSON::ParserError => e
57
+ raise Error, "#{e.class}: #{e.message}"
58
+ end
59
+
60
+ config.logger.debug(pp(body)) if config.logger
61
+ response_class.new(body)
62
+ end
63
+
64
+ def encoded_data(**params)
65
+ params_str = compact_hash(params).map { |k, v| "#{k}=#{v}" }.join(';')
66
+ CGI.escape(params_str)
67
+ end
68
+
69
+ def compact_hash(hash)
70
+ hash.select { |k, v| k && v }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Methods
5
+ class Create < Base
6
+ private
7
+
8
+ def url
9
+ "#{config.base_url}/Create"
10
+ end
11
+
12
+ def params(cheque_id:, inn:, content:, group: nil)
13
+ message = content.delete("Message") || content.delete(:Message)
14
+ params = {
15
+ Key: config.merchant_id,
16
+ Password: config.password,
17
+ Message: message,
18
+ Cheque: {
19
+ Id: cheque_id,
20
+ INN: inn,
21
+ Content: content,
22
+ },
23
+ }
24
+ params[:Group] = group if group
25
+ params
26
+ end
27
+
28
+ def response_class
29
+ Responses::Create
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Methods
5
+ class Status < Base
6
+ private
7
+
8
+ def url
9
+ "#{config.base_url}/Status"
10
+ end
11
+
12
+ def params(cheque_id:)
13
+ {
14
+ Key: config.merchant_id,
15
+ Password: config.password,
16
+ Id: cheque_id,
17
+ }
18
+ end
19
+
20
+ def response_class
21
+ Responses::Status
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Responses
5
+ class Base
6
+ attr_reader :body
7
+
8
+ def initialize(body)
9
+ @body = body
10
+ end
11
+
12
+ def success?
13
+ body['Success'] == 'True'
14
+ end
15
+
16
+ def error?
17
+ !success?
18
+ end
19
+
20
+ def error_code
21
+ body['ErrCode']
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ module Payture::Cheques
2
+ module Responses
3
+ class ChequeStatus
4
+ attr_reader :sent, :cheque, :error_code, :status
5
+
6
+ def initialize(args)
7
+ @sent = args["Sended"]
8
+ @cheque = args["Cheque"]
9
+ @status = args["Status"]
10
+ @error_code = args["ErrCode"] || Status::ERROR_STATES[@status]
11
+ end
12
+
13
+ def success?
14
+ @sent == true && @status == 'Created'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Responses
5
+ class Create < Base
6
+ ERROR_STATES = {
7
+ 'Conflict' => 'DUPLICATE_ID',
8
+ 'BadRequest' => 'BAD_REQUEST_FORMAT',
9
+ }.freeze
10
+
11
+ def success?
12
+ body['Success'] == true && body['Status'] == 'Accepted'
13
+ end
14
+
15
+ def error_code
16
+ body['ErrCode'] || ERROR_STATES[body['Status']]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Payture::Cheques
4
+ module Responses
5
+ class Status < Base
6
+ ERROR_STATES = {
7
+ 'Unauthorized' => 'CANNOT_AUTHORIZE_TERMINAL',
8
+ 'NotFound' => 'NOT_FOUND',
9
+ 'Timeout' => 'TIMEOUT',
10
+ 'Unknown' => 'UNKNOWN',
11
+ }.freeze
12
+
13
+ def cheques
14
+ @cheques ||= body['Cheques'].map { |c| ChequeStatus.new(c) }
15
+ end
16
+
17
+ def success?
18
+ body['Success'] == true
19
+ end
20
+
21
+ def error_code
22
+ body['ErrCode'] || ERROR_STATES[body['Status']]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Payture
2
+ module Cheques
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'payture/cheques/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "payture-cheques"
8
+ spec.version = Payture::Cheques::VERSION
9
+ spec.authors = ["Alexander Sviridov"]
10
+ spec.email = ["alexander.sviridov@gmail.com"]
11
+
12
+ spec.summary = %q{Payture payment system API for sendind cheques separately from payments}
13
+ spec.homepage = 'https://github.com/busfor/payture-cheques'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'faraday', '~> 0.9'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.14'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'pry', '~> 0.10'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.51'
30
+ spec.add_development_dependency 'simplecov', '~> 0.14'
31
+ spec.add_development_dependency 'vcr', '~> 2.9'
32
+ spec.add_development_dependency 'webmock', '~> 3.0'
33
+ spec.add_development_dependency 'pp'
34
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payture-cheques
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Sviridov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
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.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.51'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.51'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.14'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pp
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
+ description:
154
+ email:
155
+ - alexander.sviridov@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".travis.yml"
162
+ - Gemfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - Rakefile
166
+ - bin/console
167
+ - bin/setup
168
+ - lib/payture/cheques.rb
169
+ - lib/payture/cheques/client.rb
170
+ - lib/payture/cheques/config.rb
171
+ - lib/payture/cheques/methods/base.rb
172
+ - lib/payture/cheques/methods/create.rb
173
+ - lib/payture/cheques/methods/status.rb
174
+ - lib/payture/cheques/responses/base.rb
175
+ - lib/payture/cheques/responses/cheque_status.rb
176
+ - lib/payture/cheques/responses/create.rb
177
+ - lib/payture/cheques/responses/status.rb
178
+ - lib/payture/cheques/version.rb
179
+ - payture-cheques.gemspec
180
+ homepage: https://github.com/busfor/payture-cheques
181
+ licenses:
182
+ - MIT
183
+ metadata: {}
184
+ post_install_message:
185
+ rdoc_options: []
186
+ require_paths:
187
+ - lib
188
+ required_ruby_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ required_rubygems_version: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ requirements: []
199
+ rubyforge_project:
200
+ rubygems_version: 2.5.2
201
+ signing_key:
202
+ specification_version: 4
203
+ summary: Payture payment system API for sendind cheques separately from payments
204
+ test_files: []