rubyqrpay 0.2.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: 277d8b13ede58783d2c30a11f667f4efdd2161b3
4
+ data.tar.gz: 3be8ea0e0f297ee0fbb3928da5ce38f1a9423925
5
+ SHA512:
6
+ metadata.gz: 938ec061d9c373e501718b1bc1b61d87584d51ad530a22c289d7d322f2f6ac892083c59501046ab7a4a603445939136a835f64bdc7f03078276abf3187fac50d
7
+ data.tar.gz: ad454af86daee739adbea1791a85c62fafd6f321b2f8827fd27281879de10e72fb6170d28136177f01bc1ce1cf0a678a234b4f608193fef28f7ab38d9ebd75e4
data/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ /.rspec
13
+ /.rspec_status
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ # .env
17
+
18
+ ## Specific to RubyMotion:
19
+ .dat*
20
+ .repl_history
21
+ build/
22
+ *.bridgesupport
23
+ build-iPhoneOS/
24
+ build-iPhoneSimulator/
25
+
26
+ ## Specific to RubyMotion (use of CocoaPods):
27
+ #
28
+ # We recommend against adding the Pods directory to your .gitignore. However
29
+ # you should judge for yourself, the pros and cons are mentioned at:
30
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
31
+ #
32
+ # vendor/Pods/
33
+
34
+ ## Documentation cache and generated files:
35
+ /.yardoc/
36
+ /_yardoc/
37
+ /doc/
38
+ /rdoc/
39
+
40
+ ## Environment normalization:
41
+ /.bundle/
42
+ /vendor/bundle
43
+ /lib/bundler/man/
44
+
45
+ # for a library or gem, you might want to ignore these files since the code is
46
+ # intended to run in multiple environments; otherwise, check them in:
47
+ # Gemfile.lock
48
+ # .ruby-version
49
+ # .ruby-gemset
50
+
51
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
52
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at gudel98@github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rubyqrpay.gemspec
6
+ gemspec
7
+ gem 'pry'
8
+ gem 'digest-crc'
9
+ gem 'dry-validation'
10
+ gem 'money'
11
+ gem 'countries'
12
+ gem 'iso639'
13
+ gem 'rqrcode'
data/Gemfile.lock ADDED
@@ -0,0 +1,93 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubyqrpay (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ chunky_png (1.3.10)
10
+ coderay (1.1.2)
11
+ concurrent-ruby (1.0.5)
12
+ countries (2.1.4)
13
+ i18n_data (~> 0.8.0)
14
+ money (~> 6.9)
15
+ sixarm_ruby_unaccent (~> 1.1)
16
+ unicode_utils (~> 1.4)
17
+ diff-lcs (1.3)
18
+ digest-crc (0.4.1)
19
+ dry-configurable (0.7.0)
20
+ concurrent-ruby (~> 1.0)
21
+ dry-container (0.6.0)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-configurable (~> 0.1, >= 0.1.3)
24
+ dry-core (0.4.5)
25
+ concurrent-ruby (~> 1.0)
26
+ dry-equalizer (0.2.0)
27
+ dry-logic (0.4.2)
28
+ dry-container (~> 0.2, >= 0.2.6)
29
+ dry-core (~> 0.2)
30
+ dry-equalizer (~> 0.2)
31
+ dry-types (0.12.2)
32
+ concurrent-ruby (~> 1.0)
33
+ dry-configurable (~> 0.1)
34
+ dry-container (~> 0.3)
35
+ dry-core (~> 0.2, >= 0.2.1)
36
+ dry-equalizer (~> 0.2)
37
+ dry-logic (~> 0.4, >= 0.4.2)
38
+ inflecto (~> 0.0.0, >= 0.0.2)
39
+ dry-validation (0.11.1)
40
+ concurrent-ruby (~> 1.0)
41
+ dry-configurable (~> 0.1, >= 0.1.3)
42
+ dry-core (~> 0.2, >= 0.2.1)
43
+ dry-equalizer (~> 0.2)
44
+ dry-logic (~> 0.4, >= 0.4.0)
45
+ dry-types (~> 0.12.0)
46
+ i18n (0.9.5)
47
+ concurrent-ruby (~> 1.0)
48
+ i18n_data (0.8.0)
49
+ inflecto (0.0.2)
50
+ iso639 (1.3.2)
51
+ method_source (0.9.0)
52
+ money (6.10.1)
53
+ i18n (>= 0.6.4, < 1.0)
54
+ pry (0.11.3)
55
+ coderay (~> 1.1.0)
56
+ method_source (~> 0.9.0)
57
+ rake (10.5.0)
58
+ rqrcode (0.10.1)
59
+ chunky_png (~> 1.0)
60
+ rspec (3.7.0)
61
+ rspec-core (~> 3.7.0)
62
+ rspec-expectations (~> 3.7.0)
63
+ rspec-mocks (~> 3.7.0)
64
+ rspec-core (3.7.1)
65
+ rspec-support (~> 3.7.0)
66
+ rspec-expectations (3.7.0)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.7.0)
69
+ rspec-mocks (3.7.0)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.7.0)
72
+ rspec-support (3.7.1)
73
+ sixarm_ruby_unaccent (1.2.0)
74
+ unicode_utils (1.4.0)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ bundler (~> 1.16)
81
+ countries
82
+ digest-crc
83
+ dry-validation
84
+ iso639
85
+ money
86
+ pry
87
+ rake (~> 10.0)
88
+ rqrcode
89
+ rspec (~> 3.0)
90
+ rubyqrpay!
91
+
92
+ BUNDLED WITH
93
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Egor Shpalov
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,72 @@
1
+ # rubyQRpay
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'rubyqrpay'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubyqrpay
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ transaction_information = {
23
+ agregator_id: 'rubyqrpay',
24
+ merchant_account_32: {
25
+ service_code_erip: '111111',
26
+ payer_unique_id: '12345678',
27
+ payer_number: '--',
28
+ amount_edit_possibility: true,
29
+ },
30
+ merchant_account_33: {
31
+ service_producer_code: '123',
32
+ service_code: '--',
33
+ outlet: '--',
34
+ order_code: '--'
35
+ },
36
+ merchant_category_code: 2934,
37
+ currency: 933,
38
+ amount: 10.05,
39
+ convenience_indicator: 1,
40
+ fixed_fee: 0.01,
41
+ percentage_fee: 12.0,
42
+ country: 'BY',
43
+ merchant_name: 'Egor',
44
+ merchant_city: 'Minsk',
45
+ postal_code: '222310',
46
+ additional_data: {
47
+ bill_number: '--',
48
+ mobile_number: '--',
49
+ store_label: '--',
50
+ loyalty_number: '***',
51
+ reference_label: '***',
52
+ customer_label: '--',
53
+ terminal_label: '--',
54
+ purpose_of_transaction: '***',
55
+ consumer_data_request: 'AME'
56
+ },
57
+ merchant_information_language: {
58
+ language_reference: 'ru',
59
+ name_alternate: 'Егор',
60
+ city_alternate: 'Минск'
61
+ }
62
+ }
63
+
64
+ url = 'https://pay.raschet.by#'
65
+
66
+ payload = Rubyqrpay::Generator.generate_payload(transaction_information)
67
+ base64 = Rubyqrpay::Generator.generate_png(url, payload)
68
+ ```
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubyqrpay"
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 "irb"
14
+ 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
data/lib/rubyqrpay.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'rubyqrpay/generator'
@@ -0,0 +1,61 @@
1
+ module Rubyqrpay
2
+ class Generator
3
+ ID_PAYLOAD_FORMAT = '00'
4
+ ID_POI_METHOD = '01'
5
+ ID_MERCHANT_INFORMATION_32 = '32'
6
+ ID_MERCHANT_INFORMATION_33 = '33'
7
+ ID_MERCHANT_CATEGORY_CODE = '52'
8
+ ID_TRANSACTION_CURRENCY = '53'
9
+ ID_TRANSACTION_AMOUNT = '54'
10
+ ID_TIP_OF_CONVENIENCE_INDICATOR = '55'
11
+ ID_VALUE_OF_CONVENIENCE_FEE_FIXED = '56'
12
+ ID_VALUE_OF_CONVENIENCE_FEE_PERCENTAGE = '57'
13
+ ID_COUNTRY = '58'
14
+ ID_MERCHANT_NAME = '59'
15
+ ID_MERCHANT_CITY = '60'
16
+ ID_POSTAL_CODE = '61'
17
+ ID_ADDITIONAL_DATA_FIELD = '62'
18
+ ID_MERCHANT_INFORMATION_LANGUAGE = '64'
19
+ ID_CRC = '63'
20
+
21
+ PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE = '01'
22
+ POI_METHOD_STATIC = '11'
23
+ POI_METHOD_DYNAMIC = '12'
24
+ CONVENIENCE_INDICATOR_FIXED = '02'
25
+ CONVENIENCE_INDICATOR_PERCENTAGE = '03'
26
+ MERCHANT_INFORMATION_TEMPLATE_ID_GUID = '00'
27
+ # ID_MERCHANT_INFORMATION_32
28
+ ID_SERVICE_CODE_ERIP = '01'
29
+ ID_PAYER_UNIQUE = '10'
30
+ ID_PAYER_NUMBER = '11'
31
+ ID_AMOUNT_EDIT_POSSIBILITY = '12'
32
+ # ID_MERCHANT_INFORMATION_33
33
+ ID_SERVICE_PRODUCER_CODE = '03'
34
+ ID_SERVICE_CODE = '04'
35
+ ID_OUTLET = '05'
36
+ ID_ORDER_CODE = '06'
37
+ # ID_ADDITIONAL_DATA_FIELD
38
+ ID_BILL_NUMBER = '01'
39
+ ID_MOBILE_NUMBER = '02'
40
+ ID_STORE_LABEL = '03'
41
+ ID_LOYALTY_NUMBER = '04'
42
+ ID_REFERENCE_LABEL = '05'
43
+ ID_CUSTOMER_LABEL = '06'
44
+ ID_TERMINAL_LABEL = '07'
45
+ ID_PURPOSE_OF_TRANSACTION = '08'
46
+ ID_CONSUMER_DATA_REQUEST = '09'
47
+ # ID_MERCHANT_INFORMATION_LANGUAGE
48
+ ID_LANGUAGE_REFERENCE = '00'
49
+ ID_MERCHANT_NAME_ALTERNATE = '01'
50
+ ID_MERCHANT_CITY_ALTERNATE = '02'
51
+
52
+ GUID_PROMPTPAY_32 = 'by.raschet'
53
+ GUID_PROMPTPAY_33 = 'by.epos.'
54
+ CRC_SYMBOL_SIZE = '04'
55
+ AEP_DEFAULT = '11'
56
+ AEP_FALSE = '12'
57
+
58
+ K_SIZE_FUNC = 0.15
59
+ B_SIZE_FUNC = 140
60
+ end
61
+ end
@@ -0,0 +1,165 @@
1
+ require 'rubyqrpay/version'
2
+ require_relative 'validator'
3
+ require_relative 'constants'
4
+ require 'rqrcode'
5
+ require 'digest/crc16_ccitt'
6
+ require 'base64'
7
+ require 'uri'
8
+
9
+ module Rubyqrpay
10
+ class Generator
11
+ def self.generate_payload(opts)
12
+ opts = Rubyqrpay::Validator.validate_payload(opts)
13
+ unless opts.nil?
14
+ payload = generation(opts)
15
+ percent_encode payload
16
+ end
17
+ end
18
+
19
+ def self.generate_png(url, payload, **opts)
20
+ opts_default = {size: size_func(url, payload).to_i, level: :l}
21
+ qrcode = RQRCode::QRCode.new("#{url}#{payload}", level: opts[:level] || opts_default[:level])
22
+ png = qrcode.as_png(resize_gte_to: false,
23
+ resize_exactly_to: false,
24
+ fill: 'white',
25
+ color: 'black',
26
+ size: opts[:size] || opts_default[:size],
27
+ border_modules: 4,
28
+ module_px_size: 6)
29
+ qrcode_to_base64(png)
30
+ end
31
+
32
+ private
33
+
34
+ def self.size_func(url, payload)
35
+ x = "#{url}#{payload}".size
36
+ K_SIZE_FUNC * x + B_SIZE_FUNC # linear function (kx + b)
37
+ end
38
+
39
+ def self.qrcode_to_base64(png)
40
+ base64 = Base64.encode64(png.to_s)
41
+ base64.split("\n").join
42
+ end
43
+
44
+ def self.generation(opts)
45
+ payload_data = {
46
+ ID_PAYLOAD_FORMAT => PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE,
47
+ ID_POI_METHOD => opts[:amount] ? POI_METHOD_DYNAMIC : POI_METHOD_STATIC,
48
+ ID_MERCHANT_INFORMATION_32 => merchant_account_32_data(opts[:merchant_account_32]),
49
+ ID_MERCHANT_INFORMATION_33 => merchant_account_33_data(opts[:agregator_id], opts[:merchant_account_33]),
50
+ ID_MERCHANT_CATEGORY_CODE => opts[:merchant_category_code],
51
+ ID_TRANSACTION_CURRENCY => opts[:currency],
52
+ ID_TRANSACTION_AMOUNT => format_amount(opts[:amount]),
53
+ ID_TIP_OF_CONVENIENCE_INDICATOR => opts[:convenience_indicator],
54
+ ID_COUNTRY => opts[:country],
55
+ ID_MERCHANT_NAME => opts[:merchant_name],
56
+ ID_MERCHANT_CITY => opts[:merchant_city],
57
+ ID_POSTAL_CODE => opts[:postal_code],
58
+ ID_ADDITIONAL_DATA_FIELD => additional_data_field(opts[:additional_data]),
59
+ ID_MERCHANT_INFORMATION_LANGUAGE => merchant_language_data(opts[:merchant_information_language])
60
+ }
61
+ payload_data = convenience_indicator_case(payload_data, opts)
62
+ payload = join_hash(payload_data)
63
+ payload += crc(payload)
64
+ end
65
+
66
+ def self.merchant_account_32_data(merchant_account)
67
+ merchant_account = {
68
+ MERCHANT_INFORMATION_TEMPLATE_ID_GUID => GUID_PROMPTPAY_32,
69
+ ID_SERVICE_CODE_ERIP => merchant_account[:service_code_erip],
70
+ ID_PAYER_UNIQUE => merchant_account[:payer_unique_id],
71
+ ID_PAYER_NUMBER => merchant_account[:payer_number],
72
+ ID_AMOUNT_EDIT_POSSIBILITY => aep_convert(merchant_account[:amount_edit_possibility])
73
+ }
74
+ join_hash(merchant_account)
75
+ end
76
+
77
+ def self.aep_convert(aep)
78
+ if aep || aep.nil?
79
+ AEP_DEFAULT
80
+ else
81
+ AEP_FALSE
82
+ end
83
+ end
84
+
85
+ def self.merchant_account_33_data(agregator_id, merchant_account)
86
+ unless agregator_id.nil?
87
+ merchant_account ||= Hash.new
88
+ merchant_account = {
89
+ MERCHANT_INFORMATION_TEMPLATE_ID_GUID => "#{GUID_PROMPTPAY_33}#{agregator_id}",
90
+ ID_SERVICE_PRODUCER_CODE => merchant_account[:service_producer_code],
91
+ ID_SERVICE_CODE => merchant_account[:service_code],
92
+ ID_OUTLET => merchant_account[:outlet],
93
+ ID_ORDER_CODE => merchant_account[:order_code]
94
+ }
95
+ join_hash(merchant_account)
96
+ end
97
+ end
98
+
99
+ def self.additional_data_field(additional_data)
100
+ additional_data ||= Hash.new
101
+ additional_data = {
102
+ ID_BILL_NUMBER => additional_data[:bill_number],
103
+ ID_MOBILE_NUMBER => additional_data[:mobile_number],
104
+ ID_STORE_LABEL => additional_data[:store_label],
105
+ ID_LOYALTY_NUMBER => additional_data[:loyalty_number],
106
+ ID_REFERENCE_LABEL => additional_data[:reference_label],
107
+ ID_CUSTOMER_LABEL => additional_data[:customer_label],
108
+ ID_TERMINAL_LABEL => additional_data[:terminal_label],
109
+ ID_PURPOSE_OF_TRANSACTION => additional_data[:purpose_of_transaction],
110
+ ID_CONSUMER_DATA_REQUEST => additional_data[:consumer_data_request]
111
+ }
112
+ join_hash(additional_data)
113
+ end
114
+
115
+ def self.merchant_language_data(merchant_information_language)
116
+ merchant_information_language ||= Hash.new
117
+ merchant_information_language = {
118
+ ID_LANGUAGE_REFERENCE => merchant_information_language[:language_reference],
119
+ ID_MERCHANT_NAME_ALTERNATE => merchant_information_language[:name_alternate],
120
+ ID_MERCHANT_CITY_ALTERNATE => merchant_information_language[:city_alternate]
121
+ }
122
+ join_hash(merchant_information_language)
123
+ end
124
+
125
+ def self.convenience_indicator_case(payload_data, opts)
126
+ payload_data.tap do |data|
127
+ case format_indicator(opts[:convenience_indicator])
128
+ when CONVENIENCE_INDICATOR_FIXED
129
+ data[ID_VALUE_OF_CONVENIENCE_FEE_FIXED] = format_amount(opts[:fixed_fee])
130
+ when CONVENIENCE_INDICATOR_PERCENTAGE
131
+ data[ID_VALUE_OF_CONVENIENCE_FEE_PERCENTAGE] = format_amount(opts[:percentage_fee])
132
+ end
133
+ end
134
+ end
135
+
136
+ def self.join_hash(hash)
137
+ hash.map do |id, value|
138
+ value = value.to_s
139
+ unless value.empty?
140
+ len = "00#{value.size}".slice(-2..-1)
141
+ id + len + value
142
+ end
143
+ end.join
144
+ end
145
+
146
+ def self.format_amount(amount)
147
+ "%.2f" % amount.to_f
148
+ end
149
+
150
+ def self.format_indicator(convenience_indicator)
151
+ "0#{convenience_indicator}"
152
+ end
153
+
154
+ def self.percent_encode(str)
155
+ URI.escape(str)
156
+ end
157
+
158
+ def self.crc(data)
159
+ data += ID_CRC + CRC_SYMBOL_SIZE
160
+ x = Digest::CRC16CCITT.new
161
+ x.update(data)
162
+ ID_CRC + CRC_SYMBOL_SIZE + x.hexdigest.upcase
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,126 @@
1
+ require 'dry-validation'
2
+ require 'money'
3
+ require 'countries'
4
+ require 'iso639'
5
+
6
+ module Rubyqrpay
7
+ class Validator
8
+ CONSUMER_DATA_REQUEST_PATTERN = /^(A?E?M|E?M?A|M?A?E|A?M?E|E?M?A|M?E?A)$/
9
+ ANS_PATTERN = /^[\w\s.:\/?#\[\]@!$&'()*+,;=\-~]*$/
10
+ MIN_MCC = 1_000
11
+ MAX_MCC = 10_000
12
+ MIN_FIXED = 0.01
13
+ MAX_FIXED = 9_999_999_999.99
14
+ MIN_PERCENT = 0.01
15
+ MAX_PERCENT = 99.99
16
+
17
+ def self.validate_payload(opts)
18
+ schema = Dry::Validation.Schema do
19
+ configure do
20
+ def self.messages
21
+ super.merge(
22
+ en: { errors: { valid_currency: 'currency is not valid',
23
+ country_code?: 'country is not valid',
24
+ valid_language_reference: 'language_reference is not valid',
25
+ agregator_id_presence: 'agregator must be filled',
26
+ valid_merchant_account_32: 'field is too long',
27
+ valid_merchant_account_33: 'field is too long',
28
+ valid_additional_data: 'field is too long'} }
29
+ )
30
+ end
31
+
32
+ def country_code?(value)
33
+ ISO3166::Country.find_country_by_alpha2(value)
34
+ end
35
+ end
36
+
37
+ validate(valid_currency: :currency) do |currency|
38
+ Money::Currency.find_by_iso_numeric(currency)
39
+ end
40
+
41
+ rule(valid_country: [:country]) do |country|
42
+ country.filled?.then(country.country_code?)
43
+ end
44
+
45
+ rule(fixed_fee: [:convenience_indicator, :fixed_fee]) do |indicator, fixed|
46
+ indicator.eql?(2).then(fixed.filled?)
47
+ end
48
+
49
+ rule(percentage_fee: [:convenience_indicator, :percentage_fee]) do |indicator, percentage|
50
+ indicator.eql?(3).then(percentage.filled?)
51
+ end
52
+
53
+ rule(agregator_id_presence: [:agregator_id, :merchant_account_33]) do |agregator_id, merchant_account_33|
54
+ merchant_account_33.filled?.then(agregator_id.filled?)
55
+ end
56
+
57
+ optional(:agregator_id).maybe(:str?, format?: ANS_PATTERN)
58
+ required(:merchant_account_32).schema do
59
+ required(:service_code_erip).filled(:str?)
60
+ optional(:payer_unique_id).maybe(:str?)
61
+ optional(:payer_number).maybe(:str?)
62
+ optional(:amount_edit_possibility).maybe(:bool?)
63
+ end
64
+
65
+ optional(:merchant_account_33).schema do
66
+ required(:service_producer_code).filled(:str?)
67
+ optional(:service_code).maybe(:str?)
68
+ optional(:outlet).maybe(:str?)
69
+ optional(:order_code).maybe(:str?)
70
+ end
71
+
72
+ validate(valid_merchant_account_32: [:merchant_account_32]) do |merchant_account_32|
73
+ (0..99).include? merchant_account_32.to_h.values.join.size
74
+ end
75
+
76
+ validate(valid_merchant_account_33: [:merchant_account_33]) do |merchant_account_33|
77
+ (0..99).include? merchant_account_33.to_h.values.join.size
78
+ end
79
+
80
+ validate(valid_additional_data: [:additional_data]) do |additional_data|
81
+ (0..99).include? additional_data.to_h.values.join.size
82
+ end
83
+
84
+ required(:currency).filled(:int?)
85
+ optional(:convenience_indicator).maybe(:int?, gteq?: 1, lteq?: 3)
86
+ optional(:fixed_fee).maybe(:float?, gteq?: MIN_FIXED, lteq?: MAX_FIXED)
87
+ optional(:percentage_fee).maybe(:float?, gteq?: MIN_PERCENT, lteq?: MAX_PERCENT)
88
+ optional(:merchant_category_code).maybe(:int?, gteq?: MIN_MCC, lt?: MAX_MCC)
89
+ optional(:amount).maybe(:float?, gteq?: MIN_FIXED, lteq?: MAX_FIXED)
90
+ optional(:country).maybe(:str?)
91
+ optional(:merchant_name).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
92
+ optional(:merchant_city).maybe(:str?, max_size?: 15, format?: ANS_PATTERN)
93
+ optional(:postal_code).maybe(:str?, max_size?: 10, format?: ANS_PATTERN)
94
+
95
+ optional(:additional_data).schema do
96
+ optional(:bill_number).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
97
+ optional(:mobile_number).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
98
+ optional(:store_label).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
99
+ optional(:loyalty_number).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
100
+ optional(:reference_label).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
101
+ optional(:customer_label).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
102
+ optional(:terminal_label).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
103
+ optional(:purpose_of_transaction).maybe(:str?, max_size?: 25, format?: ANS_PATTERN)
104
+ optional(:consumer_data_request).maybe(format?: CONSUMER_DATA_REQUEST_PATTERN)
105
+ end
106
+
107
+ optional(:merchant_information_language).schema do
108
+ validate(valid_language_reference: :language_reference) do |language_reference|
109
+ Iso639[language_reference]
110
+ end
111
+
112
+ required(:language_reference).filled(:str?, max_size?: 2)
113
+ required(:name_alternate).filled(:str?, max_size?: 25)
114
+ optional(:city_alternate).maybe(:str?, max_size?: 15)
115
+ end
116
+ end
117
+
118
+ result = schema.call(opts)
119
+ if result.success?
120
+ opts
121
+ else
122
+ raise ArgumentError, result.errors.first.to_s
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module Rubyqrpay
2
+ VERSION = "0.2.1"
3
+ end
data/rubyqrpay.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rubyqrpay/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubyqrpay"
8
+ spec.version = Rubyqrpay::VERSION
9
+ spec.authors = ["Egor Shpalov"]
10
+ spec.email = ["egorshpalov1998@gmail.com"]
11
+ spec.summary = "Ruby prompt pay QR-code generator"
12
+ spec.homepage = 'https://github.com/gudel98/rubyqrpay'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_runtime_dependency "digest-crc", ["~> 0.4"]
26
+ spec.add_runtime_dependency "dry-validation", ["~> 0.11"]
27
+ spec.add_runtime_dependency "money", ["~> 6.10"]
28
+ spec.add_runtime_dependency "countries", ["~> 2.1"]
29
+ spec.add_runtime_dependency "iso639", ["~> 1.3"]
30
+ spec.add_runtime_dependency "rqrcode", ["~> 0.10"]
31
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyqrpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Egor Shpalov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-03 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: digest-crc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dry-validation
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: money
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '6.10'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '6.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: countries
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: iso639
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.3'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rqrcode
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.10'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.10'
139
+ description:
140
+ email:
141
+ - egorshpalov1998@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rspec_status"
149
+ - ".travis.yml"
150
+ - CODE_OF_CONDUCT.md
151
+ - Gemfile
152
+ - Gemfile.lock
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/rubyqrpay.rb
159
+ - lib/rubyqrpay/constants.rb
160
+ - lib/rubyqrpay/generator.rb
161
+ - lib/rubyqrpay/validator.rb
162
+ - lib/rubyqrpay/version.rb
163
+ - rubyqrpay.gemspec
164
+ homepage: https://github.com/gudel98/rubyqrpay
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.6.14
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Ruby prompt pay QR-code generator
188
+ test_files: []