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,188 @@
1
+ # encoding: utf-8
2
+
3
+ module Klarna
4
+ module API
5
+ module Methods
6
+ module Reservation
7
+
8
+ # Reserve a purchase amount for a specific customer. The reservation is valid, by default, for 7 days.
9
+ # Pass cellphone no. instead of Pno for SMS payments.
10
+ #
11
+ def reserve_amount(params)
12
+
13
+ xmlrpc_params = [
14
+ ::Klarna::API::PROTOCOL_VERSION,
15
+ ::XMLRPC::Client::USER_AGENT,
16
+ params[:pno],
17
+ params[:gender] || 0,
18
+ params[:amount],
19
+ params[:reference] || '',
20
+ params[:reference_code] || '',
21
+ params[:order_id],
22
+ params[:order_id],
23
+ params[:delivery_address],
24
+ params[:billing_address],
25
+ params[:client_ip] || '0.0.0.0',
26
+ params[:flags] || 0,
27
+ params[:currency],
28
+ params[:country],
29
+ params[:language],
30
+ self.store_id,
31
+ self.digest(params[:pno], params[:amount]),
32
+ params[:pno_encoding],
33
+ (params[:pclass] || ::Klarna::API::DEFAULTS[:PCLASS]),
34
+ params[:goods_list],
35
+ params[:comment] || '',
36
+ params[:shipmentinfo] || { delay_adjust: 1 },
37
+ params[:travelinfo] || [],
38
+ params[:income_expense] || [::Klarna::API::DEFAULTS[:YSALARY]],
39
+ params[:bankinfo] || [],
40
+ params[:session_id] || [],
41
+ params[:extra_info] || []
42
+ ]
43
+
44
+ self.call(:reserve_amount, *xmlrpc_params).tap do |result|
45
+ result = result.first
46
+ end
47
+ end
48
+
49
+ def check_order_status(id)
50
+ xmlrpc_params = [
51
+ ::Klarna::API::PROTOCOL_VERSION,
52
+ ::XMLRPC::Client::USER_AGENT,
53
+ self.store_id.to_i,
54
+ self.digest(id.to_s),
55
+ id.to_s,
56
+ 0
57
+ ]
58
+ self.call(:check_order_status, *xmlrpc_params)
59
+ end
60
+ # Activate purchases which have been previously reserved with the reserve_amount function.
61
+ #
62
+ def activate_reservation(params)
63
+
64
+ xmlrpc_params = [
65
+ ::Klarna::API::PROTOCOL_VERSION,
66
+ ::XMLRPC::Client::USER_AGENT,
67
+ params[:reservation_id],
68
+ params[:ocr] || '',
69
+ params[:pno],
70
+ params[:gender] || 0,
71
+ params[:reference] || '',
72
+ params[:reference_code] || '',
73
+ params[:order_id_1],
74
+ params[:order_id_2],
75
+ params[:delivery_address],
76
+ params[:billing_address],
77
+ params[:client_ip] || '0.0.0.0',
78
+ params[:flags] || 0,
79
+ params[:currency],
80
+ params[:country],
81
+ params[:language],
82
+ self.store_id,
83
+ self.digest(params[:pno], params[:goods_list].map{ |goods| goods[:goods][:artno]+':'+goods[:qty].to_s } ),
84
+ params[:pno_encoding],
85
+ (params[:pclass] || ::Klarna::API::DEFAULTS[:PCLASS]),
86
+ params[:goods_list],
87
+ params[:comment] || '',
88
+ params[:shipmentinfo] || { delay_adjust: 1 },
89
+ params[:travelinfo] || [],
90
+ params[:income_expense] || [::Klarna::API::DEFAULTS[:YSALARY]],
91
+ params[:session_id] || [],
92
+ params[:extra_info] || []
93
+ ]
94
+
95
+ xmlrpc_params = [xmlrpc_params] # Klarna needs all values to be in first param for activate_reservation only
96
+
97
+ self.call(:activate_reservation, *xmlrpc_params)
98
+ end
99
+
100
+ # Cancel a reservation.
101
+ #
102
+ def cancel_reservation(params)
103
+ xmlrpc_params = [
104
+ ::Klarna::API::PROTOCOL_VERSION,
105
+ ::XMLRPC::Client::USER_AGENT,
106
+ params[:reservation_id],
107
+ self.store_id,
108
+ self.digest(params[:reservation_id])
109
+ ]
110
+ self.call(:cancel_reservation, *xmlrpc_params)
111
+ end
112
+
113
+ # Split a reservation due to for example outstanding articles.
114
+ #
115
+ def split_reservation(params)
116
+
117
+ xmlrpc_params = [
118
+ ::Klarna::API::PROTOCOL_VERSION,
119
+ ::XMLRPC::Client::USER_AGENT,
120
+ params[:reservation_id],
121
+ params[:split_amount],
122
+ params[:order_id_1],
123
+ params[:order_id_2],
124
+ params[:flags].to_i || 0,
125
+ self.store_id,
126
+ self.digest(params[:reservation_id], params[:split_amount])
127
+ ]
128
+ self.call(:split_reservation, *xmlrpc_params).tap do |result|
129
+ result = result.first
130
+ end
131
+ end
132
+
133
+ # Change a reservation.
134
+ #
135
+ def change_reservation(params)
136
+
137
+ xmlrpc_params = [
138
+ ::Klarna::API::PROTOCOL_VERSION,
139
+ ::XMLRPC::Client::USER_AGENT,
140
+ params[:reservation_id],
141
+ params[:new_amount],
142
+ self.store_id,
143
+ self.digest(params[:reservation_id], params[:new_amount])
144
+ ]
145
+ self.call(:change_reservation, *xmlrpc_params).tap do |result|
146
+ result = result.first
147
+ end
148
+ end
149
+
150
+ # Reserves one or more OCR numbers for your store.
151
+ #
152
+ def reserve_ocr_numbers(number_of_ocrs)
153
+ # params = [
154
+ # ::Klarna::API::PROTOCOL_VERSION,
155
+ # ::XMLRPC::Client::USER_AGENT,
156
+ # number_of_ocrs,
157
+ # self.store_id,
158
+ # self.digest(number_of_ocrs)
159
+ # ]
160
+ # self.call(:reserve_ocr_nums, *params)
161
+ raise NotImplementedError
162
+ end
163
+
164
+ # Create addresses for arguments such as the +activate_reservation+ function.
165
+ #
166
+ def make_reservation_address(params)
167
+ {
168
+ :email => (params[:email] || ''),
169
+ :telno => (params[:telno] || ''),
170
+ :cellno => (params[:cellno] || ''),
171
+ :fname => (params[:fname] || ''),
172
+ :lname => (params[:lname] || ''),
173
+ :company => (params[:company] || ''),
174
+ :careof => (params[:careof] || ''),
175
+ :street => (params[:street] || ''),
176
+ :zip => (params[:zip] || ''),
177
+ :city => (params[:city] || ''),
178
+ :country => (::Klarna::API.id_for(:country, params[:country]) || ''),
179
+ :house_number => (params[:house_number] || ''),
180
+ :house_extension => (params[:house_extension] || '')
181
+ }.with_indifferent_access
182
+ end
183
+ alias :mk_reservation_address :make_reservation_address
184
+
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ module Klarna
4
+ module API
5
+ module Methods
6
+ module Standard
7
+
8
+ # Retrieve a customer’s address(es). Using this, the customer is not required to enter
9
+ # any information – only confirm the one presented to him/her.
10
+ # Can also be used for companies: If the customer enters a company number,
11
+ # it will return all the addresses where the company is registered at.
12
+ #
13
+ # == Note:
14
+ #
15
+ # ONLY allowed to be used for Swedish persons with the following conditions:
16
+ #
17
+ # * It can be only used if invoice or part payment is the default payment method
18
+ # * It has to disappear if the customer chooses another payment method
19
+ # * The button is not allowed to be called get address ("hämta adress"), but continue ("fortsätt")
20
+ # or it can be picked up automatically when all the numbers have been typed.
21
+ #
22
+ # In the other Nordic countries you will have to have input fields for name, last name, street name,
23
+ # zip code and city so that the customer can enter this information by himself.
24
+ #
25
+ def get_addresses(pno, pno_encoding, address_type = :GIVEN)
26
+ pno = pno.to_s.gsub(/[\W]/, '')
27
+ pno_encoding = ::Klarna::API.id_for(:pno_format, pno_encoding)
28
+ address_type = ::Klarna::API.id_for(:address_format, address_type)
29
+ params = [
30
+ ::Klarna::API::PROTOCOL_VERSION,
31
+ ::XMLRPC::Client::USER_AGENT,
32
+ pno,
33
+ self.store_id,
34
+ self.digest(pno),
35
+ pno_encoding,
36
+ address_type,
37
+ self.client_ip
38
+ ]
39
+ self.call(:get_addresses, *params).tap do |result|
40
+ result = result.first
41
+ country_id = ::Klarna::API.id_for(:country, result[5])
42
+ result[5] = ::Klarna::API::COUNTRIES.key(country_id)
43
+ end
44
+ end
45
+
46
+ # Same as +get_addresses+ but returns only first address.
47
+ #
48
+ def get_address(*args)
49
+ self.get_addresses(*args).first
50
+ end
51
+
52
+ # Create addresses (i.e. the +address+ argument to the +add_transaction+ method).
53
+ #
54
+ def make_address(co_address, street_address, zip, city, country, phone, cell_phone, email,
55
+ house_number = nil, house_extension = nil)
56
+ country = ::Klarna::API.id_for(:country, country)
57
+ phone = phone.to_s.gsub(/[\W\s\t]/, '')
58
+ cell_phone = cell_phone.to_s.gsub(/[\W\s\t]/, '')
59
+ {
60
+ :careof => co_address,
61
+ :street => street_address,
62
+ :postno => zip,
63
+ :city => city,
64
+ :country => country,
65
+ :telno => phone,
66
+ :cellno => cell_phone,
67
+ :email => email
68
+ }.with_indifferent_access
69
+ end
70
+ alias :mk_address :make_address
71
+
72
+ # Create an inventory (i.e. the +goods_list+ argument) to the +add_transaction+ function.
73
+ #
74
+ # == Flags:
75
+ #
76
+ # Argument +flags+ can be used to set the precision of the article, to indicate a shipment
77
+ # or a fee or sending the price with VAT.
78
+ #
79
+ # By using either +PRINT_1000+, +PRINT_100+, or +PRINT_10+, the +flags+
80
+ # function can be used to set article quantity (+quantity+). Unit is either +1/10+, +1/100+
81
+ # or +1/1000+. This is useful for goods measured in meters or kilograms, rather than number of items.
82
+ #
83
+ # By using the +IS_SHIPMENT+ or +IS_HANDLING+ flags a shipping or handling fee can
84
+ # be applied. The fees are sent excluding VAT with the arguments +shipping_fee+ or +handling_fee+.
85
+ #
86
+ # By using the +INC_VAT+ flag, you can send the price including VAT.
87
+ #
88
+ # == Note:
89
+ #
90
+ # If you are implementing sales with Euros, use the function mk_goods_flags instead
91
+ # since using this method can result in round off problems. With +mk_goods_flags+ you
92
+ # are able to send the price with VAT.
93
+ #
94
+ def make_goods(quantity, article_no, title, price, vat, discount = nil, flags = nil)
95
+ flags = ::Klarna::API.parse_flags(:GOODS, flags)
96
+ goods = {
97
+ :goods => {
98
+ :artno => article_no,
99
+ :title => title,
100
+ :price => price.to_i,
101
+ :vat => vat.to_f.round(2),
102
+ :discount => discount.to_f.round(2),
103
+ },
104
+ :qty => quantity.to_i
105
+ }
106
+ goods[:goods].merge!(:flags => flags.to_i) # if flags.present?
107
+ goods.with_indifferent_access
108
+ end
109
+ alias :mk_goods :make_goods
110
+ alias :mk_goods_flags :make_goods
111
+
112
+ # Check if a user has an account.
113
+ #
114
+ def has_account?(pno, pno_encoding)
115
+ params = [
116
+ self.store_id,
117
+ self.digest(pno),
118
+ pno_encoding
119
+ ]
120
+ self.call(:has_account, *params)
121
+ end
122
+
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module Klarna
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,97 @@
1
+ # Here are Klarnas test COMPANIES.
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
+ companies:
10
+ se:
11
+ approved:
12
+ cno: "6020310139"
13
+ name: "Kalle Anka AB"
14
+ street_address: "Storgatan 1"
15
+ postal_code: "12345"
16
+ city: "Ankeborg"
17
+ street_address_2: "Lillgatan 99"
18
+ postal_code_2: "77777"
19
+ city_2: "Ankeborg"
20
+ country: "SE"
21
+ cell_phone: "076 526 00 00"
22
+ currency: "SEK"
23
+ language: "SV"
24
+ unapproved:
25
+ cno: "6720217931"
26
+ name: "Björnligan AB"
27
+ street_address: "Fulgatan 1"
28
+ postal_code: "12345"
29
+ city: "Ankeborg"
30
+ street_address_2: "Bakgatan 99"
31
+ postal_code_2: "77777"
32
+ city_2: "Ankeborg"
33
+ country: "SE"
34
+ cell_phone: "076 526 00 00"
35
+ currency: "SEK"
36
+ language: "SV"
37
+
38
+ no:
39
+ # N/A
40
+
41
+ dk:
42
+ approved:
43
+ pno: "27968880"
44
+ first_name: ""
45
+ last_name: "Onbase ApS"
46
+ street_address: "Centrumgaden 37"
47
+ postal_code: "2750"
48
+ city: "Ballerup"
49
+ country: "DK"
50
+ cell_phone: "20 123 456"
51
+ currency: "DKK"
52
+ language: "DA"
53
+ unapproved:
54
+ pno: "99999993"
55
+ first_name: ""
56
+ last_name: "Larsen & Olsen Contracters ApS"
57
+ street_address: "Glarmestervej 2"
58
+ postal_code: "8600"
59
+ city: "Silkeborg"
60
+ street_address: "Kristianiagade 9"
61
+ postal_code: "2100"
62
+ city: "København Ø"
63
+ country: "DK"
64
+ cell_phone: "20 123 456"
65
+ currency: "DKK"
66
+ language: "DA"
67
+
68
+ fi:
69
+ approved:
70
+ pno: "10891871"
71
+ first_name: ""
72
+ last_name: "Porin Mies-Laulu r.y."
73
+ street_address: "Vapaudenkatu 10"
74
+ postal_code: "28100"
75
+ city: "Pori"
76
+ country: "FI"
77
+ cell_phone: "040 123 45 67"
78
+ currency: "EUR"
79
+ language: "FI"
80
+
81
+ unapproved:
82
+ pno: "07527622"
83
+ first_name: ""
84
+ last_name: "Mankalan Perhekodit Oy"
85
+ street_address: "Porrassalmenkatu 19 B"
86
+ postal_code: "50100"
87
+ city: "Parikkala"
88
+ country: "FI"
89
+ cell_phone: "040 123 45 67"
90
+ currency: "EUR"
91
+ language: "FI"
92
+
93
+ de:
94
+ # N/A
95
+
96
+ nl:
97
+ # N/A
@@ -0,0 +1,37 @@
1
+ # Pclass id number, Description, Amount of months for part payment, Start fee, Invoice fee, Interest rate, Minimum purchase amount for pclass
2
+
3
+ pclasses:
4
+ sek:
5
+ 390:
6
+ pclass_id: 390
7
+ description: "Konto"
8
+ months: 24
9
+ start_fee: 0
10
+ invoice_fee: 2900
11
+ interest_rate: 1950
12
+ minimum_purchase_amount: 100
13
+ 121:
14
+ pclass_id: 121
15
+ description: "6 m\303\245n"
16
+ months: 6
17
+ start_fee: 19500
18
+ invoice_fee: 2900
19
+ interest_rate: 0
20
+ minimum_purchase_amount: 100000
21
+ 120:
22
+ pclass_id: 120
23
+ description: "3 m\303\245n"
24
+ months: 3
25
+ start_fee: 9500
26
+ invoice_fee: 2900
27
+ interest_rate: 0
28
+ minimum_purchase_amount: 100000
29
+
30
+ nok:
31
+ # Not available (for some reason)
32
+
33
+ dkk:
34
+ # Not available (for some reason)
35
+
36
+ eur:
37
+ # Not available (for some reason)