@dintero/checkout-web-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.8.12 • Public • Published

Dintero Checkout JavaScript SDK for frontend applications Actions Status npm latest version

Use this SDK in your frontend application to

  • embed an existing Dintero Checkout payment session in an iframe on your website
  • redirect the end user to the full page version of the Dintero Checkout for an existing payment session

Note that this SDK is for redirecting or embedding existing payment sessions. You cannot use this SDK to send requests to the Checkout API or to crate new payment sessions.

Learn more about the Dintero Checkout at docs.dintero.com

Before you start

We cannot guarantee the delivery of events from the embedded checkout to the SDK client runtime. The sessions machine-to-machine callback_url will be delivered at least once. Read more about the callback_url parameter in our api spec.

For payments on devices with the Vipps app installed, after payment is completed in the Vipps app, the end user will be returned to the browser where the return_url on the payment is opened in a new browser tab leaving the site that has the embedded checkout still open in a background browser tab on the device. In this case the SDK cannot guarantee that the handlers for onPaymentAuthorized or onPaymentError will be called.

If no custom handler are added for onPaymentError, onPaymentAuthorized and onPaymentCanceled the SDK will redirect the user to the return_url in the payment session.

Installation

NPM package

npm install @dintero/checkout-web-sdk

Using the SDK for an embedded checkout

The Dintero Checkout will be added to the <div id="checkout-container"></div> DOM-node.

Minimal example

When payment is completed, the SDK will redirect the end user to the return_url defined in the payment session.

<script type="text/javascript">
    const container = document.getElementById("checkout-container");

    dintero.embed({
        container,
        sid: "T11223344.<short-uuid>",
    });
</script>

Full inline HTML JavaScript example

The checkout sdk will add a polyfill for promises if the browser does not support promises natively.

<script type="text/javascript">
    const container = document.getElementById("checkout-container");

    dintero
        .embed({
            container,
            sid: "T11223344.<short-uuid>",
            popOut: false, // optional parameter to enable pop out mode
            language: "no", // optional parameter, an ISO 639-1 two-letter language code
            onSession: function (event, checkout) {
                console.log("session", event.session);
            },
            onPayment: function (event, checkout) {
                console.log("transaction_id", event.transaction_id);
                console.log("href", event.href);
                checkout.destroy();
            },
            onPaymentError: function (event, checkout) {
                console.log("href", event.href);
                checkout.destroy();
            },
            onSessionCancel: function (event, checkout) {
                console.log("href", event.href);
                checkout.destroy();
            },
            onSessionNotFound: function (event, checkout) {
                console.log("session not found (expired)", event.type);
                checkout.destroy();
            },
            onSessionLocked: function (event, checkout, callback) {
                console.log("pay_lock_id", event.pay_lock_id);
                callback(); // refresh session
            },
            onSessionLockFailed: function (event, checkout) {
                console.log("session lock failed");
            },
            onActivePaymentType: function (event, checkout) {
                console.log(
                    "payment product type selected",
                    event.payment_product_type,
                );
            },
            onValidateSession: function (event, checkout, callback) {
                console.log("validating session", event.session);
                callback({
                    success: true,
                    clientValidationError: undefined,
                });
            },
        })
        .then(function (checkout) {
            console.log("checkout", checkout);
        });
</script>

Typescript example

import {
    embed,
    SessionLoaded,
    SessionUpdated,
    SessionPayment,
    SessionPaymentError,
    SessionCancel,
    SessionNotFound,
} from "@dintero/checkout-web-sdk";

const container = document.getElementById("checkout-container");

const checkout = await embed({
    container,
    sid: "T11223344.<short-uuid>",
    popOut: false, // optional parameter to enable pop out mode
    language: "no", // optional parameter, an ISO 639-1 two-letter language code
    onSession: (event: SessionLoaded | SessionUpdated) => {
        console.log("session", event.session);
    },
    onPayment: (
        event: SessionPaymentAuthorized | SessionPaymentOnHold,
        checkout,
    ) => {
        console.log("transaction_id", event.transaction_id);
        console.log("href", event.href);
        checkout.destroy();
    },
    onPaymentError: (event: SessionPaymentError, checkout) => {
        console.log("href", event.href);
        checkout.destroy();
    },
    onSessionCancel: (event: SessionCancel, checkout) => {
        console.log("href", event.href);
        checkout.destroy();
    },
    onSessionNotFound: (event: SessionNotFound, checkout) => {
        console.log("session not found (expired)", event.type);
        checkout.destroy();
    },
    onSessionLocked: (event: SessionLocked, checkout, callback) => {
        console.log("pay_lock_id", event.pay_lock_id);
        callback(); // refresh session
    },
    onSessionLockFailed: (event: SessionLockFailed, checkout) => {
        console.log("session lock failed");
    },
    onActivePaymentType: function (event: ActivePaymentProductType, checkout) {
        console.log(
            "payment product type selected",
            event.payment_product_type,
        );
    },
    onValidateSession: function (event: ValidateSession, checkout, callback) {
        console.log("validating session", event.session);
        callback({
            success: true,
            clientValidationError: undefined,
        });
    },
});

