Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes based on feedback from Klarna tech preparing for first public r…

…elease.
  • Loading branch information...
commit 5add30a63d9154d7c25350d5fc9360091704a386 1 parent 7d585a4
@grimen grimen authored
View
10 README.textile
@@ -10,6 +10,14 @@ h2. What is Klarna?
Sweden (SE), Norway (NO), Denmark (DK), Finland (FI), Germany (DE), Netherlands (NL).
+h2. Implementation/Background
+
+This gem was developed as a semi-1:1 implementation of the corresponding API implementations provided by Klarna (with a few simple justifications and heavily refactored methods). Klarnas official implementations were not implemented using OO patterns and as their implementation was changing very often without any notice/versioning I kept it simple as possible.
+
+Since Klarna 2.0 was released they'va refactored their implementations a lot which makes it a good idea to review this implementation as well. Like stated in the TODO I had a clean classy DSL in mind.
+
+This is not a complete implementation, but not far from it and enough for most use cases - a few of the more advanced API methods needs a review/revisit (code disabled now).
+
h2. Setup
Install the gem:
@@ -123,7 +131,7 @@ This gem was developed for our own requirements at "Merchii":http://github.com/m
h2. To-do
-This is not a complete implementation, but not far from it though. I had bigger plans way back when we wrote this, but since I assume Klarna will release a completely new API soon (_Klarna 2.0_ which I assume will be REST API?) - we're not sure we will bother to "finish" the planned features for the current XML-RPC API. Time will tell. For more details, see the "TODO":https://github.com/merchii/klarna-ruby/blob/master/TODO.
+See the "TODO":https://github.com/merchii/klarna-ruby/blob/master/TODO.
h2. License
View
14 TODO
@@ -1,15 +1,16 @@
== CURRENT
-x [feature]: Implement the last missing Invoicing methods
------
-* [feature]: Review/Implement something like: our_settype_integer, our_strip_string, our_settype_string (consider one generic method, e.g. parse(:price, value) - all validation/masking/casting in one place)
-* [feature]: Ensure valid string format/stripping (e.g. SSN, etc)
-* [feature]: Ensure correct params datatypes are submitted (validation - consider generic method)
+x [issue]: Test the invoice activation in a proper way - not allowed in the Klarna 2.0 API (for some weird reason...)
== HIGH-PRIO
+* [feature]: Review/Implement something like: our_settype_integer, our_strip_string, our_settype_string (consider one generic method, e.g. parse(:price, value) - all validation/masking/casting in one place)
+* [feature]: Ensure valid string format/stripping (e.g. SSN, etc) using regular expressions
+* [feature]: Compare implementation with Klarna's new API 2.0
+* [feature]: Ensure correct params datatypes are submitted (validation - consider generic method)
+* [feature]: Klarna::DSL - wrapper DSL for less sucky integrations
* [feature]: Implement #get_pclasses method to export pclasses as hash (and maybe export to YAML?)
* [feature]: ActiveMerchant-integration (fork ActiveMerchant and add klarna as new gateway with "klarna-ruby" as dependency)
@@ -18,7 +19,6 @@ x [feature]: Implement the last missing Invoicing methods
* [feature]: Finalize the rest of the specs for already implemented methods, commented out currently.
* [feature]: Implement Reservation methods
-* [feature]: Klarna::DSL - wrapper DSL for less sucky integrations
* [feature]: Bring back "examples/web" - probably not working at all now.
* [feature]: Rack middleware?
@@ -26,3 +26,5 @@ x [feature]: Implement the last missing Invoicing methods
== MAYBE
* [feature]: Mock XML-responses by default to avoid live API calls, but allow live calls using ENV['REMOTE]=true
+
+ https://github.com/myronmarston/vcr
View
8 lib/klarna/api/client.rb
@@ -9,7 +9,7 @@ class Client < ::XMLRPC::Client
include ::Klarna::API::Methods
- attr_accessor :store_id,
+ attr_accessor :store_id,
:store_secret,
:mode,
:timeout,
@@ -79,15 +79,15 @@ def ssl?
alias :use_ssl? :ssl?
def protocol
- @protocol ||= ::Klarna::API::END_POINT[self.mode][:protocol]
+ @protocol = ::Klarna::API::END_POINT[self.mode][:protocol]
end
def host
- @host ||= ::Klarna::API::END_POINT[self.mode][:host]
+ @host = ::Klarna::API::END_POINT[self.mode][:host]
end
def port
- @port ||= ::Klarna::API::END_POINT[self.mode][:port]
+ @port = ::Klarna::API::END_POINT[self.mode][:port]
end
def endpoint_uri
View
8 lib/klarna/api/constants.rb
@@ -10,17 +10,17 @@ module Constants
END_POINT = {
:test => {
:protocol => 'http',
- :host => 'payment.klarna.com',
+ :host => 'payment-beta.klarna.com',
:port => 80
},
:production => {
- :protocol => 'http',
+ :protocol => 'https',
:host => 'payment.klarna.com',
- :port => 80
+ :port => 443
}
}.freeze
- PROTOCOL_ENCODING = 'iso-8859-1'.freeze # ['UTF-8', 'ISO-8859-1', 'US-ASCII'].freeze
+ PROTOCOL_ENCODING = 'iso-8859-1'.freeze # NOTE: New API supports: ['UTF-8', 'ISO-8859-1', 'US-ASCII'].freeze
PROTOCOL_VERSION = '4.0'.freeze
# -----------------------------------------------------------------------
View
2  lib/klarna/api/methods/cost_calculations.rb
@@ -5,7 +5,7 @@ module API
module Methods
module CostCalculations
- # Purpose: Obtain pcalss values from Klarna.
+ # Purpose: Obtain pclass values from Klarna.
#
# == Note:
#
View
72 lib/klarna/api/methods/invoicing.rb
@@ -12,7 +12,7 @@ module Invoicing
# Create an invoice.
#
- def add_invoice(store_user_id, order_id, goods_list, shipping_fee,
+ def add_invoice(store_user_id, order_id, articles, shipping_fee,
handling_fee, shipment_type, pno, first_name, last_name, address, client_ip,
currency, country, language, pno_encoding, pclass = nil, annual_salary = nil,
password = nil, ready_date = nil, comment = nil, rand_string = nil, new_password = nil, flags = nil)
@@ -23,13 +23,14 @@ def add_invoice(store_user_id, order_id, goods_list, shipping_fee,
pno_encoding = ::Klarna::API.id_for(:pno_format, pno_encoding)
pclass = pclass ? ::Klarna::API.id_for(:pclass, pclass) : -1
flags = ::Klarna::API.parse_flags(:INVOICE, flags)
+ articles = Array.wrap(articles).compact
params = [
self.store_id,
store_user_id,
- self.digest(goods_list.collect { |g| g[:goods][:title] }, :store_id => false),
+ self.digest(articles.collect { |g| g[:goods][:title] }, :store_id => false),
order_id,
- goods_list,
+ articles,
shipping_fee,
shipment_type,
handling_fee,
@@ -70,6 +71,7 @@ def add_invoice(store_user_id, order_id, goods_list, shipping_fee,
def activate_invoice(invoice_no, articles = nil)
# TODO: Parse/Validate invoice_no as :integer
# TODO: Parse/Valdiate articles as array of articles
+ articles = Array.wrap(articles).compact
params = [
self.store_id,
@@ -79,7 +81,7 @@ def activate_invoice(invoice_no, articles = nil)
# Only partly?
if articles.present?
params << articles
- params << self.digest(invoice_no, articles.collect { |a| a.join(':') }.join(':'))
+ params << self.digest(invoice_no, articles.collect { |a| [a[:goods][:artno], a[:qty]].join(':') }.join(':'))
method = :activate_part
else
params << self.digest(invoice_no)
@@ -107,36 +109,37 @@ def delete_invoice(invoice_no)
# Give discounts for invoices.
#
def return_amount(invoice_no, amount, vat)
- # params = [
- # self.store_id,
- # invoice_no,
- # amount,
- # vat,
- # self.digest(invoice_no)
- # ]
- # self.call(:return_amount, *params)
- raise NotImplementedError
+ params = [
+ self.store_id,
+ invoice_no,
+ amount,
+ vat,
+ self.digest(invoice_no)
+ ]
+ self.call(:return_amount, *params) # raise NotImplementedError
end
# Return a invoice - optionally only partly.
#
def credit_invoice(invoice_no, credit_id, articles = nil)
- # params = [
- # self.store_id,
- # invoice_no,
- # credit_id,
- # ]
- # # Only partly?
- # if articles.present?
- # params << articles
- # params << self.digest(invoice_no, articles.collect { |a| a.join(':') }.join(':'))
- # method = :credit_part
- # else
- # params << self.digest(invoice_no)
- # method = :credit_invoice
- # end
- # self.call(method, *params)
- raise NotImplementedError
+ articles = Array.wrap(articles).compact
+
+ params = [
+ self.store_id,
+ invoice_no,
+ credit_id,
+ ]
+
+ if articles.present? # Only partly?
+ params << articles
+ params << self.digest(invoice_no, articles.collect { |a| [a[:goods][:artno], a[:qty]].join(':') }.join(':'))
+ method = :credit_part
+ else
+ params << self.digest(invoice_no)
+ method = :credit_invoice
+ end
+
+ self.call(method, *params)
end
# Send an active invoice to the customer via e-mail.
@@ -253,6 +256,13 @@ def invoice_address(invoice_no)
#
def invoice_amount(invoice_no, articles = nil)
# TODO: Parse/Validate invoice_no as integer
+ articles = Array.wrap(articles).compact
+ artnos =
+ if articles.first.respond_to?(:key?) && articles.first.key?(:qty) && articles.first.key?(:artno)
+ articles
+ else
+ articles.collect { |a| {:artno => a[:goods][:artno], :qty => a[:qty]} }
+ end
params = [
self.store_id,
@@ -261,8 +271,8 @@ def invoice_amount(invoice_no, articles = nil)
# Only partly?
if articles.present?
- params << articles
- params << self.digest(invoice_no, articles.collect { |a| a.join(':') }.join(':'))
+ params << artnos
+ params << self.digest(invoice_no, artnos.collect { |an| [an[:artno], an[:qty]].join(':') }.join(':'))
method = :invoice_part_amount
else
params << self.digest(invoice_no)
View
13 lib/klarna/api/methods/standard.rb
@@ -109,13 +109,12 @@ def make_goods(quantity, article_no, title, price, vat, discount = nil, flags =
# Check if a user has an account.
#
def has_account?(pno, pno_encoding)
- # params = [
- # self.store_id,
- # self.digest(pno),
- # pno_encoding
- # ]
- # self.call(:has_account, *params)
- raise NotImplementedError
+ params = [
+ self.store_id,
+ self.digest(pno),
+ pno_encoding
+ ]
+ self.call(:has_account, *params)
end
end
View
2  lib/klarna/version.rb
@@ -1,5 +1,5 @@
# encoding: utf-8
module Klarna
- VERSION = '0.1.2'
+ VERSION = '0.1.3'
end
View
2  test/klarna/api/methods/cost_calculations_test.rb
@@ -3,7 +3,7 @@
describe Klarna::API::Methods::CostCalculations do
- # TODO: Mock responses.
+ # TODO: Mock responses using VCR.
before do
valid_credentials!
View
330 test/klarna/api/methods/invoicing_test.rb
@@ -3,7 +3,7 @@
describe Klarna::API::Methods::Invoicing do
- # TODO: Mock responses.
+ # TODO: Mock responses using VCR.
INVALID_ORDER_NO = '12312312312312312'
@@ -18,10 +18,10 @@
expose_protected_methods_in @klarna.class
@order_items = []
- @order_items << @klarna.make_goods(1, 'ABC1', "T-shirt 1", 1.00 * 100, 25)
- @order_items << @klarna.make_goods(3, 'ABC2', "T-shirt 2", 7.00 * 100, 25)
- @order_items << @klarna.make_goods(7, 'ABC3', "T-shirt 3", 17.00 * 100, 25)
- @order_items_total = ((1 * (1.00 * 100) + 3 * (7.00 * 100) + 7 * (17.00 * 100)) * 1.25).to_i
+ @order_items << @klarna.make_goods(1, 'ABC1', "T-shirt 1", 1.00 * 100, 25, 0, :INC_VAT => true)
+ @order_items << @klarna.make_goods(3, 'ABC2', "T-shirt 2", 7.00 * 100, 25, 0, :INC_VAT => true)
+ @order_items << @klarna.make_goods(7, 'ABC3', "T-shirt 3", 17.00 * 100, 25, 0, :INC_VAT => true)
+ @order_items_total = (1 * (1.00 * 100) + 3 * (7.00 * 100) + 7 * (17.00 * 100)).to_i
@address_SE = @klarna.make_address("c/o Lidin", "Junibackg. 42", "23634", "Höllviken", :SE, "076 526 00 00", "076 526 00 00", "karl.lidin@klarna.com")
@@ -52,40 +52,58 @@
end
end
- # FAILS: active_invoice don't seem to work with the Klarna backend currently. Tried the TEST_MODE-flag but no luck. Revisit.
+ # NOTE: active_invoice don't seem to work with the Klarna 2.0 backend currently, raises "invoice_in_test_mode" (which it didn't before).
# Spec:
# http://integration.klarna.com/en/api/standard-integration/functions/activateinvoice
# http://integration.klarna.com/en/api/standard-integration/functions/activatepart (combined)
- # describe '#activate_invoice' do
- # it 'should be defined' do
- # assert_respond_to @klarna, :activate_invoice
- # end
+ describe '#activate_invoice' do
+ it 'should be defined' do
+ assert_respond_to @klarna, :activate_invoice
+ end
- # it 'should raise error for when trying to activate an invalid/non-existing invoice' do
- # assert_raises ::Klarna::API::Errors::KlarnaServiceError do
- # @klarna.activate_invoice("12312312312312312")
- # end
- # end
+ describe 'full' do
+ it 'should raise error for when trying to activate an non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ @klarna.activate_invoice(INVALID_ORDER_NO)
+ end
+ end
- # it 'should successfully activate an existing invoice' do
- # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ it 'should successfully activate an existing invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ #
+ # assert_equal "#{@klarna.endpoint_uri}/temp/#{invoice_no}.pdf", @klarna.activate_invoice(invoice_no)
+ # end
+ end
- # assert_equal "#{@klarna.endpoint_uri}/temp/#{invoice_no}.pdf", @klarna.activate_invoice(invoice_no)
- # end
+ describe 'partial' do
+ it 'should raise error for when trying to activate an non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ activate_articles = [@order_items.first]
+ @klarna.activate_invoice(INVALID_ORDER_NO, activate_articles)
+ end
+ end
- # # it 'should active an existing invoice partially if articles are specified as well'
- # end
+ # FAILS: Klarna API 2.0 don't support this for test-accounts. :(
+ it 'should successfully activate an existing partial invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # activate_articles = [@order_items.first]
+ #
+ # assert_equal "#{@klarna.endpoint_uri}/temp/#{invoice_no}.pdf", @klarna.activate_invoice(invoice_no, activate_articles)
+ # end
+ end
+ end
- # Spec: http://integration.klarna.com/en/api/standard-integration/functions/deleteinvoice
+ # Spec:
+ # http://integration.klarna.com/en/api/standard-integration/functions/deleteinvoice
describe '#delete_invoice' do
it 'should be defined' do
assert_respond_to @klarna, :delete_invoice
end
- it 'should raise error when trying to delete an invalid/non-existing invoice' do
+ it 'should raise error when trying to delete an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
- @klarna.delete_invoice("12312312312312312")
+ @klarna.delete_invoice(INVALID_ORDER_NO)
end
end
@@ -96,63 +114,143 @@
end
end
- # Spec: http://integration.klarna.com/en/api/invoice-handling-functions/functions/returnamount
- # describe '#return_amount' do
- # it 'should be defined' do
- # assert_respond_to @klarna, :return_amount
- # end
- # end
-
- # Spec: http://integration.klarna.com/en/api/invoice-handling-functions/functions/returnamount
- # describe '#credit_invoice' do
- # it 'should be defined' do
- # assert_respond_to @klarna, :credit_invoice
- # end
- # end
-
- # Spec: http://integration.klarna.com/en/api/invoice-handling-functions/functions/emailinvoice
- # describe '#email_invoice' do
- # it 'should be defined' do
- # assert_respond_to @klarna, :email_invoice
- # end
-
- # it 'should raise error for e-mail request of an existing but un-activated invoice' do
- # assert_raises ::Klarna::API::Errors::KlarnaServiceError do
- # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- # @klarna.email_invoice(invoice_no)
- # end
- # end
-
- # it 'should successfully accept email request of an existing invoice' do
- # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- # invoice_url = @klarna.activate_invoice(invoice_no)
-
- # assert_equal invoice_no, @klarna.email_invoice(invoice_no)
- # end
- # end
-
- # Spec: http://integration.klarna.com/en/api/invoice-handling-functions/functions/sendinvoice
- # describe '#send_invoice' do
- # it 'should be defined' do
- # assert_respond_to @klarna, :send_invoice
- # end
-
- # it 'should raise error for snail-mail request of an existing but un-activated invoice' do
- # assert_raises ::Klarna::API::Errors::KlarnaServiceError do
- # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- # @klarna.send_invoice(invoice_no)
- # end
- # end
-
- # it 'should successfully accept snail-mail request of an existing and activated invoice' do
- # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- # invoice_url = @klarna.activate_invoice(invoice_no)
-
- # assert_equal invoice_no, @klarna.send_invoice(invoice_no)
- # end
- # end
-
- # Spec: http://integration.klarna.com/en/api/invoice-handling-functions/functions/mkartno
+ # Spec:
+ # http://integration.klarna.com/en/api/invoice-handling-functions/functions/returnamount
+ describe '#return_amount' do
+ it 'should be defined' do
+ assert_respond_to @klarna, :return_amount
+ end
+
+ it 'should raise error for non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ amount = 100
+ vat = 25
+ @klarna.return_amount(INVALID_ORDER_NO, amount, vat)
+ end
+ end
+
+ it 'should raise error for existing but un-activated invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ amount = 100
+ vat = 25
+
+ assert_equal invoice_no, @klarna.return_amount(invoice_no, amount, vat)
+ end
+ end
+
+ # FAILS: Klarna API 2.0 don't support this for test-accounts. :(
+ it 'should successfully return amount for an activated invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # amount = 100
+ # vat = 25
+ #
+ # assert_equal invoice_no, @klarna.credit_invoice(invoice_no, credit_no)
+ # end
+ end
+
+ # Spec:
+ # http://integration.klarna.com/en/api/invoice-handling-functions/functions/returnamount
+ describe '#credit_invoice' do
+ it 'should be defined' do
+ assert_respond_to @klarna, :credit_invoice
+ end
+
+ describe 'full' do
+ it 'should raise error for non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ credit_no = ''
+ @klarna.credit_invoice(INVALID_ORDER_NO, credit_no)
+ end
+ end
+
+ it 'should raise error for existing but un-activated invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ credit_no = ''
+ @klarna.credit_invoice(invoice_no, credit_no)
+ end
+ end
+
+ # FAILS: Klarna API 2.0 don't support this for test-accounts. :(
+ it 'should successfully credit an activated invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # invoice_url = @klarna.activate_invoice(invoice_no)
+ # credit_no = ''
+ #
+ # assert_equal invoice_no, @klarna.credit_invoice(invoice_no, credit_no)
+ # end
+ end
+
+ describe 'partial' do
+ it 'should raise error for existing but un-activated invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ refund_articles = [@order_items.first]
+ credit_no = ''
+ @klarna.credit_invoice(invoice_no, credit_no, refund_articles)
+ end
+ end
+
+ it 'should successfully credit an activated invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # invoice_url = @klarna.activate_invoice(invoice_no)
+ # refund_articles = [@order_items.first]
+ # credit_no = ''
+ #
+ # assert_equal invoice_no, @klarna.credit_invoice(invoice_no, credit_no, refund_articles)
+ # end
+ end
+ end
+
+ # Spec:
+ # http://integration.klarna.com/en/api/invoice-handling-functions/functions/emailinvoice
+ describe '#email_invoice' do
+ it 'should be defined' do
+ assert_respond_to @klarna, :email_invoice
+ end
+
+ it 'should raise error for e-mail request of an existing but un-activated invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ @klarna.email_invoice(invoice_no)
+ end
+ end
+
+ # FAILS: Klarna API 2.0 don't support this for test-accounts. :(
+ it 'should successfully accept email request of an activated invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # invoice_url = @klarna.activate_invoice(invoice_no)
+ #
+ # assert_equal invoice_no, @klarna.email_invoice(invoice_no)
+ # end
+ end
+
+ # Spec:
+ # http://integration.klarna.com/en/api/invoice-handling-functions/functions/sendinvoice
+ describe '#send_invoice' do
+ it 'should be defined' do
+ assert_respond_to @klarna, :send_invoice
+ end
+
+ it 'should raise error for snail-mail request of an existing but un-activated invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ @klarna.send_invoice(invoice_no)
+ end
+ end
+
+ # FAILS: Klarna API 2.0 don't support this for test-accounts. :(
+ it 'should successfully accept snail-mail request of an activated invoice' # do
+ # invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ # invoice_url = @klarna.activate_invoice(invoice_no)
+ #
+ # assert_equal invoice_no, @klarna.send_invoice(invoice_no)
+ # end
+ end
+
+ # Spec:
+ # http://integration.klarna.com/en/api/invoice-handling-functions/functions/mkartno
describe '#make_article' do
it 'should be defined' do
assert_respond_to @klarna, :make_article
@@ -164,19 +262,20 @@
end
end
- # Spec: http://integration.klarna.com/en/api/other-functions/functions/updategoodsqty
+ # Spec:
+ # http://integration.klarna.com/en/api/other-functions/functions/updategoodsqty
describe '#update_goods_quantity' do
it 'should be defined' do
assert_respond_to @klarna, :update_goods_quantity
end
- it 'should raise error for an invalid/non-existing invoice' do
+ it 'should raise error for an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
@klarna.update_goods_quantity(INVALID_ORDER_NO, 'ABC1', 10)
end
end
- it 'should raise error for an invalid/non-existing article-no for a valid/existing invoice' do
+ it 'should raise error for an non-existing article-no for an existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
@@ -184,28 +283,29 @@
end
end
- it 'should successfully update goods quantity for a valid/existing invoice and valid article-no' do
+ it 'should successfully update goods quantity for an existing invoice and valid article-no' do
invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
assert_equal invoice_no, @klarna.update_goods_quantity(invoice_no, 'ABC1', 10)
end
end
- # Spec: http://integration.klarna.com/en/api/other-functions/functions/updatechargeamount
+ # Spec:
+ # http://integration.klarna.com/en/api/other-functions/functions/updatechargeamount
describe '#update_charge_amount' do
it 'should be defined' do
assert_respond_to @klarna, :update_charge_amount
end
- it 'should raise error for an invalid/non-existing invoice' do
+ it 'should raise error for an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
@klarna.update_charge_amount(INVALID_ORDER_NO, 1, 10.00 * 100)
end
end
- # it 'should successfully update shipment fee for a valid/existing invoice'
+ it 'should successfully update shipment fee for an existing invoice'
- # it 'should successfully update handling fee for a valid/existing invoice'
+ it 'should successfully update handling fee for an existing invoice'
end
# Spec: http://integration.klarna.com/en/api/other-functions/functions/updateorderno
@@ -214,33 +314,35 @@
assert_respond_to @klarna, :update_order_no
end
- it 'should raise error for an invalid/non-existing invoice' do
+ it 'should raise error for an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
@klarna.update_order_no(INVALID_ORDER_NO, '123')
end
end
- # Raises error, but don't know why...
- # it 'should successfully update order-no for a valid/existing invoice' do
+ # FIXME: Throws "invno"-error - don't know why. :S
+ it 'should successfully update order-no for an existing invoice' # do
# invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- #
- # assert_equal "?", @klarna.update_order_no(invoice_no, (invoice_no.to_i + 1).to_s)
+ # new_invoice_no = (invoice_no.to_i + 1).to_s
+
+ # assert_equal new_invoice_no, @klarna.update_order_no(invoice_no, new_invoice_no)
# end
end
- # Spec: http://integration.klarna.com/en/api/other-functions/functions/invoiceaddress
+ # Spec:
+ # http://integration.klarna.com/en/api/other-functions/functions/invoiceaddress
describe '#invoice_address' do
it 'should be defined' do
assert_respond_to @klarna, :invoice_address
end
- it 'should raise error for an invalid/non-existing invoice' do
+ it 'should raise error for an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
@klarna.invoice_address(INVALID_ORDER_NO)
end
end
- it 'should successfully return the address for a valid/existing invoice' do
+ it 'should successfully return the address for an existing invoice' do
invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
assert_equal ["Karl", "Lidin", "Junibacksg 42", "23634", "Hollviken", 'SE'], @klarna.invoice_address(invoice_no)
@@ -255,19 +357,35 @@
assert_respond_to @klarna, :invoice_amount
end
- it 'should raise error for an invalid/non-existing invoice' do
- assert_raises ::Klarna::API::Errors::KlarnaServiceError do
- @klarna.invoice_amount(INVALID_ORDER_NO)
+ describe 'full' do
+ it 'should raise error for an non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ @klarna.invoice_amount(INVALID_ORDER_NO)
+ end
end
- end
- it 'should successfully return the invoice amount for a valid/existing invoice' do
- invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ it 'should successfully return the invoice amount for an existing invoice' do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
- assert_equal @order_items_total, @klarna.invoice_amount(invoice_no)
+ assert_equal @order_items_total, @klarna.invoice_amount(invoice_no)
+ end
end
- # it 'should successfully return the invoice amount for parts of an existing invoice if articles are specified as well'
+ describe 'partial' do
+ it 'should raise error for an non-existing invoice' do
+ assert_raises ::Klarna::API::Errors::KlarnaServiceError do
+ articles = [@order_items.last]
+ @klarna.invoice_amount(INVALID_ORDER_NO, articles)
+ end
+ end
+
+ it 'should successfully return the invoice amount for an existing invoice' do
+ invoice_no = @klarna.add_invoice(*@valid_invoice_args_SE)
+ articles = [@order_items.last]
+
+ assert_equal 7*(17.00 * 100), @klarna.invoice_amount(invoice_no, articles)
+ end
+ end
end
describe '#invoice_paid?' do
@@ -275,7 +393,7 @@
assert_respond_to @klarna, :invoice_paid?
end
- it 'should raise error for an invalid/non-existing invoice' do
+ it 'should raise error for an non-existing invoice' do
assert_raises ::Klarna::API::Errors::KlarnaServiceError do
@klarna.invoice_paid?(INVALID_ORDER_NO)
end
View
2  test/klarna/api/methods/reservation_test.rb
@@ -3,7 +3,7 @@
describe Klarna::API::Methods::Reservation do
- # TODO: Mock responses.
+ # TODO: Mock responses using VCR.
before do
valid_credentials!
View
19 test/klarna/api/methods/standard_test.rb
@@ -3,7 +3,7 @@
describe Klarna::API::Methods::Standard do
- # TODO: Mock responses.
+ # TODO: Mock responses using VCR.
before do
valid_credentials!
@@ -19,7 +19,8 @@
expose_protected_methods_in @klarna.class
end
- # Spec: http://integration.klarna.com/en/api/standard-integration/functions/getaddresses
+ # Spec:
+ # http://integration.klarna.com/en/api/standard-integration/functions/getaddresses
describe '#get_addresses' do
it 'should be defined' do
assert_respond_to @klarna, :get_addresses
@@ -66,7 +67,8 @@
end
end
- # Spec: http://integration.klarna.com/en/api/standard-integration/functions/mkaddr
+ # Spec:
+ # http://integration.klarna.com/en/api/standard-integration/functions/mkaddr
describe '#make_address' do
it 'should be defined' do
assert_respond_to @klarna, :make_address
@@ -128,7 +130,8 @@
end
end
- # Spec: http://integration.klarna.com/en/api/standard-integration/functions/mkgoodsflags
+ # Spec:
+ # http://integration.klarna.com/en/api/standard-integration/functions/mkgoodsflags
describe '#make_goods' do
it 'should be defined' do
assert_respond_to @klarna, :make_goods
@@ -225,11 +228,17 @@
end
end
- # Spec: http://integration.klarna.com/en/api/advanced-integration/functions/hasaccount
+ # Spec:
+ # http://integration.klarna.com/en/api/advanced-integration/functions/hasaccount
describe '#has_account?' do
it 'should be defined' do
assert_respond_to @klarna, :has_account?
end
+
+ # FIXME: Throws error "Unknown call (-99)". :S
+ it 'should be true' # do
+ # assert_equal true, @klarna.has_account?("4304158399", :SE)
+ # end
end
end
View
6 test/test_helper.rb
@@ -15,8 +15,8 @@
include Klarna::AssertionsHelper
end
-VALID_STORE_ID = ENV['KLARNA_ESTORE_ID'].presence || 2 # NOTE: This estore-id used to work for testing until 2011.09.
-VALID_STORE_SECRET = ENV['KLARNA_ESTORE_SECRET'].presence || 'lakrits' # NOTE: This estore-secret used to work for testing until 2011.09.
+VALID_STORE_ID = ENV['KLARNA_ESTORE_ID'].presence
+VALID_STORE_SECRET = ENV['KLARNA_ESTORE_SECRET'].presence
VALID_COUNTRY = :SE
Klarna.store_config_file = File.join(File.dirname(__FILE__), 'fixtures', 'klarna.yml')
@@ -42,7 +42,7 @@ def valid_credentials!
c.store_id = VALID_STORE_ID.to_i
c.store_secret = VALID_STORE_SECRET
c.country = VALID_COUNTRY
- c.mode = :production # NOTE: Actually not production unless test-mode is disabled in the Klarna admin pages.
+ c.mode = ENV['KLARNA_MODE'].presence || :test
c.http_logging = (ENV['KLARNA_DEBUG'].to_s =~ /(true|1)/) || false
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.