klarna-xmlrpc 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +13 -0
  3. data/Gemfile +14 -0
  4. data/Guardfile +16 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.textile +139 -0
  7. data/Rakefile +22 -0
  8. data/TODO +30 -0
  9. data/examples/Gemfile +8 -0
  10. data/examples/config/initializer.rb +15 -0
  11. data/examples/console.rb +71 -0
  12. data/examples/public/images/klarna.png +0 -0
  13. data/examples/public/images/ruby.png +0 -0
  14. data/examples/views/_address.haml +22 -0
  15. data/examples/views/_articles.haml +21 -0
  16. data/examples/views/checkout_page_example.haml +2 -0
  17. data/examples/views/essential/add_transaction/_form.haml +32 -0
  18. data/examples/views/essential/add_transaction/result.haml +7 -0
  19. data/examples/views/essential/calculate_monthly_cost/_form.haml +16 -0
  20. data/examples/views/essential/calculate_monthly_cost/result.haml +7 -0
  21. data/examples/views/essential/get_addresses/_form.haml +9 -0
  22. data/examples/views/essential/get_addresses/result.haml +8 -0
  23. data/examples/views/index.haml +296 -0
  24. data/examples/views/layout.haml +48 -0
  25. data/examples/views/payment_terms_example.haml +102 -0
  26. data/examples/views/product_page_example.haml +2 -0
  27. data/examples/views/reservation/activate_reservation/_form.haml +54 -0
  28. data/examples/views/reservation/activate_reservation/result.haml +7 -0
  29. data/examples/views/reservation/cancel_reservation/_form.haml +8 -0
  30. data/examples/views/reservation/cancel_reservation/result.haml +7 -0
  31. data/examples/views/reservation/change_reservation/_form.haml +10 -0
  32. data/examples/views/reservation/change_reservation/result.haml +7 -0
  33. data/examples/views/reservation/reserve_amount/_form.haml +58 -0
  34. data/examples/views/reservation/reserve_amount/result.haml +7 -0
  35. data/examples/views/reservation/reserve_ocr_numbers/_form.haml +8 -0
  36. data/examples/views/reservation/reserve_ocr_numbers/result.haml +7 -0
  37. data/examples/views/reservation/split_reservation/_form.haml +14 -0
  38. data/examples/views/reservation/split_reservation/result.haml +7 -0
  39. data/examples/views/special/get_pclasses/_form.haml +8 -0
  40. data/examples/views/special/get_pclasses/result.haml +7 -0
  41. data/examples/views/special/invoice_address/_form.haml +8 -0
  42. data/examples/views/special/invoice_address/result.haml +7 -0
  43. data/examples/views/special/invoice_amount/_form.haml +8 -0
  44. data/examples/views/special/invoice_amount/result.haml +7 -0
  45. data/examples/views/special/is_invoice_paid/_form.haml +8 -0
  46. data/examples/views/special/is_invoice_paid/result.haml +7 -0
  47. data/examples/views/special/update_charge_amount/_form.haml +15 -0
  48. data/examples/views/special/update_charge_amount/result.haml +7 -0
  49. data/examples/views/special/update_goods_quantity/_form.haml +17 -0
  50. data/examples/views/special/update_goods_quantity/result.haml +7 -0
  51. data/examples/views/special/update_order_number/_form.haml +10 -0
  52. data/examples/views/special/update_order_number/result.haml +7 -0
  53. data/examples/views/useful/activate_invoice/_form.haml +9 -0
  54. data/examples/views/useful/activate_invoice/result.haml +7 -0
  55. data/examples/views/useful/credit_invoice/_form.haml +11 -0
  56. data/examples/views/useful/credit_invoice/result.haml +7 -0
  57. data/examples/views/useful/delete_invoice/_form.haml +9 -0
  58. data/examples/views/useful/delete_invoice/result.haml +7 -0
  59. data/examples/views/useful/email_invoice/_form.haml +9 -0
  60. data/examples/views/useful/email_invoice/result.haml +7 -0
  61. data/examples/views/useful/has_account/_form.haml +9 -0
  62. data/examples/views/useful/has_account/result.haml +7 -0
  63. data/examples/views/useful/return_amount/_form.haml +15 -0
  64. data/examples/views/useful/return_amount/result.haml +7 -0
  65. data/examples/views/useful/send_invoice/_form.haml +9 -0
  66. data/examples/views/useful/send_invoice/result.haml +7 -0
  67. data/examples/web.rb +349 -0
  68. data/klarna.gemspec +34 -0
  69. data/lib/klarna.rb +171 -0
  70. data/lib/klarna/api.rb +187 -0
  71. data/lib/klarna/api/client.rb +126 -0
  72. data/lib/klarna/api/constants.rb +647 -0
  73. data/lib/klarna/api/errors.rb +154 -0
  74. data/lib/klarna/api/methods.rb +16 -0
  75. data/lib/klarna/api/methods/cost_calculations.rb +134 -0
  76. data/lib/klarna/api/methods/invoicing.rb +304 -0
  77. data/lib/klarna/api/methods/reservation.rb +188 -0
  78. data/lib/klarna/api/methods/standard.rb +126 -0
  79. data/lib/klarna/version.rb +5 -0
  80. data/test/fixtures/api/companies.yml +97 -0
  81. data/test/fixtures/api/pclasses.yml +37 -0
  82. data/test/fixtures/api/persons.yml +144 -0
  83. data/test/fixtures/api/stores.yml +6 -0
  84. data/test/fixtures/klarna.yml +10 -0
  85. data/test/klarna/api/client_test.rb +288 -0
  86. data/test/klarna/api/errors_test.rb +34 -0
  87. data/test/klarna/api/methods/cost_calculations_test.rb +78 -0
  88. data/test/klarna/api/methods/invoicing_test.rb +409 -0
  89. data/test/klarna/api/methods/reservation_test.rb +66 -0
  90. data/test/klarna/api/methods/standard_test.rb +244 -0
  91. data/test/klarna/api_test.rb +184 -0
  92. data/test/klarna_test.rb +204 -0
  93. data/test/support/assertions_helper.rb +40 -0
  94. data/test/test_helper.rb +55 -0
  95. metadata +335 -0
