webtopay 1.2.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +145 -0
  4. data/lib/webtopay.rb +5 -3
  5. data/lib/webtopay/exception.rb +11 -1
  6. data/lib/webtopay/payment.rb +45 -0
  7. data/lib/webtopay/response.rb +198 -0
  8. data/lib/webtopay/version.rb +1 -1
  9. data/lib/webtopay_controller.rb +14 -19
  10. data/lib/webtopay_helper.rb +19 -15
  11. data/spec/dummy/README.rdoc +28 -0
  12. data/spec/dummy/Rakefile +6 -0
  13. data/spec/dummy/app/assets/images/.keep +0 -0
  14. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  15. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  17. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  18. data/spec/dummy/app/controllers/payments_controller.rb +11 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/mailers/.keep +0 -0
  21. data/spec/dummy/app/models/.keep +0 -0
  22. data/spec/dummy/app/models/concerns/.keep +0 -0
  23. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/bin/bundle +3 -0
  25. data/spec/dummy/bin/rails +4 -0
  26. data/spec/dummy/bin/rake +4 -0
  27. data/spec/dummy/config.ru +4 -0
  28. data/spec/dummy/config/application.rb +23 -0
  29. data/spec/dummy/config/boot.rb +5 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +29 -0
  33. data/spec/dummy/config/environments/production.rb +80 -0
  34. data/spec/dummy/config/environments/test.rb +36 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  37. data/spec/dummy/config/initializers/inflections.rb +16 -0
  38. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  39. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  40. data/spec/dummy/config/initializers/session_store.rb +3 -0
  41. data/spec/dummy/config/initializers/webtopay.rb +4 -0
  42. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/spec/dummy/config/locales/en.yml +23 -0
  44. data/spec/dummy/config/routes.rb +61 -0
  45. data/spec/dummy/db/test.sqlite3 +0 -0
  46. data/spec/dummy/lib/assets/.keep +0 -0
  47. data/spec/dummy/log/.keep +0 -0
  48. data/spec/dummy/public/404.html +58 -0
  49. data/spec/dummy/public/422.html +58 -0
  50. data/spec/dummy/public/500.html +57 -0
  51. data/spec/dummy/public/favicon.ico +0 -0
  52. data/spec/lib/webtopay/payment_spec.rb +31 -0
  53. data/spec/lib/webtopay/response_spec.rb +57 -0
  54. data/spec/lib/webtopay_controller_spec.rb +19 -0
  55. data/spec/spec_helper.rb +59 -0
  56. data/webtopay.gemspec +10 -3
  57. metadata +195 -47
  58. data/README +0 -71
  59. data/lib/webtopay/api.rb +0 -407
@@ -0,0 +1,59 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
4
+
5
+ require 'rspec/rails'
6
+ require 'factory_girl_rails'
7
+ require 'database_cleaner'
8
+
9
+ ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
10
+
11
+ # Requires supporting ruby files with custom matchers and macros, etc,
12
+ # in spec/support/ and its subdirectories.
13
+ Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
14
+
15
+ RSpec.configure do |config|
16
+ config.include FactoryGirl::Syntax::Methods
17
+
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+
20
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
21
+ config.fixture_path = "#{::Rails.root}/spec/fixtures/"
22
+
23
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
24
+ # examples within a transaction, remove the following line or assign false
25
+ # instead of true.
26
+ config.use_transactional_fixtures = false
27
+
28
+ # If true, the base class of anonymous controllers will be inferred
29
+ # automatically. This will be the default behavior in future versions of
30
+ # rspec-rails.
31
+ config.infer_base_class_for_anonymous_controllers = true
32
+
33
+ # Run specs in random order to surface order dependencies. If you find an
34
+ # order dependency and want to debug it, you can fix the order by providing
35
+ # the seed, which is printed after each run.
36
+ # --seed 1234
37
+ config.order = "random"
38
+
39
+ config.before :suite do
40
+ DatabaseCleaner.strategy = :transaction
41
+ DatabaseCleaner.clean_with :truncation
42
+ end
43
+ #
44
+ #config.before js: true do
45
+ # DatabaseCleaner.strategy = :truncation
46
+ #end
47
+ #
48
+ #config.after js: true do
49
+ # DatabaseCleaner.strategy = :transaction
50
+ #end
51
+ #
52
+ config.before do
53
+ DatabaseCleaner.start
54
+ end
55
+ #
56
+ config.after do
57
+ DatabaseCleaner.clean
58
+ end
59
+ end
@@ -6,9 +6,9 @@ Gem::Specification.new do |s|
6
6
  s.name = "webtopay"
