Home > klarna-ruby

klarna-ruby

Klarna-ruby is a project mainly written in Ruby, based on the MIT license.

A Ruby wrapper for Klarna XML-RPC API.

h1. KLARNA "!https://secure.travis-ci.org/merchii/klarna-ruby.png!":http://travis-ci.org/merchii/klarna-ruby

A Ruby wrapper for the Klarna XML-RPC API.

h2. What is Klarna?

"Klarna":http://klarna.com is a payment solution with focus on e-commerce. Visit "klarna.com":http://klarna.com for more information, or why not "Wikipedia: Klarna":http://en.wikipedia.org/wiki/Klarna. We might be biased, but at "Merchii":http://merchii.com we currently consider Klarna the most convenient online payment solution that exists - and that's why we lean on it.

Supported countries:

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:

  $ gem install klarna

...or if you use "Bundler":http://gembundler.com:

@Gemfile@:

  gem 'klarna'

...and @$ bundle install@.

h2. Usage

Trivial example looking up an address using - in this case - a Swedish SSN:

  require 'rubygems'
  require 'klarna'

Klarna.configure do |c| c.store_id = 123 c.store_secret = 'secret'

c.mode = :test
c.http_logging = false

end

client = Klarna::API::Client.new

client.get_addresses("430415-8399", :SE)

See the code/docs for more information.

h2. Testing

A couple of ways...

Specs

To run unit/integration tests, simply do:

  $ bundle exec rake test

...or if you use "Guard":http://github.com/guard/guard:

  $ bundle exec guard

Dummie client

  $ cd examples
  $ ruby console.rb

Web/Sinatra app

  1. Start the server...
  $ cd examples
  $ ruby web.rb
  ...
  >> Listening on 0.0.0.0:4567, CTRL+C to stop
  1. Open it up!

@http://localhost:4567@

NOTE: The web app actions/forms are probably completely broken currently as it hasn't been updated to reflect the current library implementation.

h2. Debugging

HTTP-debugging in Wireshark

Ensure port @4567@ (port used for Klarna API integration test/debugging) is in the list of captured ports in the HTTP/HTTPS preferences, or these requests/responses will not be captured. Note: Port @80@ (port used for Klarna API integration in production) is captured by default.

XML-RPC-debugging in a XML-RPC client

Try this:

Endpoint: @http://beta-test.klarna.com:4567@ Method: @get_addresses@ Params: @["1.0","ruby/xmlrpc","430415-8399",2,"AY2qnVNi8ccTsuDw2XNovQ==",2,2]@

h2. References

  • "Klarna API Documentation":http://integration.klarna.com/en/api/step-by-step
  • "XML-RPC Overview":http://en.wikipedia.org/wiki/XML-RPC

h2. Tools

  • "XML-RPC Client":http://ditchnet.org/xmlrpc/ for XML-RPC debugging on OS X.
  • "Wireshark":http://wireshark.org for platform-independent protocol sniffing, useful for debugging XML-RPC requests/responses.

h2. Notes

This gem was developed for our own requirements at "Merchii":http://github.com/merchii, so feel free to send pull-requests with enhancements of any kind (features, bug-fixes, documentation, tests, etc.) to make it better or useful for you as well.

h2. To-do

See the "TODO":https://github.com/merchii/klarna-ruby/blob/master/TODO.

h2. License

Released under the MIT license. Copyright (c) "Jonas Grimfelt":http://github.com/grimen, "Merchii":http://merchii.com