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/conversion.rb,
lib/mangopay/http_calls.rb,
lib/mangopay/legal_user.rb,
lib/mangopay/regulatory.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/ubo_declaration.rb,
lib/mangopay/virtual_account.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/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, KycDocument, LegalUser, Mandate, NaturalUser, PayIn, PayOut, PaymentMethodMetadata, PreAuthorization, Refund, Regulatory, Report, Resource, ResponseError, Transaction, Transfer, Ubo, UboDeclaration, User, VirtualAccount, Wallet

Constant Summary collapse

VERSION =
'3.28.1'

Class Method Summary collapse

Class Method Details

.add_config(name, config) ⇒ Object

Add MangoPay.Configuration to the list of configs



171
172
173
# File 'lib/mangopay.rb', line 171

def add_config(name, config)
  @configurations[name] = config
end

.api_pathObject



122
123
124
# File 'lib/mangopay.rb', line 122

def api_path
  "/#{version_code}/#{MangoPay.configuration.client_id}"
end

.api_path_no_clientObject



126
127
128
# File 'lib/mangopay.rb', line 126

def api_path_no_client
  "/#{version_code}"
end

.api_uri(url = '') ⇒ Object



130
131
132
# File 'lib/mangopay.rb', line 130

def api_uri(url='')
  URI(configuration.root_url + url)
end

.configurationObject



139
140
141
142
143
144
145
146
# File 'lib/mangopay.rb', line 139

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



134
135
136
137
# File 'lib/mangopay.rb', line 134

def configuration=(value)
  Thread.current[:mangopay_configuration] = value
  @last_configuration_set = value
end

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


148
149
150
151
152
# File 'lib/mangopay.rb', line 148

def configure
  config = self.configuration
  yield config
  self.configuration = config
end

.fetch_response(idempotency_key) ⇒ Object

Retrieve a previous response by idempotency_key See docs.mangopay.com/api-references/idempotency-support/



254
255
256
257
# File 'lib/mangopay.rb', line 254

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



176
177
178
179
180
# File 'lib/mangopay.rb', line 176

def get_config(name)
  config = @configurations[name]
  raise "Could not find any configuration with name '#{name}'" unless config
  config
end

.ratelimitObject



162
163
164
# File 'lib/mangopay.rb', line 162

def ratelimit
  @ratelimit
end

.ratelimit=(obj) ⇒ Object



166
167
168
# File 'lib/mangopay.rb', line 166

def ratelimit=(obj)
  @ratelimit = obj
end

.remove_config(name) ⇒ Object



182
183
184
185
# File 'lib/mangopay.rb', line 182

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
# File 'lib/mangopay.rb', line 197

def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil, before_request_proc = nil)
  uri = api_uri(url)
  uri.query = URI.encode_www_form(filters) unless filters.empty?

  if headers_or_idempotency_key.is_a?(Hash)
    headers = headers_or_idempotency_key
  else
    headers = request_headers
    headers['Idempotency-Key'] = headers_or_idempotency_key if headers_or_idempotency_key != nil
  end

  if configuration.uk_header_flag
    headers['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, headers)
    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?

  # decode json data
  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)
    raise MangoPay::ResponseError.new(uri, res.code, data)
  end

  # copy pagination info if any
  ['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

  data
end

.version_codeObject



118
119
120
# File 'lib/mangopay.rb', line 118

def version_code
  "v2.01"
end

.with_configuration(config) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/mangopay.rb', line 154

def with_configuration(config)
  original_config = MangoPay.configuration
  MangoPay.configuration = config
  yield
ensure
  MangoPay.configuration = original_config
end