Setting payment product type

The payment product type can be set with the returned setActivePaymentProductType()function when embedding the checkout.

Select "vipps" payment product type:

checkout.setActivePaymentProductType("vipps");

Resetting selection (so no option is selected in the checkout):

checkout.setActivePaymentProductType();

Updating an Checkout Express-session

To update an existing Checkout Express-session, follow these steps:

  1. Lock the session with the SDK
  2. Perform a server-to-server session update
  3. Refresh the session with the SDK

Locking the session

Call lockSession on the checkout object:

checkout
    .lockSession()
    .then(function (sessionLockedEvent) {
        // initiate server side session update and then refresh the session
    })
    .catch(function (sessionLockFailedEvent) {
        // handle failure to lock
    });

lockSession() returns a promise that is resolved when the SessionLocked event is received from the checkout or rejected if the SessionLockFailed event is received.

When the session is successfully locked, you'll get a callback at onSessionLocked. If locking the session fails, there will be a callback at onSessionLockFailed.

While the session is locked, all editing and paying in the checkout is disabled.

Perform a server-to-server session update

See session update for details on what parts of the session can be updated, and how.

Refreshing the session

After updating the session, call refreshSession on the checkout object:

checkout.refreshSession();

or use the callback in onSessionLocked:

onSessionLocked: (event, checkout, callback) => {
    console.log("pay_lock_id", event.pay_lock_id);
    callback(); // refresh session
};

refreshSession() returns a promise that is resolved when the SessionUpdated event is received from the checkout.

Editing and paying in the checkout is enabled again when a session without a pay_lock is loaded by the checkout.

Validating session before payment

To validate the session and perform actions before the session is paid, use the onSessionValidation-handler.

The checkout will be locked and payment will be paused until the provided callback function is called, or checkout.submitValidationResult is called with the result.

When validated successfully, return a successful result:

{
    success: true;
}

If the validation is not successful, return the result with an error message:

{
   success: false,
   clientValidationError: "session is not in sync with cart"
}

Example implementation:

onValidateSession: function(event, checkout, callback) {
     // Call the ecommerce solution to make sure the session is sync with the cart
     callback({
         success: false,
         clientValidationError: "session is not in sync with cart",
     });
},

Using the SDK for a redirect checkout

The user is redirected to the Dintero Checkout to complete payment.

import { redirect } from "dintero-checkout-web-sdk";

const checkout = redirect({
    sid: "T11223344.<short-uuid>",
});

Bugs

Bugs can be reported to https://github.com/Dintero/Dintero.Checkout.Web.SDK/issues

Security

Contact us at security@dintero.com

Browser support

All major browsers above version N - 1, where N is the most recent version. For Internet Explorer, only version 11 is supported.

The SDK includes a polyfill for promises that is added to the global scope if promises are not supported by the browser.

Building from source

yarn install
yarn run build

The Dintero Checkout SDK is built with microbundle.

Creating a new release

  1. Enforce all commits to the master branch to be formatted according to the Angular Commit Message Format
  2. When merged to master, it will automatically be released with semantic-release

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.8.12110latest

Version History

VersionDownloads (Last 7 Days)Published
0.8.12110
0.8.110
0.8.10391
0.8.97
0.8.820
0.8.75
0.8.64
0.8.525
0.8.40
0.8.30
0.8.20
0.8.11
0.8.034
0.7.40
0.7.30
0.7.20
0.7.10
0.7.00
0.6.739
0.6.60
0.6.50
0.6.41
0.6.322
0.6.20
0.6.10
0.6.00
0.5.1016
0.5.90
0.5.80
0.5.70
0.5.61
0.5.51
0.5.46
0.5.310
0.5.20
0.5.10
0.5.00
0.4.50
0.4.40
0.4.30
0.4.20
0.4.10
0.4.00
0.3.10
0.3.00
0.2.00
0.0.1726
0.0.160
0.0.1536
0.0.140
0.0.1347
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.61
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @dintero/checkout-web-sdk

Weekly Downloads

803

Version

0.8.12

License

MIT

Unpacked Size

404 kB

Total Files

14

Last publish

Collaborators

  • svnv
  • frodeaa
  • andersem