npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

klarna-checkout-sdk

v1.2.6

Published

Klarna checkout `klarna-checkout-sdk` npm package for Node.js.

Downloads

8

Readme

Klarna checkout SDK

Klarna checkout klarna-checkout-sdk npm package for Node.js.

Promise based client for node.js, for interaction with Klarna checkout API V3.

Setting up Klarna Merchant playground portal

Sign up to playground Klarna Merchant portal & and Create your Klarna checkout API credentials under Settings.

 Install

You can install klarna-checkout-sdk with npm, yarn or pnpm.

Using npm:

npm install klarna-checkout-sdk --save

Using yarn:

yarn add klarna-checkout-sdk

Using pnpm:

pnpm add klarna-checkout-sdk

Usage

klarna-checkout-sdk is shipped with full support for TypeScript, ESM Modules and Commonjs. This means that package can be used with import & export syntax and module.exports & require syntax.

Usage with TypeScript

Example code:

import { initialize, createOrder, getOrder, updateOrder, markAnOrderAsAborted, IKlarnaSDK, IKlarnaOrder } from 'klarna-checkout-sdk';

/** initialize klarna SDK, with your credentials, that you can get from playground merchant portal settings page. */
const klarnaSDK: IKlarnaSDK = initialize(
    {
        eid: process.env.EID,
        secret: process.env.KLARNA_SECRET,
        live: process.env.NODE_ENV === 'production' ? true : false
    }
);

const order: IKlarnaOrder = {
            order_lines: [
                {
                    reference: 'test-id-one',
                    name: 'test product',
                    unit_price: 2,
                    quantity: 1,
                    total_tax_amount: 0.39,
                    total_discount_amount: 0,
                    total_amount: 2,
                    tax_rate: 2400,
                    image_uri:
                        'https://res.cloudinary.com/image/upload/v1662557938/dpcd0a3jqbbozy1ygmxt.jpg',
                    uri: 'https://teststore.com/lp:t/test-id-one',
                    type: 'physical',
                },
            ],
            order_amount: 2,
            order_tax_amount: 0.39,
            purchase_country: 'US',
            purchase_currency: 'USD',
            locale: 'us-US',
            merchant_urls: {
                checkout: 'http://localhost:3000/ostoskori',
                confirmation: 'http://localhost:3000/ostoskori/vahvistus',
                push: 'http://localhost:8080/klarna/push',
                terms: 'http://localhost:3000/ehdot',
            },
            billing_adress: {
                given_name: 'John',
                family_name: 'Doe',
                email: '[email protected]',
                street_address: 'Johnintie 4 B 67',
                postal_code: '00910',
                city: 'Helsinki',
                phone: '+358401234567',
                country: 'FI',
            },
            shipping_address: {
                given_name: 'John',
                family_name: 'Doe',
                email: '[email protected]',
                street_address: 'Johnintie 4 B 67',
                postal_code: '00910',
                city: 'Helsinki',
                phone: '+358401234567',
                country: 'FI',
            },
        };
        /** Create order and render payment info in the client from html_snippet property. */
        const newKlarnaOrder = await createOrder(klarnaSDK, order);
        /** Get order and render checkout snippet. */
        const klarnaOrder = await getOrder(klarnaSDK, newKlarnaOrder.order_id);
        /** Update order and render checkout snippet. */
        const updatedKlarnaOrder = await updateOrder(klarnaSDK, klarnaOrder);
        /** Mark an order as aborted and render confirmation snippet. */
        const abortedOrder = await markAnOrderAsAborted(klarnaSDK, klarnaOrder.order_id);

Example with Express, Node.js & TypeScript

klarna.ts

import { IKlarnaSDK, initialize } from 'klarna-checkout-sdk';

const klarna = (): IKlarnaSDK => {
    return initialize({
        eid: process.env.EID,
        secret: process.env.KLARNA_SECRET,
        live: process.env.NODE_ENV === 'production' ? true : false,
    });
};

export default klarna;

routes/klarna.routes.ts

