mangopay 3.0.25 → 3.0.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +30 -28
- data/.rspec +2 -2
- data/.travis.yml +13 -4
- data/Gemfile +2 -2
- data/LICENSE +20 -20
- data/README.md +126 -126
- data/bin/mangopay +9 -9
- data/lib/generators/mangopay/install_generator.rb +60 -60
- data/lib/generators/templates/mangopay.rb.erb +5 -5
- data/lib/mangopay.rb +228 -225
- data/lib/mangopay/authorization_token.rb +88 -88
- data/lib/mangopay/bank_account.rb +38 -38
- data/lib/mangopay/bankingaliases.rb +29 -0
- data/lib/mangopay/bankingaliases_iban.rb +16 -0
- data/lib/mangopay/card.rb +8 -8
- data/lib/mangopay/card_registration.rb +9 -9
- data/lib/mangopay/client.rb +74 -74
- data/lib/mangopay/dispute.rb +130 -130
- data/lib/mangopay/errors.rb +61 -61
- data/lib/mangopay/event.rb +18 -18
- data/lib/mangopay/filter_parameters.rb +46 -46
- data/lib/mangopay/hook.rb +9 -9
- data/lib/mangopay/http_calls.rb +85 -85
- data/lib/mangopay/json.rb +14 -14
- data/lib/mangopay/kyc_document.rb +70 -70
- data/lib/mangopay/legal_user.rb +15 -15
- data/lib/mangopay/mandate.rb +32 -32
- data/lib/mangopay/natural_user.rb +14 -14
- data/lib/mangopay/pay_in.rb +96 -85
- data/lib/mangopay/pay_out.rb +14 -14
- data/lib/mangopay/pre_authorization.rb +13 -13
- data/lib/mangopay/refund.rb +7 -7
- data/lib/mangopay/report.rb +17 -17
- data/lib/mangopay/resource.rb +21 -21
- data/lib/mangopay/transaction.rb +24 -24
- data/lib/mangopay/transfer.rb +9 -9
- data/lib/mangopay/user.rb +43 -43
- data/lib/mangopay/version.rb +3 -3
- data/lib/mangopay/wallet.rb +17 -17
- data/mangopay.gemspec +30 -30
- data/spec/mangopay/authorization_token_spec.rb +70 -70
- data/spec/mangopay/bank_account_spec.rb +97 -97
- data/spec/mangopay/bankingaliases_spec.rb +29 -0
- data/spec/mangopay/card_registration_spec.rb +73 -73
- data/spec/mangopay/client_spec.rb +110 -110
- data/spec/mangopay/configuration_spec.rb +95 -95
- data/spec/mangopay/dispute_spec.rb +262 -262
- data/spec/mangopay/event_spec.rb +31 -31
- data/spec/mangopay/fetch_filters_spec.rb +63 -63
- data/spec/mangopay/hook_spec.rb +37 -37
- data/spec/mangopay/idempotency_spec.rb +41 -41
- data/spec/mangopay/kyc_document_spec.rb +103 -103
- data/spec/mangopay/log_requests_filter_spec.rb +25 -25
- data/spec/mangopay/mandate_spec.rb +92 -92
- data/spec/mangopay/payin_bankwire_direct_spec.rb +74 -74
- data/spec/mangopay/payin_card_direct_spec.rb +68 -68
- data/spec/mangopay/payin_card_web_spec.rb +47 -38
- data/spec/mangopay/payin_directdebit_direct_spec.rb +37 -37
- data/spec/mangopay/payin_directdebit_web_spec.rb +38 -38
- data/spec/mangopay/payin_paypal_web_spec.rb +38 -38
- data/spec/mangopay/payin_preauthorized_direct_spec.rb +68 -68
- data/spec/mangopay/payout_bankwire_spec.rb +54 -54
- data/spec/mangopay/preauthorization_spec.rb +42 -42
- data/spec/mangopay/refund_spec.rb +21 -21
- data/spec/mangopay/report_spec.rb +39 -39
- data/spec/mangopay/shared_resources.rb +397 -381
- data/spec/mangopay/transaction_spec.rb +54 -54
- data/spec/mangopay/transfer_spec.rb +69 -69
- data/spec/mangopay/user_spec.rb +137 -137
- data/spec/mangopay/wallet_spec.rb +80 -80
- data/spec/spec_helper.rb +31 -31
- metadata +7 -6
- data/lib/mangopay/temp.rb +0 -74
- 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
|
-
|
9
|
-
|
10
|
-
require 'mangopay/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
autoload :
|
17
|
-
autoload :
|
18
|
-
autoload :
|
19
|
-
autoload :
|
20
|
-
autoload :
|
21
|
-
autoload :
|
22
|
-
autoload :
|
23
|
-
autoload :
|
24
|
-
autoload :
|
25
|
-
autoload :
|
26
|
-
autoload :
|
27
|
-
autoload :
|
28
|
-
autoload :
|
29
|
-
autoload :
|
30
|
-
autoload :
|
31
|
-
autoload :
|
32
|
-
autoload :
|
33
|
-
autoload :
|
34
|
-
autoload :
|
35
|
-
autoload :
|
36
|
-
autoload :
|
37
|
-
autoload :
|
38
|
-
autoload :
|
39
|
-
autoload :
|
40
|
-
|
41
|
-
|
42
|
-
autoload :
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
MangoPay.configuration =
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
#
|
103
|
-
# - +
|
104
|
-
# - +
|
105
|
-
# - +
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
headers
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
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
|
-
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
|