@@ -0,0 +1,144 @@
1
+ # Here are Klarnas test PERSONS.
2
+ # All approved persons have unlimited credit and the system will never accept purchases from the not approved ones.
3
+ #
4
+ # Purchases done with the test persons will be displayed in Klarna Online as regular invoices, although they will
5
+ # unvisible unless you have "Yes" selected next to "Display test invoices" when searching for invoices.
6
+ #
7
+ # We will not pay for the invoices created with the test persons nor companies and we will not collect any fees as well.
8
+
9
+ persons:
10
+ se:
11
+ approved:
12
+ pno: "4304158399"
13
+ first_name: "Karl"
14
+ last_name: "Lidin"
15
+ street_address: "Junibackg. 42"
16
+ postal_code: "23634"
17
+ city: "Hollviken"
18
+ country: "SE"
19
+ cell_phone: "076 526 00 00"
20
+ currency: "SEK"
21
+ language: "SV"
22
+ unapproved:
23
+ pno: "5311096845"
24
+ first_name: "Maud"
25
+ last_name: "Johansson"
26
+ street_address: "Köpmansgatan 7"
27
+ postal_code: "12149"
28
+ city: "Johanneshov"
29
+ country: "SE"
30
+ cell_phone: "076 526 00 00"
31
+ currency: "SEK"
32
+ language: "SV"
33
+
34
+ no:
35
+ approved:
36
+ pno: "18106500157"
37
+ first_name: "Petter"
38
+ last_name: "Testmann"
39
+ street_address: "Hundremeterskogen 100"
40
+ postal_code: "0563"
41
+ city: "Oslo"
42
+ country: "NO"
43
+ cell_phone: "40 123 456"
44
+ currency: "NOK"
45
+ language: "NO"
46
+ unapproved:
47
+ pno: "18106500076"
48
+ first_name: "Petra"
49
+ last_name: "Testdame"
50
+ street_address: "Sofienberggate 56"
51
+ postal_code: "0563"
52
+ city: "Oslo"
53
+ country: "NO"
54
+ cell_phone: "40 123 456"
55
+ currency: "NOK"
56
+ language: "NO"
57
+
58
+ dk:
59
+ approved:
60
+ pno: "0505610059"
61
+ first_name: "Rasmus Jens-Peter"
62
+ last_name: "Lybert"
63
+ street_address: "Godthåbvej 8,-2"
64
+ postal_code: "3900"
65
+ city: "Godthåb"
66
+ country: "DK"
67
+ cell_phone: "20 123 456"
68
+ currency: "DKK"
69
+ language: "DA"
70
+ unapproved:
71
+ # N/A
72
+
73
+ # fi:
74
+ # approved:
75
+ # pno: "230246-8989"
76
+ # first_name: "Suvi"
77
+ # last_name: "Aurinkoinen"
78
+ # street_address: "Planeettatie 2"
79
+ # postal_code: "01450"
80
+ # city: "Vantaa"
81
+ # country: "FI"
82
+ # cell_phone: "0401234567
83
+ # currency: "EUR"
84
+ # language: "FI"
85
+ # unapproved:
86
+ # pno: "010130-887T"
87
+ # first_name: "Mikael"
88
+ # last_name: "Miehinen"
89
+ # street_address: "Tikkuritie 11"
90
+ # postal_code: "01370"
91
+ # city: "Vantaa"
92
+ # country: "FI"
93
+ # cell_phone: "0401234567"
94
+ # currency: "EUR"
95
+ # language: "FI"
96
+ #
97
+ de:
98
+ approved:
99
+ pno: "070719601"
100
+ first_name: "Uno"
101
+ last_name: "Eins"
102
+ street_address: "Hellersbergstrasse 14"
103
+ postal_code: "41460"
104
+ city: "Neuss"
105
+ country: "DE"
106
+ cell_phone: "015 2211 3356"
107
+ currency: "EUR"
108
+ language: "DE"
109
+ unapproved:
110
+ pno: "07071950"
111
+ first_name: "Uno"
112
+ last_name: "Vier"
113
+ street_address: "Hellersbergstrasse 14"
114
+ postal_code: "41460"
115
+ city: "Neuss"
116
+ cell_phone: "015 2211 3356"
117
+ country: "DE"
118
+ currency: "EUR"
119
+ language: "DE"
120
+
121
+ nl:
122
+ approved:
123
+ pno:
124
+ first_name: "Test"
125
+ last_name: "Persoon"
126
+ street_address: "Neherkade 1"
127
+ postal_code: "2521VA"
128
+ city: "Gravenhage"
129
+ country: "NL"
130
+ cell_phone: "0612345678"
131
+ currency: "EUR"
132
+ language: "NL"
133
+ unapproved:
134
+ # Only a few details available.
135
+ pno:
136
+ first_name:
137
+ last_name:
138
+ street_address:
139
+ postal_code:
140
+ city:
141
+ country: "NL"
142
+ cell_phone: "0612345678"
143
+ currency: "EUR"
144
+ language: "NL"
@@ -0,0 +1,6 @@
1
+ # Public test stores.
2
+
3
+ stores:
4
+ test:
5
+ id: 2
6
+ secret: lakrits
@@ -0,0 +1,10 @@
1
+ ---
2
+ test:
3
+ store_id: 2
4
+ store_secred: lakrits
5
+ store_pclasses:
6
+
7
+ production:
8
+ store_id: 2
9
+ store_secred: lakrits
10
+ store_pclasses:
@@ -0,0 +1,288 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ describe Klarna::API::Client do
5
+
6
+ before do
7
+ Klarna.reset!
8
+ @client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET)
9
+ end
10
+
11
+ describe 'Initialization' do
12
+ it 'should require store-ID and store-secret' do
13
+ assert_raises(Klarna::API::Errors::KlarnaCredentialsError) do
14
+ @client = Klarna::API::Client.new
15
+ end
16
+
17
+ assert_raises(Klarna::API::Errors::KlarnaCredentialsError) do
18
+ @client = Klarna::API::Client.new(VALID_STORE_ID)
19
+ end
20
+
21
+ assert @client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET)
22
+ end
23
+ end
24
+
25
+ describe "Configuration" do
26
+ describe "Configurable" do
27
+
28
+ describe '#store_id' do
29
+ it 'should be defined' do
30
+ assert_respond_to @client, :store_id
31
+ end
32
+ end
33
+
34
+ describe '#store_secret' do
35
+ it 'should be defined' do
36
+ assert_respond_to @client, :store_secret
37
+ end
38
+ end
39
+
40
+ describe '#mode' do
41
+ it 'should be defined' do
42
+ assert_respond_to @client, :mode
43
+ end
44
+
45
+ it 'should have default value: :test' do
46
+ assert_equal :test, Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET).mode
47
+ end
48
+
49
+ it 'should use default value unless specified' do
50
+ swap Klarna, :mode => :test do
51
+ client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET)
52
+ assert_equal :test, client.mode
53
+ end
54
+
55
+ swap Klarna, :mode => :production do
56
+ client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET)
57
+ assert_equal :production, client.mode
58
+ end
59
+ end
60
+
61
+ it 'could override default value' do
62
+ swap Klarna, :mode => :test do
63
+ client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET, :mode => :production)
64
+ assert_equal :production, client.mode
65
+ end
66
+
67
+ swap Klarna, :mode => :production do
68
+ client = Klarna::API::Client.new(VALID_STORE_ID, VALID_STORE_SECRET, :mode => :test)
69
+ assert_equal :test, client.mode
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#timeout' do
75
+ it 'should be defined' do
76
+ assert_respond_to @client, :timeout
77
+ end
78
+
79
+ it 'should have default value: 10 seconds' do
80
+ assert_equal 10, @client.timeout
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "Mode-specific Attributes" do
86
+ describe '#ssl?' do
87
+ it 'should be defined' do
88
+ assert_respond_to @client, :ssl?
89
+ end
90
+
91
+ it 'should be true only if protocol is HTTPS' do
92
+ # TODO
93
+ end
94
+ end
95
+
96
+ describe "Autodetected attributes" do
97
+ describe '#protocol' do
98
+ it 'should be defined' do
99
+ assert_respond_to @client, :protocol
100
+ end
101
+
102
+ describe 'mode: test' do
103
+ before do
104
+ @client.mode = :test
105
+ end
106
+
107
+ it 'should be: http' do
108
+ assert_equal 'http', @client.protocol
109
+ end
110
+ end
111
+
112
+ describe 'mode: test' do
113
+ before do
114
+ @client.mode = :production
115
+ end
116
+
117
+ it 'should be: https' do
118
+ assert_equal 'https', @client.protocol
119
+ end
120
+ end
121
+ end
122
+
123
+ describe '#host' do
124
+ it 'should be defined' do
125
+ assert_respond_to @client, :host
126
+ end
127
+
128
+ describe 'mode: test' do
129
+ before do
130
+ @client.mode = :test
131
+ end
132
+
133
+ it 'should be: beta-test.klarna.com' do
134
+ assert_equal @client.host, 'payment-beta.klarna.com'
135
+ end
136
+ end
137
+
138
+ describe 'mode: production' do
139
+ before do
140
+ @client.mode = :production
141
+ end
142
+
143
+ it 'should be: payment.klarna.com' do
144
+ assert_equal @client.host, 'payment.klarna.com'
145
+ end
146
+ end
147
+ end
148
+
149
+ describe '#port' do
150
+ it 'should be defined' do
151
+ assert_respond_to @client, :port
152
+ end
153
+
154
+ describe 'mode: test' do
155
+ before do
156
+ @client.mode = :test
157
+ end
158
+
159
+ it 'should be: 80' do
160
+ assert_equal 80, @client.port
161
+ end
162
+ end
163
+
164
+ describe 'mode: production' do
165
+ before do
166
+ @client.mode = :production
167
+ end
168
+
169
+ it 'should be: 443' do
170
+ assert_equal 443, @client.port
171
+ end
172
+ end
173
+ end
174
+
175
+ describe '#endpoint_uri' do
176
+ describe 'mode: test' do
177
+ before do
178
+ @client.mode = :test
179
+ end
180
+
181
+ it 'should be: http://beta-test.klarna.com:80' do
182
+ assert_equal 'http://payment-beta.klarna.com:80', @client.endpoint_uri
183
+ end
184
+ end
185
+
186
+ describe 'mode: production' do
187
+ before do
188
+ @client.mode = :production
189
+ end
190
+
191
+ it 'should be: https://payment.klarna.com:443' do
192
+ assert_equal 'https://payment.klarna.com:443', @client.endpoint_uri
193
+ end
194
+ end
195
+
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ describe "Helpers" do
202
+
203
+ describe '#call' do
204
+ it 'should be defined' do
205
+ assert_respond_to @client, :call
206
+ end
207
+
208
+ it 'should raise Klarna service error for dummie method: -99' do
209
+ assert_raises Klarna::API::Errors::KlarnaServiceError do
210
+ begin
211
+ @client.call(:hello)
212
+ rescue Klarna::API::Errors::KlarnaServiceError => e
213
+ assert_equal -99, e.error_code
214
+ raise e
215
+ end
216
+ end
217
+ end
218
+ end
219
+
220
+ describe '#digest' do
221
+ it 'should be defined' do
222
+ assert_respond_to @client, :digest
223
+ end
224
+
225
+ describe 'default behaviour' do
226
+ it 'should calculate a valid digest secret for one value' do
227
+ swap @client, :store_id => 123, :store_secret => "secret" do
228
+ assert_equal ::Klarna::API.digest(123, "value 123", "secret"), @client.send(:digest, "value 123")
229
+ end
230
+ end
231
+
232
+ it 'should calculate a valid digest secret for an array of values' do
233
+ swap @client, :store_id => 123, :store_secret => "secret" do
234
+ assert_equal ::Klarna::API.digest(123, "value 1", "value 2", "value 3", "secret"), @client.send(:digest, "value 1", "value 2", "value 3")
235
+ end
236
+ end
237
+ end
238
+
239
+ describe 'without estore-ID - some methods apparently need this' do
240
+ it 'should calculate a valid digest secret for one value' do
241
+ swap @client, :store_id => 123, :store_secret => "secret" do
242
+ assert_equal ::Klarna::API.digest("value 123", "secret"), @client.send(:digest, "value 123", :store_id => false)
243
+ end
244
+ end
245
+
246
+ it 'should calculate a valid digest secret for an array of values' do
247
+ swap @client, :store_id => 123, :store_secret => "secret" do
248
+ assert_equal ::Klarna::API.digest("value 1", "value 2", "value 3", "secret"), @client.send(:digest, "value 1", "value 2", "value 3", :store_id => false)
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ describe '#content_type_headers' do
255
+ it 'should be defined' do
256
+ assert_respond_to @client, :content_type_headers
257
+ end
258
+
259
+ it 'should have header: Accept-Charset: iso-8859-1' do
260
+ assert_equal 'iso-8859-1', @client.send(:content_type_headers)['Accept-Charset']
261
+ end
262
+
263
+ it 'should have header: Content-Type: text/xml;charset=iso-8859-1' do
264
+ assert_equal 'text/xml;charset=iso-8859-1', @client.send(:'content_type_headers')['Content-Type']
265
+ end
266
+
267
+ it 'should have header: Connection: close' do
268
+ assert_equal 'close', @client.send(:content_type_headers)['Connection']
269
+ end
270
+
271
+ it 'should have header: Accept-Charset: iso-8859-1' do
272
+ assert_equal 'ruby/xmlrpc', @client.send(:content_type_headers)['User-Agent']
273
+ end
274
+ end
275
+
276
+ describe '#add_meta_params' do
277
+ it 'should be defined' do
278
+ assert_respond_to @client, :add_meta_params
279
+ end
280
+
281
+ it %Q{should push default RPC-params/arguments: protocol version + client version ("1.0", "#{::XMLRPC::Client::USER_AGENT}")} do
282
+ assert_equal ["#{::Klarna::API::PROTOCOL_VERSION}", "#{::XMLRPC::Client::USER_AGENT}"], @client.send(:add_meta_params)
283
+ end
284
+ end
285
+
286
+ end
287
+
288
+ end