7
7
  s.version = Webtopay::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Laurynas Butkus", "Kristijonas Urbaitis"]
10
- s.email = ["laurynas.butkus@gmail.com", "kristis@micro.lt"]
11
- s.homepage = "https://github.com/laurynas/webtopay"
9
+ s.authors = ["Povilas Jurčys", "Laurynas Butkus", "Kristijonas Urbaitis"]
10
+ s.email = ["bloomrain@gmail.com", "laurynas.butkus@gmail.com", "kristis@micro.lt"]
11
+ s.homepage = "https://github.com/bloomrain/webtopay"
12
12
  s.summary = %q{Provides integration with http://www.webtopay.com (mokejimai.lt) payment system}
13
13
  s.description = %q{Verifies webtopay.com (mokejimai.lt) payment data transfer}
14
14
 
@@ -18,5 +18,12 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "sqlite3", '~> 1.3', '>= 1.3.8'
23
+ s.add_development_dependency "rails", '~> 4.0', '>= 4.0.0'
24
+ s.add_development_dependency 'factory_girl_rails', '~> 4.3', '>= 4.3.0'
25
+ s.add_development_dependency 'database_cleaner', '~> 1.2', '>= 1.2.0'
26
+ s.add_development_dependency "rspec", '~> 2.14', '>= 2.14.1'
27
+ s.add_development_dependency "rspec-rails", '~> 2.14', '>= 2.14.1'
21
28
  end
22
29
 
metadata CHANGED
@@ -1,81 +1,229 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: webtopay
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 1
10
- version: 1.2.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
7
+ - Povilas Jurčys
13
8
  - Laurynas Butkus
14
9
  - Kristijonas Urbaitis
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-04-26 00:00:00 Z
20
- dependencies: []
21
-
13
+ date: 2014-03-05 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sqlite3
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.3.8
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - "~>"
30
+ - !ruby/object:Gem::Version
31
+ version: '1.3'
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.3.8
35
+ - !ruby/object:Gem::Dependency
36
+ name: rails
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '4.0'
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 4.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - "~>"
50
+ - !ruby/object:Gem::Version
51
+ version: '4.0'
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_girl_rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.3'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 4.3.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '4.3'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.3.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: database_cleaner
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.2'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.2.0
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.2'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.2.0
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '2.14'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.14.1
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '2.14'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 2.14.1
115
+ - !ruby/object:Gem::Dependency
116
+ name: rspec-rails
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '2.14'
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.14.1
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.14'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 2.14.1
22
135
  description: Verifies webtopay.com (mokejimai.lt) payment data transfer
23
- email:
136
+ email:
137
+ - bloomrain@gmail.com
24
138
  - laurynas.butkus@gmail.com
25
139
  - kristis@micro.lt
26
140
  executables: []
27
-
28
141
  extensions: []
29
-
30
142
  extra_rdoc_files: []
31
-
32
- files:
33
- - .gitignore
143
+ files:
144
+ - ".gitignore"
34
145
  - Gemfile
35
146
  - MIT-LICENSE
36
- - README
147
+ - README.md
37
148
  - Rakefile
38
149
  - init.rb
39
150
  - lib/webtopay.rb
40
- - lib/webtopay/api.rb
41
151
  - lib/webtopay/configuration.rb
42
152
  - lib/webtopay/exception.rb
153
+ - lib/webtopay/payment.rb
154
+ - lib/webtopay/response.rb
43
155
  - lib/webtopay/version.rb
44
156
  - lib/webtopay_controller.rb
45
157
  - lib/webtopay_helper.rb
158
+ - spec/dummy/README.rdoc
159
+ - spec/dummy/Rakefile
160
+ - spec/dummy/app/assets/images/.keep
161
+ - spec/dummy/app/assets/javascripts/application.js
162
+ - spec/dummy/app/assets/stylesheets/application.css
163
+ - spec/dummy/app/controllers/application_controller.rb
164
+ - spec/dummy/app/controllers/concerns/.keep
165
+ - spec/dummy/app/controllers/payments_controller.rb
166
+ - spec/dummy/app/helpers/application_helper.rb
167
+ - spec/dummy/app/mailers/.keep
168
+ - spec/dummy/app/models/.keep
169
+ - spec/dummy/app/models/concerns/.keep
170
+ - spec/dummy/app/views/layouts/application.html.erb
171
+ - spec/dummy/bin/bundle
172
+ - spec/dummy/bin/rails
173
+ - spec/dummy/bin/rake
174
+ - spec/dummy/config.ru
175
+ - spec/dummy/config/application.rb
176
+ - spec/dummy/config/boot.rb
177
+ - spec/dummy/config/database.yml
178
+ - spec/dummy/config/environment.rb
179
+ - spec/dummy/config/environments/development.rb
180
+ - spec/dummy/config/environments/production.rb
181
+ - spec/dummy/config/environments/test.rb
182
+ - spec/dummy/config/initializers/backtrace_silencers.rb
183
+ - spec/dummy/config/initializers/filter_parameter_logging.rb
184
+ - spec/dummy/config/initializers/inflections.rb
185
+ - spec/dummy/config/initializers/mime_types.rb
186
+ - spec/dummy/config/initializers/secret_token.rb
187
+ - spec/dummy/config/initializers/session_store.rb
188
+ - spec/dummy/config/initializers/webtopay.rb
189
+ - spec/dummy/config/initializers/wrap_parameters.rb
190
+ - spec/dummy/config/locales/en.yml
191
+ - spec/dummy/config/routes.rb
192
+ - spec/dummy/db/test.sqlite3
193
+ - spec/dummy/lib/assets/.keep
194
+ - spec/dummy/log/.keep
195
+ - spec/dummy/public/404.html
196
+ - spec/dummy/public/422.html
197
+ - spec/dummy/public/500.html
198
+ - spec/dummy/public/favicon.ico
199
+ - spec/lib/webtopay/payment_spec.rb
200
+ - spec/lib/webtopay/response_spec.rb
201
+ - spec/lib/webtopay_controller_spec.rb
202
+ - spec/spec_helper.rb
46
203
  - webtopay.gemspec
47
- homepage: https://github.com/laurynas/webtopay
204
+ homepage: https://github.com/bloomrain/webtopay
48
205
  licenses: []
49
-
206
+ metadata: {}
50
207
  post_install_message:
51
208
  rdoc_options: []
52
-
53
- require_paths:
209
+ require_paths:
54
210
  - lib
55
- required_ruby_version: !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
58
213
  - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
64
- required_rubygems_version: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
67
218
  - - ">="
68
- - !ruby/object:Gem::Version
69
- hash: 3
70
- segments:
71
- - 0
72
- version: "0"
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
73
221
  requirements: []
74
-
75
222
  rubyforge_project: webtopay
76
- rubygems_version: 1.7.2
223
+ rubygems_version: 2.2.0
77
224
  signing_key:
78
- specification_version: 3
79
- summary: Provides integration with http://www.webtopay.com (mokejimai.lt) payment system
225
+ specification_version: 4
226
+ summary: Provides integration with http://www.webtopay.com (mokejimai.lt) payment
227
+ system
80
228
  test_files: []
81
-
229
+ has_rdoc:
data/README DELETED
@@ -1,71 +0,0 @@
1
- WebToPay
2
- ===========
3
-
4
- It is a plugin which could be very useful on working with https://www.webtopay.com/ (https://www.mokejimai.lt/) billing system.
5
- The main function of this plugin is protection against forgeries that could be done by sending required parameters to the application and getting services without paying for free.
6
- This plugin could be integrated with both billing types - MACRO (VISA, MasterCard, banks etc.) and MICRO (SMS, phone calls etc.).
7
-
8
- *It works with OpenSSL so be sure that you have installed all necessary libs or modules on your system.
9
-
10
- Installation
11
- ===========
12
-
13
- cd your_application
14
- ./script/plugin install git://github.com/kristis/webtopay.git
15
-
16
- Configuration
17
- ===========
18
- Create initializer
19
- config/initializers/webtopay.rb
20
-
21
- WebToPay.configure do |config|
22
- config.project_id = 00000
23
- config.sign_password = 'your sign password'
24
- end
25
-
26
- Examples
27
- ===========
28
-
29
- These code slices will protect your controller actions, which work with webtopay.com billing, against forgeries.
30
-
31
- ****** Usage for MICRO and MACRO billing on controller:
32
-
33
- [........]
34
-
35
- webtopay :activate_user, :confirm_cart # You can add here as many actions as you want
36
-
37
- def activate_user
38
- # write here code which do some stuff
39
- render :text => "Your user has been successfully activated. Thank you!" # it sends SMS answer
40
- end
41
-
42
- def confirm_cart
43
- # write here code which do some stuff
44
- render :text => "ok" # it sends successful answer to webtopay.com crawler
45
- end
46
-
47
- [........]
48
-
49
- ****** You can also use helper method to generate required form for MACRO billing query:
50
-
51
- [........]
52
-
53
- <% macro_form :test => 1, :orderid => 123, :amount => 2000, :payment => 'vb', :country => 'lt', :paytext => "Billing for XX at the website XXX" do %>
54
- Select paying method: <%= select_tag(:payment, options_for_select(['', 'vb', 'hanza', 'nord', 'snoras'])) %>
55
- <%= submit_tag "test paying" %>
56
- <% end %>
57
-
58
- [........]
59
-
60
- * Attention!!! Be sure that you have set required option, which let sending test requests, at your webtopay.com account settings at first if you want to use test requests.
61
- * You can add into the helper options additional parameters or remove them according to technical specifications at https://www.webtopay.com/ .
62
-
63
- TODO
64
- ===========
65
-
66
- 1. Write more clear documentation with real examples
67
- 2. Write unit tests for each billing method (requires some testing data from https://www.webtopay.com/)
68
- 3. Add more settings for controller filters
69
-
70
- ===========
71
- Copyright (c) 2009 Kristijonas Urbaitis, released under the MIT license
@@ -1,407 +0,0 @@
1
- require 'digest/md5'
2
- require 'openssl'
3
- require 'open-uri'
4
-
5
- module WebToPay
6
- class Api
7
- VERSION = "1.2"
8
- PREFIX = "wp_"
9
- CERT_PATH = "http://downloads.webtopay.com/download/public.key"
10
-
11
- # Array structure:
12
- # * name – request item name.
13
- # * maxlen – max allowed value for item.
14
- # * required – is this item is required.
15
- # * user – if true, user can set value of this item, if false
16
- # item value is generated.
17
- # * isrequest – if true, item will be included in request array, if
18
- # false, item only be used internaly and will not be
19
- # included in outgoing request array.
20
- # * regexp – regexp to test item value.
21
- REQUEST_SPECS = [ [ :projectid, 11, true, true, true, /^\d+$/ ],
22
- [ :orderid, 40, true, true, true, '' ],
23
- [ :lang, 3, false, true, true, /^[a-z]{3}$/i ],
24
- [ :amount, 11, false, true, true, /^\d+$/ ],
25
- [ :currency, 3, false, true, true, /^[a-z]{3}$/i ],
26
- [ :accepturl, 255, true, true, true, '' ],
27
- [ :cancelurl, 255, true, true, true, '' ],
28
- [ :callbackurl, 255, true, true, true, '' ],
29
- [ :payment, 20, false, true, true, '' ],
30
- [ :country, 2, false, true, true, /^[a-z]{2}$/i ],
31
- [ :paytext, 255, false, true, true, '' ],
32
- [ :p_firstname, 255, false, true, true, '' ],
33
- [ :p_lastname, 255, false, true, true, '' ],
34
- [ :p_email, 255, false, true, true, '' ],
35
- [ :p_street, 255, false, true, true, '' ],
36
- [ :p_city, 255, false, true, true, '' ],
37
- [ :p_state, 20, false, true, true, '' ],
38
- [ :p_zip, 20, false, true, true, '' ],
39
- [ :p_countrycode, 2, false, true, true, /^[a-z]{2}$/i ],
40
- [ :sign, 255, true, false, true, '' ],
41
- [ :sign_password, 255, true, true, false, '' ],
42
- [ :test, 1, false, true, true, /^[01]$/ ],
43
- [ :version, 9, true, false, true, /^\d+\.\d+$/ ] ]
44
-
45
- # Array structure:
46
- # * name – request item name.
47
- # * maxlen – max allowed value for item.
48
- # * required – is this item is required in response.
49
- # * mustcheck – this item must be checked by user.
50
- # * isresponse – if false, item must not be included in response array.
51
- # * regexp – regexp to test item value.
52
- MAKRO_RESPONSE_SPECS = [
53
- [ :projectid, 11, true, true, true, /^\d+$/ ],
54
- [ :orderid, 40, false, false, true, '' ],
55
- [ :lang, 3, false, false, true, /^[a-z]{3}$/i ],
56
- [ :amount, 11, false, false, true, /^\d+$/ ],
57
- [ :currency, 3, false, false, true, /^[a-z]{3}$/i ],
58
- [ :payment, 20, false, false, true, '' ],
59
- [ :country, 2, false, false, true, /^[a-z]{2}$/i ],
60
- [ :paytext, 0, false, false, true, '' ],
61
- [ :_ss2, 0, true, false, true, '' ],
62
- [ :_ss1, 0, false, false, true, '' ],
63
- [ :name, 255, false, false, true, '' ],
64
- [ :surename, 255, false, false, true, '' ],
65
- [ :status, 255, false, false, true, '' ],
66
- [ :error, 20, false, false, true, '' ],
67
- [ :test, 1, false, false, true, /^[01]$/ ],
68
- [ :p_email, 0, false, false, true, '' ],
69
- [ :payamount, 0, false, false, true, '' ],
70
- [ :paycurrency, 0, false, false, true, '' ],
71
- [ :version, 9, true, false, true, /^\d+\.\d+$/ ],
72
- [ :sign_password, 255, false, true, false, '' ] ]
73
-
74
- # Specification array for mikro response.
75
- #
76
- # Array structure:
77
- # * name – request item name.
78
- # * maxlen – max allowed value for item.
79
- # * required – is this item is required in response.
80
- # * mustcheck – this item must be checked by user.
81
- # * isresponse – if false, item must not be included in response array.
82
- # * regexp – regexp to test item value.
83
- MIKRO_RESPONSE_SPECS = [
84
- [ :to, 0, true, false, true, '' ],
85
- [ :sms, 0, true, false, true, '' ],
86
- [ :from, 0, true, false, true, '' ],
87
- [ :operator, 0, true, false, true, '' ],
88
- [ :amount, 0, true, false, true, '' ],
89
- [ :currency, 0, true, false, true, '' ],
90
- [ :country, 0, true, false, true, '' ],
91
- [ :id, 0, true, false, true, '' ],
92
- [ :_ss2, 0, true, false, true, '' ],
93
- [ :_ss1, 0, true, false, true, '' ],
94
- [ :test, 0, true, false, true, '' ],
95
- [ :key, 0, true, false, true, '' ],
96
- #[ :version, 9, true, false, true, /^\d+\.\d+$/ ]
97
- ]
98
-
99
- # Checks user given request data array.
100
- #
101
- # If any errors occurs, WebToPay::Exception will be raised.
102
- #
103
- # This method returns validated request array. Returned array contains
104
- # only those items from data, that are needed.
105
- #
106
- def self.check_request_data(data)
107
- request = {}
108
- data.symbolize_keys!
109
-
110
- REQUEST_SPECS.each do |spec|
111
- name, maxlen, required, user, isrequest, regexp = spec
112
-
113
- next unless user
114
-
115
- name = name.to_sym
116
-
117
- if required && data[name].nil?
118
- e = Exception.new self._("'%s' is required but missing.", name)
119
- e.code = Exception::E_MISSING
120
- e.field_name = name
121
- raise e
122
- end
123
-
124
- unless data[name].to_s.empty?
125
- if (maxlen && data[name].to_s.length > maxlen)
126
- e = Exception.new self._("'%s' value '%s' is too long, %d characters allowed.",
127
- name, data[name], maxlen)
128
- e.code = Exception::E_MAXLEN
129
- e.field_name = name
130
- raise e
131
- end
132
-
133
- if ('' != regexp && !data[name].to_s.match(regexp))
134
- e = Exception.new self._("'%s' value '%s' is invalid.", name, data[name])
135
- e.code = Exception::E_REGEXP
136
- e.field_name = name
137
- raise e
138
- end
139
- end
140
-
141
- if isrequest && !data[name].nil?
142
- request[name] = data[name]
143
- end
144
- end
145
-
146
- return request
147
- end
148
-
149
- # Puts signature on request data hash
150
- def self.sign_request(request, password)
151
- fields = [ :projectid, :orderid, :lang, :amount, :currency,
152
- :accepturl, :cancelurl, :callbackurl, :payment, :country,
153
- :p_firstname, :p_lastname, :p_email, :p_street,
154
- :p_city, :p_state, :p_zip, :p_countrycode, :test,
155
- :version ]
156
-
157
- request.symbolize_keys!
158
-
159
- data = ''
160
-
161
- fields.each do |key|
162
- val = request[key].to_s
163
-
164
- unless val.strip.blank?
165
- data+= sprintf("%03d", val.length) + val.downcase
166
- end
167
- end
168
-
169
- request[:sign] = Digest::MD5.hexdigest(data + password)
170
-
171
- return request
172
- end
173
-
174
- # Builds request data array.
175
- #
176
- # This method checks all given data and generates correct request data
177
- # array or raises WebToPayException on failure.
178
- #
179
- # Method accepts single parameter $data of array type. All possible array
180
- # keys are described here:
181
- # https://www.mokejimai.lt/makro_specifikacija.html
182
- def self.build_request(data)
183
- data.symbolize_keys!
184
-
185
- request = self.check_request_data(data)
186
- request[:version] = self::VERSION
187
- request = self.sign_request(request, data[:sign_password])
188
-
189
- return request
190
- end
191
-
192
- # Checks and validates response from WebToPay server.
193
- #
194
- # This function accepts both mikro and makro responses.
195
- #
196
- # First parameter usualy should by params hash
197
- #
198
- # Description about response can be found here:
199
- # makro: https://www.mokejimai.lt/makro_specifikacija.html
200
- # mikro: https://www.mokejimai.lt/mikro_mokejimu_specifikacija_SMS.html
201
- #
202
- # If response is not correct, WebToPay::Exception will be raised.
203
- def self.check_response(query, user_data = {})
204
- @@verified = false;
205
-
206
- response = self.query_to_response(query)
207
- response = self.get_prefixed(response, self::PREFIX)
208
- response.symbolize_keys!
209
-
210
- # *get* response type (makro|mikro)
211
- type, specs = self.get_specs_for_response(response)
212
-
213
- self.check_response_data(response, user_data, specs)
214
- @@verified = 'RESPONSE';
215
-
216
- # *check* response
217
- if :makro == type && response[:version] != self::VERSION
218
- e = Exception.new(self._('Incompatible library and response versions: ' +
219
- 'libwebtopay %s, response %s', self::VERSION, response[:version]))
220
- e.code = Exception::E_INVALID
221
- raise e
222
- end
223
-
224
- if :makro == type
225
- @@verified = 'RESPONSE VERSION ' + response[:version] + ' OK'
226
- end
227
-
228
- orderid = :makro == type ? response[:orderid] : response[:id]
229
- password = user_data[:sign_password]
230
-
231
- if self.check_response_cert(query)
232
- @@verified = 'SS2 public.key'
233
- end
234
-
235
- # *check* status
236
- if :makro == type && 1 != response[:status].to_i
237
- e = Exception.new(self._('Returned transaction status is %d, successful status ' +
238
- 'should be 1.', response[:status]))
239
- e.code = Exception::E_INVALID
240
- raise e
241
- end
242
-
243
- return response
244
- end
245
-
246
- def self.check_response_data(response, mustcheck_data, specs)
247
- resp_keys = []
248
-
249
- response.symbolize_keys!
250
- mustcheck_data.symbolize_keys!
251
-
252
- specs.each do |spec|
253
- name, maxlen, required, mustcheck, is_response, regexp = spec
254
-
255
- if required && response[name].nil?
256
- e = Exception.new(self._("'%s' is required but missing.", name))
257
- e.code = Exception::E_MISSING
258
- e.field_name = name
259
- raise e
260
- end
261
-
262
- if mustcheck
263
- if mustcheck_data[name].nil?
264
- e = Exception.new(self._("'%s' must exists in array of second " +
265
- "parameter of checkResponse() method.", name))
266
- e.code = Exception::E_USER_PARAMS
267
- e.field_name = name
268
- raise e
269
- end
270
-
271
- if is_response
272
- if response[name].to_s != mustcheck_data[name].to_s
273
- e = Exception.new(self._("'%s' yours and requested value is not " +
274
- "equal ('%s' != '%s') ",
275
- name, mustcheck_data[name], response[name]))
276
- e.code = Exception::E_INVALID
277
- e.field_name = name
278
- raise e
279
- end
280
- end
281
- end
282
-
283
- if !response[name].to_s.empty?
284
- if maxlen > 0 && response[name].to_s.length > maxlen
285
- e = Exception.new(self._("'%s' value '%s' is too long, %d characters allowed.",
286
- name, response[name], maxlen))
287
- e.code = Exception::E_MAXLEN
288
- e.field_name = name
289
- raise e
290
- end
291
-
292
- if '' != regexp && !response[name].to_s.match(regexp)
293
- e = new WebToPayException(self._("'%s' value '%s' is invalid.",
294
- name, response[name]))
295
- e.code = Exception::E_REGEXP
296
- e.field_name = name
297
- raise e
298
- end
299
- end
300
-
301
- resp_keys << name unless response[name].nil?
302
- end
303
-
304
- # Filter only parameters passed from webtopay
305
- _response = {}
306
-
307
- response.keys.each do |key|
308
- _response[key] = response[key] if resp_keys.include?(key)
309
- end
310
-
311
- return _response
312
- end
313
-
314
- # Return type and specification of given response array.
315
- def self.get_specs_for_response(response)
316
- response.symbolize_keys!
317
-
318
- if ( !response[:to].nil? && !response[:from].nil? &&
319
- !response[:sms].nil? && response[:projectid].nil? )
320
-
321
- type = :mikro
322
- specs = self::MIKRO_RESPONSE_SPECS
323
- else
324
- type = :makro
325
- specs = self::MAKRO_RESPONSE_SPECS
326
- end
327
-
328
- return [ type, specs ]
329
- end
330
-
331
- # Check if response certificate is valid
332
- def self.check_response_cert(query)
333
- public_key = self.get_public_key
334
-
335
- if (!public_key)
336
- e = Exception.new(self._('Can\'t get openssl public key for %s', cert))
337
- e.code = Exception::E_INVALID
338
- raise e
339
- end
340
-
341
- res = self.query_to_response(query)
342
- res = self.get_prefixed(res, PREFIX).symbolize_keys
343
- keys = self.get_query_keys(query)
344
- skip = [ :_ss2, :controller, :action ]
345
- _SS2 = ''
346
-
347
- keys.each do |key|
348
- if !skip.include?(key)
349
- _SS2 << res[key].to_s + '|'
350
- end
351
- end
352
-
353
- if !public_key.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(res[:_ss2]), _SS2)
354
- e = Exception.new(self._('Can\'t verify SS2'))
355
- e.code = Exception::E_INVALID
356
- raise e
357
- end
358
-
359
- return true
360
- end
361
-
362
- def self.get_public_key
363
- OpenSSL::X509::Certificate.new(open(CERT_PATH).read).public_key
364
- end
365
-
366
- def self.get_prefixed(data, prefix)
367
- return data if prefix.to_s.blank?
368
-
369
- ret = {}
370
- reg = /^#{prefix}/
371
-
372
- data.stringify_keys!
373
-
374
- data.each_pair do |key, val|
375
- if key.length > prefix.length && key.match(reg)
376
- ret[key.gsub(reg, '')] = val
377
- end
378
- end
379
-
380
- return ret
381
- end
382
-
383
- def self.query_to_response(query)
384
- response = {}
385
-
386
- query.split(/&/).each do |item|
387
- key, val = item.split(/\=/)
388
- response[key] = CGI.unescape(val.to_s)
389
- end
390
-
391
- return response
392
- end
393
-
394
- def self.get_query_keys(query)
395
- query.split(/&/).collect { |i| i.split(/\=/).first.gsub(/^#{PREFIX}/, '').to_sym }
396
- end
397
-
398
- # I18n support.
399
- def self._(*args)
400
- if args.length > 1
401
- return send(:sprintf, *args)
402
- else
403
- return args[0]
404
- end
405
- end
406
- end
407
- end