allorails 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,443 +0,0 @@
1
- require 'net/http'
2
- require 'json'
3
-
4
- module Allorails
5
- module Response
6
-
7
- class ApiResponse
8
-
9
- ##
10
- # Constructor
11
- # @param signature (string) Expected response signature
12
- # @param headers (list) Response HTTP headers
13
- # @param body (string) Raw response data
14
- def initialize(signature, headers, body)
15
- @_signature = signature
16
- @_headers = headers
17
- @_body = body
18
- end
19
-
20
- ##
21
- # Internal method checking the response
22
- # @throws ApiFalseResponseSignature If the expected signature is not found among response headers
23
- def _verify
24
- raise Allorails::ApiFalseResponseSignatureError unless @_headers['x-allopass-response-signature'].include?(@_signature)
25
- end
26
-
27
- end # -- end class ApiResponse
28
-
29
-
30
- class JsonNode
31
-
32
- attr_reader :children
33
- attr_reader :attributes
34
-
35
- def initialize(json_obj)
36
- @attributes = json_obj.delete('@attributes') || {}
37
- @children = {}
38
- json_obj.each_pair do |k, v|
39
- @children[k] = v.is_a?(Hash) ? JsonNode.new(v) : (v.is_a?(Array) ? v.map{|el| JsonNode.new(el)} : v)
40
- end
41
- end
42
-
43
- def method_missing(m, *args, &block)
44
- m = m.to_s
45
- return attributes[m] if attributes.has_key?(m)
46
- return children[m] if children.has_key?(m)
47
- return nil
48
- end
49
-
50
- end
51
-
52
-
53
- class ApiMappingResponse < ApiResponse
54
-
55
- attr_reader :json
56
-
57
- ##
58
- # Constructor
59
- # @param signature (string) Expected response signature
60
- # @param headers (list) Response HTTP headers
61
- # @param body (string) Raw response data
62
- def initialize(signature, headers, body)
63
- super(signature, headers, body)
64
- begin
65
- @json = JsonNode.new(JSON.parse(body)['response'] || {})
66
- rescue JSON::ParserError => e
67
- raise Allorails::ApiWrongFormatResponseError
68
- end
69
- _verify
70
- end
71
-
72
- ##
73
- # Internal method checking the response
74
- # @throws ApiFalseResponseSignature If the expected signature is not found among response headers
75
- def _verify
76
- if json.code.nil? || json.code.to_i != 0
77
- raise Allorails::ApiRemoteErrorError
78
- end
79
- super
80
- end
81
-
82
- ##
83
- # Internal method allowing easy reading of JSON nodes
84
- # @param (sym) name of the (top-level) node that needs to be read
85
- # @param (Class) type to which the result should be cast
86
- def self.node_reader(node_name, type = String)
87
- # define a Proc to cast the node's text to the specified type
88
- cast = case true
89
- when type == String then Proc.new{|x| x.to_s}
90
- when type == Integer then Proc.new{|x| x.to_i}
91
- when type == Float then Proc.new{|x| x.to_f}
92
- when type == DateTime then Proc.new{|x| x.date.nil? ? (x.timestamp.nil? ? nil : DateTime.strptime(x.timestamp, "%s")) : DateTime.strptime(x.date)}
93
- else Proc.new{|x| type.new x}
94
- end
95
- # add a method accessing the node and casting the text
96
- send :define_method, node_name do
97
- result = json.send(node_name)
98
- return cast.call(result) unless result.nil?
99
- end
100
- end
101
-
102
- end
103
-
104
-
105
- class OnetimePricingResponse < ApiMappingResponse
106
-
107
- ## Provides the creation date
108
- # @return (DateTime) Creation date
109
- node_reader :creation_date, DateTime
110
-
111
- ## Provides the customer ip
112
- # @return (string) customer ip
113
- node_reader :customer_ip
114
-
115
- ## Provides the customer country
116
- # @return (string) customer country
117
- node_reader :customer_country
118
-
119
- ## Provides the website
120
- # @return (Website) website
121
- node_reader :website, Website
122
-
123
- ## Provides the pricepoints by countries
124
- # @return (Array) available countries (list of Country object)
125
- def countries
126
- countries = []
127
- json.countries.children.values.each{|r| countries += r.children.values.map{|c| ::Allorails::Country.new(c)}}
128
- countries
129
- end
130
-
131
- ## Provides the pricepoints by region
132
- # @return (Array) available regions (list of Regions object)
133
- def regions
134
- json.countries.children.values.map{|r| ::Allorails::Region.new(r)}
135
- end
136
-
137
- ## Provides the pricepoints by markets
138
- # @return (list) available markets (list of Market object)
139
- def markets
140
- json.markets.children.values.map{|m| ::Allorails::Market.new(m)}
141
- end
142
-
143
- end
144
-
145
-
146
- ## Class defining a onetime validate-codes request's response
147
- class OnetimeValidateCodesResponse < ApiMappingResponse
148
- ## The validation is successful
149
- VALIDATESCODES_SUCCESS = 0
150
-
151
- ## The validation failed
152
- VALIDATESCODES_FAILED = 1
153
-
154
- ## Provides the validation status
155
- # @return (int) validation status
156
- node_reader :status, Integer
157
-
158
- ## Provides the validation status description
159
- # @return (string) validation status description
160
- node_reader :status_description
161
-
162
- ## Provides access type
163
- # @return (string) access type
164
- node_reader :access_type
165
-
166
- ## Provides the transaction id
167
- # @return (string) transaction id
168
- node_reader :transaction_id
169
-
170
- ## Provides price information
171
- # @return (Price) price information
172
- node_reader :price, ::Allorails::Price
173
-
174
- ## Provides paid price information
175
- # @return (Price) paid price information
176
- node_reader :paid, ::Allorails::Price
177
-
178
- ## Provides the validation date
179
- # @return (datetime.datetime) validation date
180
- node_reader :validation_date, DateTime
181
-
182
- ## Provides the product name
183
- # @return (string) product name
184
- node_reader :product_name
185
-
186
- ## Provides the website
187
- # @return (Website) website
188
- node_reader :website, Website
189
-
190
- ## Provides the customer ip
191
- # @return (string) customer ip
192
- node_reader :customer_ip
193
-
194
- ## Provides the customer country
195
- # @return (string) customer country
196
- node_reader :customer_country
197
-
198
- ## Provides the expected number of codes
199
- # @return (int) expected number of codes
200
- node_reader :expected_number_of_codes, Integer
201
-
202
- ## Provides the codes you tried to validate
203
- # @return (Array) list of Code objects
204
- def codes
205
- json.codes.children.values.map{|c| ::Allorails::Code.new(c)}
206
- end
207
-
208
- ## Provides the merchant transaction id
209
- # @return (string) merchant transaction id
210
- node_reader :merchant_transaction_id
211
-
212
- ## Provides the client data
213
- # @return (string) client data
214
- node_reader :data
215
-
216
- ## Provides the affiliation code
217
- # @return (string) affiliation code
218
- node_reader :affiliate
219
-
220
- ## Provides information about the associated partners
221
- # @return (list) partners information (list of Partner objects)
222
- def partners
223
- json.partners.children.values.map{|c| ::Allorails::Partner.new(c)}
224
- end
225
-
226
- end
227
-
228
-
229
- ## Class defining a product detail request's response
230
- class ProductDetailResponse < ApiMappingResponse
231
-
232
- ## Provides the product id
233
- # @return (int) product id
234
- node_reader :id, Integer
235
-
236
- ## Provides the product key
237
- # @return (string) product key
238
- node_reader :key
239
-
240
- ## Provides access type
241
- # @return (string) access type
242
- node_reader :access_type
243
-
244
- ## Provides the creation date
245
- # @return (datetime.datetime) Creation date
246
- node_reader :creation_date, DateTime
247
-
248
- ## Provides the product name
249
- # @return (string) product name
250
- node_reader :name
251
-
252
- ## Provides the website
253
- # @return (Website) website
254
- node_reader :website, Website
255
-
256
- ## Provides the expected number of codes
257
- # @return (int) expected number of codes
258
- node_reader :expected_number_of_codes, Integer
259
-
260
- ## Provides the purchase url
261
- # @return (string) purchase url
262
- node_reader :purchase_url
263
-
264
- ## Provides the forward url
265
- # @return (string) forward url
266
- node_reader :forward_url
267
-
268
- ## Provides the error url
269
- # @return (string) error url
270
- node_reader :error_url
271
-
272
- ## Provides the notification url
273
- # @return (string) notification url
274
- node_reader :notification_url
275
-
276
- end
277
-
278
-
279
- ## Class defining a transaction prepare request's response
280
- class TransactionPrepareResponse < ApiMappingResponse
281
-
282
- ## Provides access type
283
- # @return (string) access type
284
- node_reader :access_type
285
-
286
- ## Provides the transaction id
287
- # @return (string) transaction id
288
- node_reader :transaction_id
289
-
290
- ## Provides the creation date
291
- # @return (datetime.datetime) Creation date
292
- node_reader :creation_date, DateTime
293
-
294
- ## Provides price information
295
- # @return (Price) price information
296
- node_reader :price, ::Allorails::Price
297
-
298
- ## Provides information about the pricepoint
299
- # @return (Pricepoint) pricepoint information
300
- node_reader :pricepoint, ::Allorails::Pricepoint
301
-
302
- ## Provides the website
303
- # @return (Website) website
304
- node_reader :website, Website
305
-
306
- ## Provides the buy url
307
- # @return (string) buy url
308
- node_reader :buy_url
309
-
310
- ## Provides the checkout button
311
- # @return (string) checkout button (html code)
312
- node_reader :checkout_button
313
-
314
- end
315
-
316
-
317
- ## Class defining a transaction detail request's response
318
- class TransactionDetailResponse < ApiMappingResponse
319
-
320
- ## The transaction is at first step : initialization
321
- INIT = -1
322
-
323
- ## The transaction is successful
324
- SUCCESS = 0
325
-
326
- ## The transaction failed due to insufficient funds
327
- INSUFFICIENT_FUNDS = 1
328
-
329
- ## The transaction timeouted
330
- TIMEOUT = 2
331
-
332
- ## The transaction has been cancelled by user
333
- CANCELLED = 3
334
-
335
- ## The transaction has been blocked due to fraud suspicions
336
- ANTI_FRAUD = 4
337
-
338
- ## Provides the transaction status
339
- # @return (int) transaction status
340
- node_reader :status, Integer
341
-
342
- ## Provides the validation status description
343
- # @return (string) validation status description
344
- node_reader :status_description
345
-
346
- ## Provides access type
347
- # @return (string) access type
348
- node_reader :access_type
349
-
350
- ## Provides the tansaction id
351
- # @return (string) transaction id
352
- node_reader :transaction_id
353
-
354
- ## Provides price information
355
- # @return (Price) price information
356
- node_reader :price, ::Allorails::Price
357
-
358
- ## Provides paid price information
359
- # @return (Price) paid price information
360
- node_reader :paid, ::Allorails::Price
361
-
362
- ## Provides the creation date
363
- # @return (datetime.datetime) Creation date
364
- node_reader :creation_date, DateTime
365
-
366
- ## Provides the end date
367
- # @return (datetime.datetime) end date
368
- node_reader :end_date, DateTime
369
-
370
- ## Provides the product name
371
- # @return (string) product name
372
- node_reader :product_name
373
-
374
- ## Provides the customer ip
375
- # @return (string) customer ip
376
- node_reader :customer_ip
377
-
378
- ## Provides the customer country
379
- # @return (string) customer country
380
- node_reader :customer_country
381
-
382
- ## Provides the expected number of codes
383
- # @return (int) expected number of codes
384
- node_reader :expected_number_of_codes, Integer
385
-
386
- ## Provides the codes associated with the transaction
387
- # @return (list) list of codes (list of string)
388
- def codes
389
- json.codes.children.values.map{|c| ::Allorails::Code.new(c)}
390
- end
391
-
392
- ## Provides the merchant transaction id
393
- # @return (string) merchant transaction id
394
- node_reader :merchant_transaction_id
395
-
396
- ## Provides the client data
397
- # @return (string) client data
398
- node_reader :data
399
-
400
- ## Provides the affiliation code
401
- # @return (string) affiliation code
402
- node_reader :affiliate
403
-
404
- ## Provides information about the associated partners
405
- # @return (list) partners information (list of Partner objects)
406
- def partners
407
- json.partners.children.values.map{|c| ::Allorails::Partner.new(c)}
408
- end
409
-
410
- end
411
-
412
-
413
- ## Class defining a onetime button request's response
414
- class OnetimeButtonResponse < ApiMappingResponse
415
-
416
- ## Provides access type
417
- # @return (string) access type
418
- node_reader :access_type
419
-
420
- ## Provides the button id
421
- # @return (string) button id
422
- node_reader :button_id
423
-
424
- ## Provides the creation date
425
- # @return (datetime.datetime) Creation date
426
- node_reader :creation_date, DateTime
427
-
428
- ## Provides the website
429
- # @return (Website) website
430
- node_reader :website, Website
431
-
432
- ## Provides the buy url
433
- # @return (string) buy url
434
- node_reader :buy_url
435
-
436
- ## Provides the checkout button
437
- # @return (string) checkout button (html code)
438
- node_reader :checkout_button
439
-
440
- end
441
-
442
- end #-- end module Allorails::Response
443
- end #-- end module Allorails
@@ -1,112 +0,0 @@
1
- require 'allorails'
2
- require 'yaml'
3
-
4
- # load Allorails configuration options for testing
5
- #
6
- # to be able to run these tests, create a test/test-conf.yml file,
7
- # based on test/test-conf-sample.yml
8
- # but using your own API credentials
9
- Allorails.config(YAML.load File.read(File.join(File.dirname(__FILE__), 'test-conf.yml')))
10
-
11
-
12
- # some additional parameters for the tests
13
- YOUR_SITE_ID = 281629
14
- TEST_COUNTRY_CODE = 'UK'
15
-
16
-
17
- # run the tests
18
-
19
- describe Allorails::Request::ApiRequest, "#init" do
20
- it "starts" do
21
- req = Allorails::Request::ApiRequest.new(params = {}, mapping = true, email_account = nil)
22
- req.should_not be_nil
23
- end
24
- end
25
-
26
- describe Allorails::Api, "#init" do
27
- it "initializes with email=nil" do
28
- api = Allorails::Api.new
29
- end
30
- end
31
-
32
- describe Allorails::Api, "#get_onetime_pricing" do
33
- api = Allorails::Api.new
34
- it "returns a valid response" do
35
- resp = api.get_onetime_pricing({'site_id' => YOUR_SITE_ID, 'country' => TEST_COUNTRY_CODE})
36
- resp.to_s.should_not be_nil
37
-
38
- resp.creation_date.is_a?(DateTime).should be_true
39
- resp.website.is_a?(Allorails::Website).should be_true
40
- resp.regions.is_a?(Array).should be_true
41
- resp.regions.each do |reg|
42
- reg.is_a?(Allorails::Region).should be_true
43
- reg.countries.is_a?(Array).should be_true
44
- reg.countries.each do |ctry|
45
- ctry.is_a?(Allorails::Country).should be_true
46
- (ctry.code.length > 0).should be_true
47
- end
48
- end
49
-
50
- resp.markets.is_a?(Array).should be_true
51
- resp.markets.each do |m|
52
- m.is_a?(Allorails::Market).should be_true
53
- m.country_code.is_a?(String).should be_true
54
- m.country.is_a?(String).should be_true
55
- m.pricepoints.is_a?(Array).should be_true
56
- m.pricepoints.each do |pp|
57
- pp.is_a?(Allorails::Pricepoint).should be_true
58
- pp.price.is_a?(Allorails::Price).should be_true
59
- pp.payout.is_a?(Allorails::Payout).should be_true
60
-
61
- [pp.price, pp.payout].each do |ppp|
62
- ppp.currency.is_a?(String).should be_true
63
- ppp.amount.is_a?(Float).should be_true
64
- ppp.exchange.is_a?(Float).should be_true
65
- ppp.reference_currency.is_a?(String).should be_true
66
- ppp.reference_amount.is_a?(Float).should be_true
67
- end
68
-
69
- end
70
- end
71
- end
72
- it "works with symbol keys and values" do
73
- resp = api.get_onetime_pricing({:site_id => YOUR_SITE_ID, :country => TEST_COUNTRY_CODE.to_sym})
74
- resp.creation_date.is_a?(DateTime).should be_true
75
- end
76
- end
77
-
78
- describe Allorails::Api, "#create_discrete_button" do
79
- api = Allorails::Api.new
80
- resp = api.create_discrete_button({
81
- 'site_id' => YOUR_SITE_ID,
82
- 'product_name' => 'TEST-DISCRETE-BUTTON',
83
- 'forward_url' => 'http://any-test.url/is?good',
84
- 'price_mode' => 'price',
85
- 'amount' => 0.99,
86
- 'price_policy' => 'high-preferred',
87
- 'reference_currency' => 'EUR'
88
- })
89
- it "returns a valid response" do
90
- resp.to_s.should_not be_nil
91
- resp.is_a?(Allorails::Response::OnetimeButtonResponse).should be_true
92
- (resp.json).should_not be_nil
93
- # puts resp.json.inspect #DEBUG
94
- resp.button_id.should_not be_nil
95
- resp.website.is_a?(Allorails::Website).should be_true
96
- end
97
- end
98
-
99
- describe Allorails::Api, "#validate_codes" do
100
- api = Allorails::Api.new
101
- resp = api.validate_codes(
102
- 'site_id' => YOUR_SITE_ID,
103
- 'code' => ['X226658Z'],
104
- 'product_name' => 'zeproduct'
105
- )
106
- it "returns a valid response" do
107
- resp.to_s.should_not be_nil
108
- resp.is_a?(Allorails::Response::OnetimeValidateCodesResponse).should be_true
109
- (resp.json).should_not be_nil
110
- #puts resp.json.inspect #DEBUG
111
- end
112
- end
@@ -1,11 +0,0 @@
1
- accounts:
2
- your@email.com:
3
- api_key: 9696ba35ebb3dc6c11c964c269f262a6
4
- private_key: 50372be654ecacf9dbb1b431535f21d0
5
-
6
- default_hash: sha1
7
- default_format: json
8
- network_timeout: 20
9
- network_protocol: http
10
- network_port: 80
11
- host: pgeoffroy.api.payment.allopass.dev
data/test/test-conf.yml DELETED
@@ -1,12 +0,0 @@
1
- accounts:
2
- jonathan@feeligo.com:
3
- api_key: d33102f78bea414ed098e5cd0bf17632
4
- private_key: 2670c0094d163a718ea44f5459114301
5
-
6
-
7
- default_hash: sha1
8
- default_format: json
9
- network_timeout: 20
10
- network_protocol: http
11
- network_port: 80
12
- host: api.allopass.com