Skip to content

sdtechdev/solidus_klarna_payments

 
 

Repository files navigation

Klarna Payments Integration for Solidus

Klarna

This integration enables Solidus to provide Klarna Payments as a payment option.

Checkout

The example screen shot above shows a sample US Klarna Payments offer. Klarna Payments offers are specific to each market and merchant. Work with Klarna to determine what offers are appropriate for your store.

Features

  • Integrates seamlessly as a payment gateway
  • Supports auto capture
  • Supports partial captures, refunds, and partial refunds
  • Configurable design
  • ActiveMerchant interface for Klarna Payments
  • Tokenization to enable subscriptions

Limitations

  • Auto capturing payments requires prior Klarna approval.
  • Multiple captures for one authorization are currently not supported because of Solidus's process when capturing payments. This might change in future versions of Solidus and this gem respectively. However, it is possible to use the Klarna Merchant Portal to do that.
  • A customer is able to choose multiple payment options for an order. If an order does have multiple payment options, you should capture the most recent payment choice first, which be listed at the bottom of the list of payments.
  • It’s important to cancel Klarna payments if the customer paid with another payment method in the end after receiving a successful Klarna authorization, we can not send release_remaining_amount to Klarna in this case and the users credit limit would still be blocked.

Supported Solidus Versions

  • Solidus 1.3.x
  • Solidus 1.4.x
  • Solidus 2.0.x
  • Solidus 2.1.x
  • Solidus 3.1.x

Installation

Add solidus_klarna_payments to your Gemfile:

gem 'solidus_klarna_payments', github: 'solidusio-contrib/solidus_klarna_payments'

Bundle your dependencies and run the installation generator:

$ bin/rails generate solidus_klarna_payments:install

Please note that the version is still 0.9. We want to have the option to make breaking changes during the transition to 1.0.

Solidus configuration

After the installation, create a new payment method and select Spree::PaymentMethod::KlarnaPayment as the gateway. After saving the payment method, you can configure your Klarna credentials and set design options for how Klarna is displayed to the customer in the checkout.

Configuration

The "country" option is mandatory and refers to the region the account is associated with. In the example above it's us for the USA, other values would be uk for the United Kingdom and de for Germany.

The "tokenization" option is false by default and it is needed to enable the tokenization feature for this payment method. This option will change the flow and allowing the customer to complete the checkout making a subscription. You can found more information here

There are two other things to configure. Set the payment method to "active" and only enable it in the frontend. Some payment methods can be used in the backend by the merchant. As this is not appropriate for Klarna Payments, it should be disabled. You can also configure to automatically capture the payments when the customer confirms their order.

Configuration

Note: After you ran solidus_klarna_payments:install the initializer in config/initializers/solidus_klarna_payments.rb allows some configuration. It's usually not necessary to touch the file unless you're sure what you're doing.

Guest checkout

Tokenization require a user to be present to allow them to be managed after they are purchased.

Because of this, you must disable guest checkout if you want to use the klarna payment tokenization flow.

An example would be adding this to the registration page:

config/initializers/spree.rb

Spree.config do |config|
  config.allow_guest_checkout = false
end

Customize how to retrieve the user customer token locally

By default, the extension gets the user customer token from the Spree::User model. If you want to override this logic, you can provide your own retrieve_customer_token_service_class.

Customize how to store the user customer token

By default, the extension stores the user customer token inside the Spree::User model. If you want to override this logic, you can provide your own store_customer_token_service_class.

To know more about the customer token follow this link.

Technical information

The integration adds the necessary code to the checkout. It consists of mainly of three parts:

  • a template to display the iframe when Klarna is selected as the payment method
  • a JavaScript library/jQuery plugin to handle sessions and authorisation
  • a CSS file with very little code to display the iframe correctly

The template can be overwritten by copying the file to app/views/spree/checkout/payment/_klarna_credit.html.erb in the main application. Note the initialisation JS code which is required for the integration to work.

JavaScript library

The JavaScript library is used to initialize a session with Klarna, authorize the requested amount and handing the obtained authorization token to Solidus. This token is later used when authorizing the payment in Solidus.

If the checkout (template) was modified, it can be necessary to adapt the integration as well. The KlarnaGateway library was extracted to make that easier. It is initialized as follows:

KlarnaGateway.loadSdk(this, document, function() {
  jQuery(".klarna_credit_box").klarnaAuthorize();
});

loadSdk will load the Klarna JavaScript SDK from the CDN and call the provided callback function when the file was loaded. Note that it's also possible to do that manually by including the file with a <script> tag. Please see the JavaScript SDK documentation for more details.

The initialization is done in klarnaAuthorize() on the payment step page in the checkout. It will create a session with Klarna and load the iframe when Klarna is selected by the user. It is called on the container (.klarna_credit_box in this case) in which the iframe will be inserted.

To make customizations easier klarnaAuthorize() takes some optional arguments. Here they are with their defaults:

$(container).klarnaAuthorize({
  // Element that stores the authorization token. This will usually be a hidden input
  // field for the payment source.
  authorizationToken: $("#klarna_authorization_token", this),

  // The form that is submitted on the payment page. The submit event is prevented so
  // the payment can be authorized before progressing any further.
  form: $("#checkout_form_payment"),

  // Load the Klarna iframe directly after page load, not only when Klarna was selected.
  loadDirectly: false,

  // Function to perform when the form is being submitted. It is executed before the authorization
  // is being requested.
  onSubmit: function(settings) {},

  // Function to perform when the authorization process is aborted, e.g. because there was an
  // error or the user did not input required information.
  onAbort: function(settings) {},

  // Elements that trigger a check whether Klarna is selected. The iframe is only loaded
  // when Klarna is selected
  paymentChangedElements: $("input[name=\"order[payments_attributes][][payment_method_id]\"]"),

  // Where to find the payment method id. This is necessary if there are more than one Klarna
  // payment methods in the store.
  paymentId: $(this).data("payment-method-id"),

  // Wraper for the whole payment method. This is being hidden when Klarna returns with `show_form == false`.
  paymentMethodWrapper: $(".form-payment-method-klarna_credit"),
  // The session URL of the store. The store needs to create a session from the server
  // side. This should not be changed.
  sessionUrl: Spree.pathFor("/solidus_klarna_payments/session"),

  // The submit button that triggers the authorization. This button will be disabled while
  // the autorization is issued.
  submitButton: $("form.edit_order :submit")
})

Klarna API documentation

For more information see Klarna's Developers Portal.

Contributing

Contributions are always welcome. If you find a bug or have a suggestion, please open a ticket on Github. If you want to contribute code directly, just open a pull request and describe your change.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Klarna Payments gateway for Solidus.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 84.4%
  • HTML 9.9%
  • JavaScript 3.8%
  • Shell 1.4%
  • SCSS 0.5%