mangopay 3.0.25 → 3.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +30 -28
  3. data/.rspec +2 -2
  4. data/.travis.yml +13 -4
  5. data/Gemfile +2 -2
  6. data/LICENSE +20 -20
  7. data/README.md +126 -126
  8. data/bin/mangopay +9 -9
  9. data/lib/generators/mangopay/install_generator.rb +60 -60
  10. data/lib/generators/templates/mangopay.rb.erb +5 -5
  11. data/lib/mangopay.rb +228 -225
  12. data/lib/mangopay/authorization_token.rb +88 -88
  13. data/lib/mangopay/bank_account.rb +38 -38
  14. data/lib/mangopay/bankingaliases.rb +29 -0
  15. data/lib/mangopay/bankingaliases_iban.rb +16 -0
  16. data/lib/mangopay/card.rb +8 -8
  17. data/lib/mangopay/card_registration.rb +9 -9
  18. data/lib/mangopay/client.rb +74 -74
  19. data/lib/mangopay/dispute.rb +130 -130
  20. data/lib/mangopay/errors.rb +61 -61
  21. data/lib/mangopay/event.rb +18 -18
  22. data/lib/mangopay/filter_parameters.rb +46 -46
  23. data/lib/mangopay/hook.rb +9 -9
  24. data/lib/mangopay/http_calls.rb +85 -85
  25. data/lib/mangopay/json.rb +14 -14
  26. data/lib/mangopay/kyc_document.rb +70 -70
  27. data/lib/mangopay/legal_user.rb +15 -15
  28. data/lib/mangopay/mandate.rb +32 -32
  29. data/lib/mangopay/natural_user.rb +14 -14
  30. data/lib/mangopay/pay_in.rb +96 -85
  31. data/lib/mangopay/pay_out.rb +14 -14
  32. data/lib/mangopay/pre_authorization.rb +13 -13
  33. data/lib/mangopay/refund.rb +7 -7
  34. data/lib/mangopay/report.rb +17 -17
  35. data/lib/mangopay/resource.rb +21 -21
  36. data/lib/mangopay/transaction.rb +24 -24
  37. data/lib/mangopay/transfer.rb +9 -9
  38. data/lib/mangopay/user.rb +43 -43
  39. data/lib/mangopay/version.rb +3 -3
  40. data/lib/mangopay/wallet.rb +17 -17
  41. data/mangopay.gemspec +30 -30
  42. data/spec/mangopay/authorization_token_spec.rb +70 -70
  43. data/spec/mangopay/bank_account_spec.rb +97 -97
  44. data/spec/mangopay/bankingaliases_spec.rb +29 -0
  45. data/spec/mangopay/card_registration_spec.rb +73 -73
  46. data/spec/mangopay/client_spec.rb +110 -110
  47. data/spec/mangopay/configuration_spec.rb +95 -95
  48. data/spec/mangopay/dispute_spec.rb +262 -262
  49. data/spec/mangopay/event_spec.rb +31 -31
  50. data/spec/mangopay/fetch_filters_spec.rb +63 -63
  51. data/spec/mangopay/hook_spec.rb +37 -37
  52. data/spec/mangopay/idempotency_spec.rb +41 -41
  53. data/spec/mangopay/kyc_document_spec.rb +103 -103
  54. data/spec/mangopay/log_requests_filter_spec.rb +25 -25
  55. data/spec/mangopay/mandate_spec.rb +92 -92
  56. data/spec/mangopay/payin_bankwire_direct_spec.rb +74 -74
  57. data/spec/mangopay/payin_card_direct_spec.rb +68 -68
  58. data/spec/mangopay/payin_card_web_spec.rb +47 -38
  59. data/spec/mangopay/payin_directdebit_direct_spec.rb +37 -37
  60. data/spec/mangopay/payin_directdebit_web_spec.rb +38 -38
  61. data/spec/mangopay/payin_paypal_web_spec.rb +38 -38
  62. data/spec/mangopay/payin_preauthorized_direct_spec.rb +68 -68
  63. data/spec/mangopay/payout_bankwire_spec.rb +54 -54
  64. data/spec/mangopay/preauthorization_spec.rb +42 -42
  65. data/spec/mangopay/refund_spec.rb +21 -21
  66. data/spec/mangopay/report_spec.rb +39 -39
  67. data/spec/mangopay/shared_resources.rb +397 -381
  68. data/spec/mangopay/transaction_spec.rb +54 -54
  69. data/spec/mangopay/transfer_spec.rb +69 -69
  70. data/spec/mangopay/user_spec.rb +137 -137
  71. data/spec/mangopay/wallet_spec.rb +80 -80
  72. data/spec/spec_helper.rb +31 -31
  73. metadata +7 -6
  74. data/lib/mangopay/temp.rb +0 -74
  75. data/spec/mangopay/temp_paymentcard_spec.rb +0 -31