import { Router, Request, Response } from 'express';
import klarna from '../klarna';
import { createOrder, getOrder, IKlarnaOrder } from 'klarna-checkout-sdk';

const router = Router();

router.post(
    '/klarna/create-order',
    async (request: Request, response: Response) => {
        try {
            const klarnaSDK = klarna();
            const klarnaOrder = request.body as IKlarnaOrder;
            const newOder = await createOrder(klarnaSDK, klarnaOrder);
            return response.status(201).json({ order: newOder });
        } catch (error) {
            return response
                .status(500)
                .json({ message: 'some error.' });
        }
    },
);
router.post('/klarna/push', async (request: Request, response: Response) => {
    try {
        const orderId = request.query.klarna_order_id as unknown as string;
        const klarnaSDK = klarna();
        const klarnaOrder = await getOrder(klarnaSDK, orderId);
        return response.status(201).json({ order: klarnaOrder });
    } catch (error) {
         return response
                .status(500)
                .json({ message: 'some error.' });
    }
});
router.get(
    '/klarna/get-order/:orderId',
    async (request: Request, response: Response) => {
        try {
            const orderId = request.params.orderId;
            const klarnaSDK = klarna();
            const klarnaOrder = await getOrder(klarnaSDK, orderId);
            return response.status(201).json({ order: klarnaOrder });
        } catch (error) {
             return response
                .status(500)
                .json({ message: 'some error.' });
        }
    },
);

export default router;

Usage with Commonjs

Example code:

const {
    initialize,
    createOrder,
    getOrder,
    updateOrder,
    markAnOrderAsAborted,
} = require('klarna-checkout-sdk');

/** initialize klarna SDK, with your credentials, that you can get from playground merchant portal settings page. */
const klarnaSDK = initialize({
    eid: process.env.EID,
    secret: process.env.KLARNA_SECRET,
    live: process.env.NODE_ENV === 'production' ? true : false,
});

const order = {
    order_lines: [
        {
            reference: 'test-id-one',
            name: 'test product',
            unit_price: 2,
            quantity: 1,
            total_tax_amount: 0.39,
            total_discount_amount: 0,
            total_amount: 2,
            tax_rate: 2400,
            image_uri:
                'https://res.cloudinary.com/image/upload/v1662557938/dpcd0a3jqbbozy1ygmxt.jpg',
            uri: 'https://teststore.com/lp:t/test-id-one',
            type: 'physical',
        },
    ],
    order_amount: 2,
    order_tax_amount: 0.39,
    purchase_country: 'US',
    purchase_currency: 'USD',
    locale: 'us-US',
    merchant_urls: {
        checkout: 'http://localhost:3000/ostoskori',
        confirmation: 'http://localhost:3000/ostoskori/vahvistus',
        push: 'http://localhost:8080/klarna/push',
        terms: 'http://localhost:3000/ehdot',
    },
    billing_adress: {
        given_name: 'John',
        family_name: 'Doe',
        email: '[email protected]',
        street_address: 'Johnintie 4 B 67',
        postal_code: '00910',
        city: 'Helsinki',
        phone: '+358401234567',
        country: 'FI',
    },
    shipping_address: {
        given_name: 'John',
        family_name: 'Doe',
        email: '[email protected]',
        street_address: 'Johnintie 4 B 67',
        postal_code: '00910',
        city: 'Helsinki',
        phone: '+358401234567',
        country: 'FI',
    },
};
/** Create order and render payment info in the client from html_snippet property. */
const newKlarnaOrder = await createOrder(klarnaSDK, order);
/** Get order and render checkout snippet. */
const klarnaOrder = await getOrder(klarnaSDK, newKlarnaOrder.order_id);
/** Update order and render checkout snippet. */
const updatedKlarnaOrder = await updateOrder(klarnaSDK, klarnaOrder);
/** Mark an order as aborted and render confirmation snippet. */
const abortedOrder = await markAnOrderAsAborted(
    klarnaSDK,
    klarnaOrder.order_id,
);