Module: MangoPay
- Defined in:
- lib/mangopay/json.rb,
lib/mangopay.rb,
lib/mangopay/ubo.rb,
lib/mangopay/card.rb,
lib/mangopay/hook.rb,
lib/mangopay/user.rb,
lib/mangopay/event.rb,
lib/mangopay/client.rb,
lib/mangopay/errors.rb,
lib/mangopay/pay_in.rb,
lib/mangopay/refund.rb,
lib/mangopay/report.rb,
lib/mangopay/wallet.rb,
lib/mangopay/deposit.rb,
lib/mangopay/dispute.rb,
lib/mangopay/mandate.rb,
lib/mangopay/pay_out.rb,
lib/mangopay/version.rb,
lib/mangopay/resource.rb,
lib/mangopay/transfer.rb,
lib/mangopay/recipient.rb,
lib/mangopay/report_v2.rb,
lib/mangopay/conversion.rb,
lib/mangopay/http_calls.rb,
lib/mangopay/legal_user.rb,
lib/mangopay/regulatory.rb,
lib/mangopay/settlement.rb,
lib/mangopay/transaction.rb,
lib/mangopay/bank_account.rb,
lib/mangopay/kyc_document.rb,
lib/mangopay/natural_user.rb,
lib/mangopay/bankingaliases.rb,
lib/mangopay/legal_user_sca.rb,
lib/mangopay/ubo_declaration.rb,
lib/mangopay/virtual_account.rb,
lib/mangopay/natural_user_sca.rb,
lib/mangopay/card_registration.rb,
lib/mangopay/filter_parameters.rb,
lib/mangopay/pre_authorization.rb,
lib/mangopay/authorization_token.rb,
lib/mangopay/bankingaliases_iban.rb,
lib/mangopay/identity_verification.rb,
lib/mangopay/payment_method_metadata.rb
Overview
We can use MultiJson directly , why do we even have this module ?
Defined Under Namespace
Modules: AuthorizationToken, FilterParameters, HTTPCalls, JSON
Classes: BankAccount, BankingAliases, BankingAliasesIBAN, Card, CardRegistration, Client, Configuration, Conversion, Deposit, Dispute, Error, Event, Hook, IdentityVerification, KycDocument, LegalUser, LegalUserSca, Mandate, NaturalUser, NaturalUserSca, PayIn, PayOut, PaymentMethodMetadata, PreAuthorization, Recipient, Refund, Regulatory, Report, ReportV2, Resource, ResponseError, Settlement, Transaction, Transfer, Ubo, UboDeclaration, User, VirtualAccount, Wallet
Constant Summary
collapse
- VERSION =
'3.42.1'
Class Method Summary
collapse
-
.add_config(name, config) ⇒ Object
Add MangoPay.Configuration to the list of configs.
-
.api_path ⇒ Object
-
.api_path_no_client ⇒ Object
-
.api_path_v3 ⇒ Object
-
.api_uri(url = '') ⇒ Object
-
.configuration ⇒ Object
-
.configuration=(value) ⇒ Object
-
.configure {|config| ... } ⇒ Object
-
.fetch_response(idempotency_key) ⇒ Object
-
.get_config(name) ⇒ Object
Fetch a MangoPay configuration from the list of configs.
-
.ratelimit ⇒ Object
-
.ratelimit=(obj) ⇒ Object
-
.remove_config(name) ⇒ Object
-
.request(method, url, params = {}, filters = {}, headers_or_idempotency_key = nil, before_request_proc = nil) ⇒ Object
-
.request_multipart(method, url, file, file_name, idempotency_key = nil) ⇒ Object
-
.version_code ⇒ Object
-
.version_code_v3 ⇒ Object
-
.with_configuration(config) ⇒ Object
Class Method Details
.add_config(name, config) ⇒ Object
Add MangoPay.Configuration to the list of configs
187
188
189
|
# File 'lib/mangopay.rb', line 187
def add_config(name, config)
@configurations[name] = config
end
|
.api_path ⇒ Object
134
135
136
|
# File 'lib/mangopay.rb', line 134
def api_path
"/#{version_code}/#{MangoPay.configuration.client_id}"
end
|
.api_path_no_client ⇒ Object
138
139
140
|
# File 'lib/mangopay.rb', line 138
def api_path_no_client
"/#{version_code}"
end
|
.api_path_v3 ⇒ Object
142
143
144
|
# File 'lib/mangopay.rb', line 142
def api_path_v3
"/#{version_code_v3}/#{MangoPay.configuration.client_id}"
end
|
.api_uri(url = '') ⇒ Object
146
147
148
|
# File 'lib/mangopay.rb', line 146
def api_uri(url='')
URI(configuration.root_url + url)
end
|
.configuration ⇒ Object
155
156
157
158
159
160
161
162
|
# File 'lib/mangopay.rb', line 155
def configuration
config = Thread.current[:mangopay_configuration]
config ||
( @last_configuration_set &&
(self.configuration = @last_configuration_set.dup)) ||
(self.configuration = MangoPay::Configuration.new)
end
|
.configuration=(value) ⇒ Object
150
151
152
153
|
# File 'lib/mangopay.rb', line 150
def configuration=(value)
Thread.current[:mangopay_configuration] = value
@last_configuration_set = value
end
|
164
165
166
167
168
|
# File 'lib/mangopay.rb', line 164
def configure
config = self.configuration
yield config
self.configuration = config
end
|
.fetch_response(idempotency_key) ⇒ Object
342
343
344
345
|
# File 'lib/mangopay.rb', line 342
def fetch_response(idempotency_key)
url = "#{api_path}/responses/#{idempotency_key}"
request(:get, url)
end
|
.get_config(name) ⇒ Object
Fetch a MangoPay configuration from the list of configs. Throw error if not found
192
193
194
195
196
|
# File 'lib/mangopay.rb', line 192
def get_config(name)
config = @configurations[name]
raise "Could not find any configuration with name '#{name}'" unless config
config
end
|
.ratelimit ⇒ Object
178
179
180
|
# File 'lib/mangopay.rb', line 178
def ratelimit
@ratelimit
end
|
.ratelimit=(obj) ⇒ Object
182
183
184
|
# File 'lib/mangopay.rb', line 182
def ratelimit=(obj)
@ratelimit = obj
end
|
.remove_config(name) ⇒ Object
198
199
200
201
|
# File 'lib/mangopay.rb', line 198
def remove_config(name)
raise "Could not find any configuration with name '#{name}'" unless @configurations[name]
@configurations[name] = nil
end
|
.request(method, url, params = {}, filters = {}, headers_or_idempotency_key = nil, before_request_proc = nil) ⇒ Object
-
method: HTTP method; lowercase symbol, e.g. :get, :post etc.
-
url: the part after Configuration#root_url
-
params: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
-
filters: hash; pagination params etc.; will encode it by URI and assign to URI#query
-
headers_or_idempotency_key: hash of headers; or replaced by request_headers if nil; or added to request_headers as idempotency key otherwise (see docs.mangopay.com/api-references/idempotency-support/)
-
before_request_proc: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
Raises MangoPay::ResponseError if response code != 200.
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/mangopay.rb', line 213
def request(method, url, params={}, filters={}, = nil, before_request_proc = nil)
uri = api_uri(url)
uri.query = URI.encode_www_form(filters) unless filters.empty?
if .is_a?(Hash)
=
else
=
['Idempotency-Key'] = if != nil
end
if configuration.
['x-tenant-id'] = 'uk'
end
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => configuration.use_ssl?, :read_timeout => configuration.http_timeout,
:max_retries => configuration.http_max_retries,
:open_timeout => configuration.http_open_timeout, ssl_version: :TLSv1_2) do |http|
req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, )
req.body = JSON.dump(params)
before_request_proc.call(req) if before_request_proc
do_request(http, req, uri)
end
raise MangoPay::ResponseError.new(uri, '408', {'Message' => 'Request Timeout'}) if res.nil?
begin
data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
rescue MultiJson::ParseError
details = {}
details['Message'] = res.body
raise MangoPay::ResponseError.new(uri, res.code, details)
end
unless res.is_a?(Net::HTTPOK) or res.is_a?(Net::HTTPCreated) or res.is_a?(Net::HTTPNoContent)
if res.is_a?(Net::HTTPUnauthorized) and res['www-authenticate'] != nil
redirect_url = res['www-authenticate'][/RedirectUrl=(.+)/, 1]
data['RedirectUrl'] = redirect_url
data['message'] = 'SCA required to perform this action.'
raise MangoPay::ResponseError.new(uri, res.code, data)
end
raise MangoPay::ResponseError.new(uri, res.code, data)
end
['x-number-of-pages', 'x-number-of-items'].each { |k|
filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
}
if res['x-ratelimit']
self.ratelimit = {
limit: res['x-ratelimit'].split(", "),
remaining: res['x-ratelimit-remaining'].split(", "),
reset: res['x-ratelimit-reset'].split(", ")
}
end
configuration.after_request_proc&.call(data)
data
end
|
.request_multipart(method, url, file, file_name, idempotency_key = nil) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/mangopay.rb', line 276
def request_multipart(method, url, file, file_name, idempotency_key = nil)
uri = api_uri(url)
=
['Idempotency-Key'] = idempotency_key if idempotency_key
['x-tenant-id'] = 'uk' if configuration.
boundary = "#{SecureRandom.hex}"
['Content-Type'] = "multipart/form-data; boundary=#{boundary}"
multipart_body = []
multipart_body << "--#{boundary}"
multipart_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{file_name}\""
multipart_body << ""
multipart_body << file
multipart_body << "--#{boundary}--"
multipart_body << ""
body = multipart_body.map { |part| part.is_a?(String) ? part.force_encoding("ASCII-8BIT") : part }.join("\r\n")
res = Net::HTTP.start(uri.host, uri.port,
use_ssl: configuration.use_ssl?,
read_timeout: configuration.http_timeout,
open_timeout: configuration.http_open_timeout,
max_retries: configuration.http_max_retries,
ssl_version: :TLSv1_2) do |http|
req_class = Net::HTTP.const_get(method.capitalize)
req = req_class.new(uri.request_uri, )
req.body = body
do_request(http, req, uri)
end
raise MangoPay::ResponseError.new(uri, '408', { 'Message' => 'Request Timeout' }) if res.nil?
begin
data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
rescue MultiJson::ParseError
raise MangoPay::ResponseError.new(uri, res.code, { 'Message' => res.body })
end
unless res.is_a?(Net::HTTPOK) || res.is_a?(Net::HTTPCreated) || res.is_a?(Net::HTTPNoContent)
if res.is_a?(Net::HTTPUnauthorized) && res['www-authenticate']
redirect_url = res['www-authenticate'][/RedirectUrl=(.+)/, 1]
data['RedirectUrl'] = redirect_url
data['message'] = 'SCA required to perform this action.'
end
raise MangoPay::ResponseError.new(uri, res.code, data)
end
if res['x-ratelimit']
self.ratelimit = {
limit: res['x-ratelimit'].split(", "),
remaining: res['x-ratelimit-remaining'].split(", "),
reset: res['x-ratelimit-reset'].split(", ")
}
end
configuration.after_request_proc&.call(data)
data
end
|
.version_code ⇒ Object
126
127
128
|
# File 'lib/mangopay.rb', line 126
def version_code
"v2.01"
end
|
.version_code_v3 ⇒ Object
130
131
132
|
# File 'lib/mangopay.rb', line 130
def version_code_v3
"V3.0"
end
|
.with_configuration(config) ⇒ Object
170
171
172
173
174
175
176
|
# File 'lib/mangopay.rb', line 170
def with_configuration(config)
original_config = MangoPay.configuration
MangoPay.configuration = config
yield
ensure
MangoPay.configuration = original_config
end
|