@@ -1,5 +1,5 @@
1
- MangoPay.configure do |c|
2
- c.preproduction = <%= options[:preproduction] %>
3
- c.client_id = '<%= @client_id %>'
4
- c.client_passphrase = '<%= @client_passphrase %>'
5
- end
1
+ MangoPay.configure do |c|
2
+ c.preproduction = <%= options[:preproduction] %>
3
+ c.client_id = '<%= @client_id %>'
4
+ c.client_passphrase = '<%= @client_passphrase %>'
5
+ end
data/lib/mangopay.rb CHANGED
@@ -1,225 +1,228 @@
1
- require 'net/http'
2
- require 'cgi/util'
3
- require 'digest/md5'
4
- require 'multi_json'
5
- require 'benchmark'
6
- require 'logger'
7
-
8
- # helpers
9
- require 'mangopay/version'
10
- require 'mangopay/errors'
11
-
12
-
13
- module MangoPay
14
-
15
- autoload :HTTPCalls, 'mangopay/http_calls'
16
- autoload :Resource, 'mangopay/resource'
17
- autoload :Client, 'mangopay/client'
18
- autoload :User, 'mangopay/user'
19
- autoload :NaturalUser, 'mangopay/natural_user'
20
- autoload :LegalUser, 'mangopay/legal_user'
21
- autoload :PayIn, 'mangopay/pay_in'
22
- autoload :PayOut, 'mangopay/pay_out'
23
- autoload :Transfer, 'mangopay/transfer'
24
- autoload :Transaction, 'mangopay/transaction'
25
- autoload :Wallet, 'mangopay/wallet'
26
- autoload :BankAccount, 'mangopay/bank_account'
27
- autoload :CardRegistration, 'mangopay/card_registration'
28
- autoload :PreAuthorization, 'mangopay/pre_authorization'
29
- autoload :Card, 'mangopay/card'
30
- autoload :Event, 'mangopay/event'
31
- autoload :KycDocument, 'mangopay/kyc_document'
32
- autoload :Hook, 'mangopay/hook'
33
- autoload :Refund, 'mangopay/refund'
34
- autoload :Dispute, 'mangopay/dispute'
35
- autoload :Mandate, 'mangopay/mandate'
36
- autoload :Report, 'mangopay/report'
37
- autoload :JSON, 'mangopay/json'
38
- autoload :AuthorizationToken, 'mangopay/authorization_token'
39
- autoload :FilterParameters, 'mangopay/filter_parameters'
40
-
41
- # temporary
42
- autoload :Temp, 'mangopay/temp'
43
-
44
- class Configuration
45
- attr_accessor :preproduction, :root_url,
46
- :client_id, :client_passphrase,
47
- :temp_dir, :log_file
48
-
49
- def preproduction
50
- @preproduction || false
51
- end
52
-
53
- def root_url
54
- @root_url || (@preproduction == true ? "https://api.sandbox.mangopay.com" : "https://api.mangopay.com")
55
- end
56
- end
57
-
58
- class << self
59
- def version_code
60
- "v2.01"
61
- end
62
-
63
- def api_path
64
- "/#{version_code}/#{MangoPay.configuration.client_id}"
65
- end
66
-
67
- def api_uri(url='')
68
- URI(configuration.root_url + url)
69
- end
70
-
71
- def configuration=(value)
72
- Thread.current[:mangopay_configuration] = value
73
- @last_configuration_set = value
74
- end
75
-
76
- def configuration
77
- config = Thread.current[:mangopay_configuration]
78
-
79
- config ||
80
- ( @last_configuration_set &&
81
- (self.configuration = @last_configuration_set.dup)) ||
82
- (self.configuration = MangoPay::Configuration.new)
83
- end
84
-
85
- def configure
86
- config = self.configuration
87
- yield config
88
- self.configuration = config
89
- end
90
-
91
- def with_configuration(config)
92
- original_config = MangoPay.configuration
93
- MangoPay.configuration = config
94
- yield
95
- ensure
96
- MangoPay.configuration = original_config
97
- end
98
-
99
- #
100
- # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
101
- # - +url+: the part after Configuration#root_url
102
- # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
103
- # - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
104
- # - +headers_or_idempotency_key+: hash of headers; or replaced by request_headers if nil; or added to request_headers as idempotency key otherwise (see https://docs.mangopay.com/api-references/idempotency-support/)
105
- # - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
106
- #
107
- # Raises MangoPay::ResponseError if response code != 200.
108
- #
109
- def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil, before_request_proc = nil)
110
- uri = api_uri(url)
111
- uri.query = URI.encode_www_form(filters) unless filters.empty?
112
-
113
- if headers_or_idempotency_key.is_a?(Hash)
114
- headers = headers_or_idempotency_key
115
- else
116
- headers = request_headers
117
- headers['Idempotency-Key'] = headers_or_idempotency_key if headers_or_idempotency_key != nil
118
- end
119
-
120
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
121
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
122
- req.body = JSON.dump(params)
123
- before_request_proc.call(req) if before_request_proc
124
- do_request(http, req, uri)
125
- end
126
-
127
- # decode json data
128
- data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
129
-
130
- unless res.is_a?(Net::HTTPOK)
131
- raise MangoPay::ResponseError.new(uri, res.code, data)
132
- end
133
-
134
- # copy pagination info if any
135
- ['x-number-of-pages', 'x-number-of-items'].each { |k|
136
- filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
137
- }
138
-
139
- data
140
- end
141
-
142
- # Retrieve a previous response by idempotency_key
143
- # See https://docs.mangopay.com/api-references/idempotency-support/
144
- def fetch_response(idempotency_key)
145
- url = "#{api_path}/responses/#{idempotency_key}"
146
- request(:get, url)
147
- end
148
-
149
- private
150
-
151
- def user_agent
152
- {
153
- bindings_version: VERSION,
154
- lang: 'ruby',
155
- lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
156
- platform: RUBY_PLATFORM,
157
- uname: get_uname
158
- }
159
- end
160
-
161
- def get_uname
162
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
163
- rescue Errno::ENOMEM
164
- 'uname lookup failed'
165
- end
166
-
167
- def request_headers
168
- auth_token = AuthorizationToken::Manager.get_token
169
- headers = {
170
- 'user_agent' => "MangoPay V2 RubyBindings/#{VERSION}",
171
- 'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
172
- 'Content-Type' => 'application/json'
173
- }
174
- begin
175
- headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent))
176
- rescue => e
177
- headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
178
- end
179
- end
180
-
181
- def do_request(http, req, uri)
182
- if configuration.log_file.nil?
183
- do_request_without_log(http, req)
184
- else
185
- do_request_with_log(http, req, uri)
186
- end
187
- end
188
-
189
- def do_request_with_log(http, req, uri)
190
- res, time = nil, nil
191
- params = FilterParameters.request(req.body)
192
- line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params}"
193
- begin
194
- time = Benchmark.realtime { res = do_request_without_log(http, req) }
195
- res
196
- ensure
197
- params = FilterParameters.response(res.body)
198
- line = "#{log_severity(res)} #{line}"
199
- line += "\n [#{(time * 1000).round(1)}ms] #{res.code} #{params}\n"
200
- logger.info { line }
201
- end
202
- end
203
-
204
- def do_request_without_log(http, req)
205
- http.request(req)
206
- end
207
-
208
- def log_severity(res)
209
- errors = [Net::HTTPClientError, Net::HTTPServerError, Net::HTTPUnknownResponse]
210
- errors.any? { |klass| res.is_a?(klass) } ? 'E' : 'I'
211
- end
212
-
213
- def logger
214
- raise NotImplementedError if configuration.log_file.nil?
215
- if @logger.nil?
216
- @logger = Logger.new(configuration.log_file)
217
- @logger.formatter = proc do |severity, datetime, progname, msg|
218
- "#{msg}\n"
219
- end
220
- end
221
- @logger
222
- end
223
-
224
- end
225
- end
1
+ require 'net/http'
2
+ require 'cgi/util'
3
+ require 'digest/md5'
4
+ require 'multi_json'
5
+ require 'benchmark'
6
+ require 'logger'
7
+ require 'time'
8
+
9
+ # helpers
10
+ require 'mangopay/version'
11
+ require 'mangopay/errors'
12
+
13
+
14
+ module MangoPay
15
+
16
+ autoload :HTTPCalls, 'mangopay/http_calls'
17
+ autoload :Resource, 'mangopay/resource'
18
+ autoload :Client, 'mangopay/client'
19
+ autoload :User, 'mangopay/user'
20
+ autoload :NaturalUser, 'mangopay/natural_user'
21
+ autoload :LegalUser, 'mangopay/legal_user'
22
+ autoload :PayIn, 'mangopay/pay_in'
23
+ autoload :PayOut, 'mangopay/pay_out'
24
+ autoload :Transfer, 'mangopay/transfer'
25
+ autoload :Transaction, 'mangopay/transaction'
26
+ autoload :Wallet, 'mangopay/wallet'
27
+ autoload :BankAccount, 'mangopay/bank_account'
28
+ autoload :CardRegistration, 'mangopay/card_registration'
29
+ autoload :PreAuthorization, 'mangopay/pre_authorization'
30
+ autoload :Card, 'mangopay/card'
31
+ autoload :Event, 'mangopay/event'
32
+ autoload :KycDocument, 'mangopay/kyc_document'
33
+ autoload :Hook, 'mangopay/hook'
34
+ autoload :Refund, 'mangopay/refund'
35
+ autoload :Dispute, 'mangopay/dispute'
36
+ autoload :Mandate, 'mangopay/mandate'
37
+ autoload :Report, 'mangopay/report'
38
+ autoload :JSON, 'mangopay/json'
39
+ autoload :AuthorizationToken, 'mangopay/authorization_token'
40
+ autoload :FilterParameters, 'mangopay/filter_parameters'
41
+ autoload :BankingAliases, 'mangopay/bankingaliases'
42
+ autoload :BankingAliasesIBAN, 'mangopay/bankingaliases_iban'
43
+
44
+ # temporary
45
+ autoload :Temp, 'mangopay/temp'
46
+
47
+ class Configuration
48
+ attr_accessor :preproduction, :root_url,
49
+ :client_id, :client_passphrase,
50
+ :temp_dir, :log_file
51
+
52
+ def preproduction
53
+ @preproduction || false
54
+ end
55
+
56
+ def root_url
57
+ @root_url || (@preproduction == true ? "https://api.sandbox.mangopay.com" : "https://api.mangopay.com")
58
+ end
59
+ end
60
+
61
+ class << self
62
+ def version_code
63
+ "v2.01"
64
+ end
65
+
66
+ def api_path
67
+ "/#{version_code}/#{MangoPay.configuration.client_id}"
68
+ end
69
+
70
+ def api_uri(url='')
71
+ URI(configuration.root_url + url)
72
+ end
73
+
74
+ def configuration=(value)
75
+ Thread.current[:mangopay_configuration] = value
76
+ @last_configuration_set = value
77
+ end
78
+
79
+ def configuration
80
+ config = Thread.current[:mangopay_configuration]
81
+
82
+ config ||
83
+ ( @last_configuration_set &&
84
+ (self.configuration = @last_configuration_set.dup)) ||
85
+ (self.configuration = MangoPay::Configuration.new)
86
+ end
87
+
88
+ def configure
89
+ config = self.configuration
90
+ yield config
91
+ self.configuration = config
92
+ end
93
+
94
+ def with_configuration(config)
95
+ original_config = MangoPay.configuration
96
+ MangoPay.configuration = config
97
+ yield
98
+ ensure
99
+ MangoPay.configuration = original_config
100
+ end
101
+
102
+ #
103
+ # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
104
+ # - +url+: the part after Configuration#root_url
105
+ # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
106
+ # - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
107
+ # - +headers_or_idempotency_key+: hash of headers; or replaced by request_headers if nil; or added to request_headers as idempotency key otherwise (see https://docs.mangopay.com/api-references/idempotency-support/)
108
+ # - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
109
+ #
110
+ # Raises MangoPay::ResponseError if response code != 200.
111
+ #
112
+ def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil, before_request_proc = nil)
113
+ uri = api_uri(url)
114
+ uri.query = URI.encode_www_form(filters) unless filters.empty?
115
+
116
+ if headers_or_idempotency_key.is_a?(Hash)
117
+ headers = headers_or_idempotency_key
118
+ else
119
+ headers = request_headers
120
+ headers['Idempotency-Key'] = headers_or_idempotency_key if headers_or_idempotency_key != nil
121
+ end
122
+
123
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
124
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
125
+ req.body = JSON.dump(params)
126
+ before_request_proc.call(req) if before_request_proc
127
+ do_request(http, req, uri)
128
+ end
129
+
130
+ # decode json data
131
+ data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
132
+
133
+ unless res.is_a?(Net::HTTPOK)
134
+ raise MangoPay::ResponseError.new(uri, res.code, data)
135
+ end
136
+
137
+ # copy pagination info if any
138
+ ['x-number-of-pages', 'x-number-of-items'].each { |k|
139
+ filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
140
+ }
141
+
142
+ data
143
+ end
144
+
145
+ # Retrieve a previous response by idempotency_key
146
+ # See https://docs.mangopay.com/api-references/idempotency-support/
147
+ def fetch_response(idempotency_key)
148
+ url = "#{api_path}/responses/#{idempotency_key}"
149
+ request(:get, url)
150
+ end
151
+
152
+ private
153
+
154
+ def user_agent
155
+ {
156
+ bindings_version: VERSION,
157
+ lang: 'ruby',
158
+ lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
159
+ platform: RUBY_PLATFORM,
160
+ uname: get_uname
161
+ }
162
+ end
163
+
164
+ def get_uname
165
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
166
+ rescue Errno::ENOMEM
167
+ 'uname lookup failed'
168
+ end
169
+
170
+ def request_headers
171
+ auth_token = AuthorizationToken::Manager.get_token
172
+ headers = {
173
+ 'User-Agent' => "MANGOPAY V2 RubyBindings/#{VERSION}",
174
+ 'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
175
+ 'Content-Type' => 'application/json'
176
+ }
177
+ begin
178
+ headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent))
179
+ rescue => e
180
+ headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
181
+ end
182
+ end
183
+
184
+ def do_request(http, req, uri)
185
+ if configuration.log_file.nil?
186
+ do_request_without_log(http, req)
187
+ else
188
+ do_request_with_log(http, req, uri)
189
+ end
190
+ end
191
+
192
+ def do_request_with_log(http, req, uri)
193
+ res, time = nil, nil
194
+ params = FilterParameters.request(req.body)
195
+ line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params}"
196
+ begin
197
+ time = Benchmark.realtime { res = do_request_without_log(http, req) }
198
+ res
199
+ ensure
200
+ params = FilterParameters.response(res.body)
201
+ line = "#{log_severity(res)} #{line}"
202
+ line += "\n [#{(time * 1000).round(1)}ms] #{res.code} #{params}\n"
203
+ logger.info { line }
204
+ end
205
+ end
206
+
207
+ def do_request_without_log(http, req)
208
+ http.request(req)
209
+ end
210
+
211
+ def log_severity(res)
212
+ errors = [Net::HTTPClientError, Net::HTTPServerError, Net::HTTPUnknownResponse]
213
+ errors.any? { |klass| res.is_a?(klass) } ? 'E' : 'I'
214
+ end
215
+
216
+ def logger
217
+ raise NotImplementedError if configuration.log_file.nil?
218
+ if @logger.nil?
219
+ @logger = Logger.new(configuration.log_file)
220
+ @logger.formatter = proc do |severity, datetime, progname, msg|
221
+ "#{msg}\n"
222
+ end
223
+ end
224
+ @logger
225
+ end
226
+
227
+ end
228
+ end