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

@panto/panto-js

v1.0.6

Published

Panto.js loading wrapper

Downloads

6

Readme

Functional Perimeter

Accept payment methods from all over the world with an integrated and secure user interface component.

The Payment Element is a user interface component for the Web that accepts more than one payment method, validates input, and manages errors. Use it alone or with other front-end elements of your Web application.

How It Works

Installation

Installation via our PantoJS library

PantoJS is a wrapper that simplifies loading Panto on your website. It ensures that it is only loaded once and offers TypeScript autocomplete.

Installation of Panto.js

Use npm (for example) to install the Panto.js module:

npm install @panto/panto-js

Usage

loadPanto

This function returns a promise that resolves with a newly created Panto object once Panto.js has been loaded. It takes the same parameters as those passed during the direct initialization of a Panto instance. If necessary, it will load Panto.js for you by inserting the Panto.js script tag. If you call loadPanto in a server environment, it will resolve to null.

import { loadPanto } from "@panto/panto-js";

const panto = await loadPanto(
  publicApiKey, // "public_0b38ef2f95c09ad0aa08"
  'sandbox', // "sandbox" or "production"
  'my-sandbox-instance', // Only required if you are using the sandbox environment
});
  • publicApiKey: We have placed a random API key in this example. Replace it with your actual publishable API keys to test this code via your Panto account. You can find your API keys in your Panto Dashboard.
  • env: The environment in which to load Panto.js. Can be sandbox or production.

Manual Installation

Manually add the Panto.js script tag to the of each page on your site. If an existing script tag is already present, this module will not insert a new one. When you call loadPanto, it will use the existing script tag.

<!-- Somewhere in your site's <head> -->
<script src="https://js.panto.io/v1" async></script>

1) Creating a paymentIntent (Backend)

On the server side, proceed to create a paymentIntent on the route https://api.panto.io/payment-intents

Example in PHP:

function createPaymentIntent($items) {
    $username = 'Your private key';
    $password = "";

    // Calculate your product totals on the back-end to avoid any manipulation of object in front
    $calculateOrderAmount = function() use ($items) {
        $itemTotal = 0;
        foreach ($items as $item) {
            $itemTotal += $item['quantity'] * $item['price'];
        }
        return $itemTotal;
    };

    $paymentIntentBody = [
        'amount' => $calculateOrderAmount(),
        'currency' => "EUR",
        'return_url' => "https://www.my-shop.com/order/cart/checkout-onshop",
        'transfer_data' => [
            [
                'destination' => 'Your accountID',
                'amount' => $calculateOrderAmount(),
                'application_fee' => 0,
                'availability_type' => "IMMEDIATE"
            ],
        ],
    ];

    $ch = curl_init("https://api.panto.io/payment_intents");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paymentIntentBody));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "Authorization: Basic " . base64_encode($username . ":" . $password)
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    $paymentIntent = json_decode($response, true);
    return $paymentIntent['secret_key'];
}

With $items:

// Replace with your backend product price retrieval logic

$items = [
    [
        'quantity' => 2, // Quantity of item 1
        'price' => 10.00 // Price of item 1
    ],
    [
        'quantity' => 1, // Quantity of item 2
        'price' => 20.00 // Price of item 2
    ],
        ...
];

2) Create a Payment Element (Frontend)

This code creates a Payment Element and mounts it on the DOM:

const panto = await loadPanto("your public key");

const appearance = {
  /* appearance */
};
const options = {
  /* options */
};
const elements = panto.elements({ clientSecret, appearance }); // Replace clientSecret with your secret returned by the previously created paymentIntent
const paymentElement = await elements.create("payment", options);
paymentElement.mount("#payment-element");

(Optional) Appearance

Use the Appearance API to control the style of all elements. Choose a theme or update specific information.

For example, choose the "flat" theme and replace the main text color.


jsx
const panto = await loadPanto('your public key');

const appearance = {
  theme: 'panto',
  variables: {
      colorPrimary: '#587a9d',
      colorBackground: '#ff0000',
      colorText: '#515581',
    },
};
const options = { /* options */ };
const elements = panto.elements({ clientSecret, appearance }); // Replace clientSecret with your secret returned by the previously created paymentIntent
const paymentElement = await elements.create('payment', options);
paymentElement.mount('#payment-element');

3) Making the Payment on the Client Side

When your client clicks on the payment button, call confirmPayment() with the PaymentElement and pass a return_url to indicate the page to which Panto should redirect the user once the payment is made. For payments requiring authentication, Panto directly displays the 3DSecure authentication request or redirects the client to an authentication page, depending on the payment method. Once the authentication process is finalized, the client is redirected to the return_url.

document
        .querySelector("#payment-element")!
        .addEventListener("submit", handleSubmit);

      async function handleSubmit(e: any) {
        const { error } = await panto.confirmPayment({
          elements,
          confirmParams: {
            return_url: `https://www.my-shop.com/order/success`,
          },
        });
        // Error handling
        if (error.type === "card_error" or error.type === "validation_error") {
          console.error(error.message);
        } else {
          console.error("An unexpected error occurred.");
        }
      }

Error Handling

In the event of an immediate error (e.g., the card is declined), Panto.js returns an error. Display this error directly to your client so they can make another payment attempt.

Displaying a Message on the Payment Status

When Panto redirects the client to the return_url, the query parameter payment_intent_client_secret is added by Panto.js. Use it to retrieve the PaymentIntent in order to determine the data to display to your client.