This integration enables Solidus to provide Klarna Payments as a payment option.
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.
release_remaining_amount
to Klarna in this case and the user's credit limit would still be blocked.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.
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.
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, allowing the customer to complete the checkout through a subscription. You can find 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 reccomended for Klarna Payments, it should be disabled. You can also configure to automatically capture the payments when the customer confirms their order.
Note: After you run solidus_klarna_payments:install
the initializer in config/initializers/solidus_klarna_payments.rb
allows some configuration. It's recommended to avoid modifying the file unless you're sure of what you're doing.
Per default this integration will always try to connect to the URL Endpoint for North America (Test or Production). This will not work with klarna merchant accounts from other zones (you will get an 401 unauthorized). For more Information about the Zone Endpoint URLs see https://docs.klarna.com/api/api-urls/
To configure the Zone for the endpoint you can just change the zone in an initializer to europe
, oceania
or us
like so:
Klarna.configure do |config|
config.zone = :europe
end
The integration adds the necessary code to the checkout. It consists primarily of three parts:
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.
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) is modified, it might 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/api/session"),
// The submit button that triggers the authorization. This button will be disabled while
// the autorization is issued.
submitButton: $("form.edit_order :submit")
})
For more information see Klarna's Developers Portal.
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.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)