@verysell-group/verypay-js
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

verypay-js

Loading wrapper and TypeScript types for the Verypay JS SDK

build status code coverage npm version bundle size npm downloads apache license

Why use verypay-js?

The default JS SDK code snippet blocks page rendering:

<script src="https://www.verypay.ch/sdk/js?client-id=test"></script>
<script>
    verypay.Buttons().render("body");
</script>

The above snippet can be difficult to implement in a non-blocking way, especially in single page web apps. This is where the verypay-js library comes in. It provides the following benefits over the above snippet:

  • Async script loading to ensure page rendering isn't blocked.
  • A Promise API to know when script loading is complete.
  • A convenient way to reload the script when query parameters or data attributes change.

Installation

To get started, install verypay-js with npm.

npm install @verysell-group/verypay-js

Usage

Import the loadScript function for asynchronously loading the Verypay JS SDK.

loadScript(options)

  • accepts an object for passing query parameters and attributes to the JS SDK.
  • returns a Promise that resolves with window.verypay after the JS SDK is finished loading.

Async/Await

import { loadScript } from "@verysell-group/verypay-js";

let verypay;

try {
    verypay = await loadScript({ clientId: "test" });
} catch (error) {
    console.error("failed to load the Verypay JS SDK script", error);
}

if (verypay) {
    try {
        await verypay.Buttons().render("#your-container-element");
    } catch (error) {
        console.error("failed to render the Verypay Buttons", error);
    }
}

Promises

import { loadScript } from "@verysell-group/verypay-js";

loadScript({ clientId: "test" })
    .then((verypay) => {
        verypay
            .Buttons()
            .render("#your-container-element")
            .catch((error) => {
                console.error("failed to render the Verypay Buttons", error);
            });
    })
    .catch((error) => {
        console.error("failed to load the Verypay JS SDK script", error);
    });

Passing Arguments

The loadScript function accepts an object for configuring the JS SDK. It's used for setting query parameters and script attributes. It accepts parameters in camelCase or kebab-case.

Query Parameters

The following example adds client-id and currency as query string parameters:

loadScript({ clientId: "YOUR_CLIENT_ID", currency: "EUR" });

Which will load the following <script> asynchronously:

<script src="https://www.verypay.ch/sdk/js?client-id=YOUR_CLIENT_ID&currency=EUR"></script>

By default, the JS SDK only loads the buttons component. The components query string parameter can be used to load multiple components:

loadScript({
    clientId: "YOUR_CLIENT_ID",
    components: "buttons,marks,messages",
});

Which will load the following <script> asynchronously:

<script src="https://www.verypay.ch/sdk/js?client-id=YOUR_CLIENT_ID&components=buttons,marks,messages"></script>

View the full list of supported query parameters.

Data Attributes

All options prefixed with data are considered attributes. The following example adds data-page-type as an attribute:

loadScript({
    clientId: "YOUR_CLIENT_ID",
    dataPageType: "checkout",
});

Which will load the following <script> asynchronously:

<script
    data-page-type="checkout"
    src="https://www.verypay.ch/sdk/js?client-id=YOUR_CLIENT_ID"
></script>

View the full list of supported script parameters.

Merchant Id Array

The merchantId option accepts an array to simplify the implementation for Multi-Seller Payments. With this approach the caller doesn't have to worry about managing the two different merchant id values (data-merchant-id and merchant-id).

Here's an example with multiple merchantId values:

loadScript({
    clientId: "YOUR_CLIENT_ID",
    merchantId: ["123", "456", "789"],
});

Which will load the following <script> and use merchant-id=* to properly configure the edge cache:

<script
    data-merchant-id="123,456,789"
    src="https://www.verypay.ch/sdk/js?client-id=YOUR_CLIENT_ID&merchant-id=*"
></script>

Here's an example with one merchant-id value:

loadScript({
    clientId: "YOUR_CLIENT_ID",
    merchantId: ["123"],
});

When there's only one, the merchant-id is passed in using the query string.

<script src="https://www.verypay.ch/sdk/js?client-id=YOUR_CLIENT_ID&merchant-id=123"></script>

sdkBaseUrl

For local development, the sdkBaseUrl option can be used to set the base url of the JS SDK:

loadScript({
    clientId: "YOUR_CLIENT_ID",
    sdkBaseUrl: "http://localhost.verypay.ch:8000/sdk/js",
});

Legacy Browser Support

This library relies on JavaScript Promises. To support legacy browsers like IE 11, you must provide your own Promise polyfill. With a Promise polyfill this library will support the same browsers as the JS SDK.

The loadScript() function takes in a second parameter for providing a Promise ponyfill. It defaults to the global Promise object if it exists. There are two options for polyfilling the Promise object:

  1. Use a global polyfill strategy that monkey patches the window.Promise API implementation.
  2. Use a ponyfill strategy that passes a Promise library into loadScript() without affecting other code:
import { loadScript } from "@verysell-group/verypay-js";
import PromisePonyfill from "promise-polyfill";

loadScript(options, PromisePonyfill).then((verypayObject) => {});

We also provide a legacy build that includes the promise-polyfill library. You can reference it from the CDN here:

<script src="https://unpkg.com/@verysell-group/verypay-js@0.0.1/dist/verypay-sdk.iife.js"></script>

Using a CDN

The verypay-js script is also available on the unpkg CDN. The iife/verypay-js.js build assigns the loadScript function to the window object as window.verypayLoadScript. Here's an example:

<!doctype html>
<html lang="en">
    <head>
        <script src="https://unpkg.com/@verysell-group/verypay-js@0.0.1/dist/verypay-sdk.iife.js"></script>
    </head>
    <body>
        <div id="verypay-buttons"></div>
        <script>
            window.verypayLoadScript({ clientId: "test" }).then((verypay) => {
                verypay.Buttons().render("#verypay-buttons");
            });
        </script>
    </body>
</html>

loadCustomScript(options)

The loadCustomScript function is a generic script loader function that works with any url.

  • accepts an object for defining the script url and attributes.
  • returns a promise to indicate if the script was successfully loaded.

Async/Await

import { loadCustomScript } from "@verysell-group/verypay-js";

try {
    await loadCustomScript({
        url: "https://www.example.com/index.js",
        attributes: {
            id: "custom-id-value",
            "data-foo": "custom-data-attribute",
        },
    });

    console.log("successfully loaded the custom script");
} catch (error) {
    console.error("failed to load the custom script", error);
}

Promises

import { loadCustomScript } from "@verysell-group/verypay-js";

loadCustomScript({
    url: "https://www.example.com/index.js",
    attributes: { id: "custom-id-value", "data-foo": "custom-data-attribute" },
})
    .then(() => {
        console.log("successfully loaded the custom script");
    })
    .catch((err) => {
        console.error("failed to load the custom script", err);
    });

TypeScript Support

This package includes TypeScript type definitions for the Verypay JS SDK. This includes types for the window.verypay namespace. We support projects using TypeScript versions >= 3.8.

Releasing

Run npm run release to publish a new release. The version number is determined by the git commits which follow conventional commits spec.

Readme

Keywords

none

Package Sidebar

Install

npm i @verysell-group/verypay-js

Weekly Downloads

0

Version

0.0.6

License

none

Unpacked Size

61.4 kB

Total Files

12

Last publish

Collaborators

  • nam.nguyenhong
  • verypay_dev
  • cuong.nguyencao