Payin SDK

Render the Payment Labs payment element on your own page to take payments, let buyers try again after a decline, save cards for later, and sign buyers up to subscriptions.

📘

Upgrading from a 0.x release? Version 1.x renamed the Widget API to Element. See Upgrading from 0.x to 1.x.

@paymentlabs/paymentlabs-js is a TypeScript SDK that renders the Payment Labs payment element inside your own page. You build the page around it; the element collects and tokenizes the buyer's card, bank or wallet details and submits the charge, so raw card data never reaches your servers.

This guide is in four parts:

  1. Quickstart: a working payment in three steps.
  2. Handle the outcome: confirm payments, handle declines, let the buyer try again, and log errors.
  3. Customize and extend: billing address, fee, theming, saving cards and subscriptions.
  4. Reference: every method, event and type, then a go-live checklist.

Before you start

You need three things:

  1. Your merchant ID, for example C-01JBX2Q8F4H6K8M0N2P4R6T8VW. It is the merchantId on every checkout you create, and Get Merchant returns it.
  2. A token on your server. Merchants use the same login as payors, Create Access Token; see Environments and Authentication. The token never goes to the browser.
  3. A checkout or plan offer, created from your backend. Creating it on the server keeps the amount out of the buyer's reach.

How it works

A payment involves your app, the SDK element and the Payment Labs API:

   YOUR APP                  PAYMENT LABS SDK              PAYMENT LABS
┌──────────────┐          ┌────────────────────┐       ┌────────────────┐
│ Product page │          │  Payment element   │       │  Checkout API  │
│ Cart & total │ ───────▶ │  Secure card fields│ ────▶ │  Tokenization  │
│ "Pay" button │          │  Events & results  │       │  Processing    │
└──────────────┘          └────────────────────┘       └────────────────┘
   You design                 The SDK collects             We move the money
   the experience             & tokenizes safely           and tell you what
                                                            happened
  1. Your backend creates a checkout (the intent to collect money or save a card) and gets back a checkout ID.
  2. Your frontend hands that ID to the SDK, which renders the payment element inside a container you choose.
  3. The buyer enters their details. The SDK tokenizes them and submits the transaction.
  4. The SDK tells you what happened through events such as checkout-completed and checkout-failed, so you can react in your UI.
The SDK handlesYou handle
Secure card, bank and wallet input fieldsProduct pages, cart and order summary
PCI-compliant tokenization and submissionPricing, tax and shipping
Payment, retry, save-card and subscription flowsOrder creation and fulfilment
Events and result screensThe "Pay" button and the page around the element
Theming to match your brandCreating the checkout on your backend
📘

The SDK never collects money on its own. It always works against a checkout that you created first through the Payment Labs API. That checkout decides the amount, the currency, and which payment methods appear.

Install

npm install @paymentlabs/paymentlabs-js
# or
yarn add @paymentlabs/paymentlabs-js
# or
pnpm add @paymentlabs/paymentlabs-js

Prefer a script tag? Load it from our CDN and PaymentLabs becomes available on window:

<script src="https://sdk.paymentlabs.io/payin/latest/paymentlabs.js"></script>
📘

The CDN URL serves the newest 1.x release, so it picks up fixes and features automatically, and never a breaking change. To hold one exact version, install from npm instead. See the SDK Versioning and Support Policy.

Quickstart: your first payment

Step 1: Create a checkout (backend)

From your server, call Create Checkout with the amount, currency and buyer. Send the returned checkout ID to your page.

Step 2: Render the element (frontend)

Create the SDK once, then mount an element into a container on your page.

import { PaymentLabs } from "@paymentlabs/paymentlabs-js";

const sdk = PaymentLabs.create({
  environment: "sandbox", // switch to "production" when you go live
  merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
});

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
});
<div id="checkout-container"></div>
<button id="pay-button" disabled>Pay</button>

Step 3: Submit and react to the result

Wire your button to submitPayment() and listen for the outcome.

const payButton = document.getElementById("pay-button");

// The form is rendered and interactive.
element.on("checkout-loaded", () => {
  payButton.disabled = false;
});

// Disable the button while a payment is in flight.
element.on("payment-processing", (isProcessing) => {
  payButton.disabled = isProcessing;
  payButton.textContent = isProcessing ? "Processing…" : "Pay";
});

element.on("checkout-completed", () => {
  showSuccessScreen(); // then confirm the payment on your server
});

element.on("checkout-failed", ({ errorReason }) => {
  showError(errorReason); // see Handle payment failures
});

payButton.addEventListener("click", () => element.submitPayment());

That's a working integration: the SDK renders the form, tokenizes the card, runs the transaction and reports the outcome. Before you go live, read Handle the outcome.

Handle the outcome

Confirm the payment on your server

Browser events can be lost when the buyer closes the tab, so checkout-completed is not proof of payment. Fulfil an order only after your server sees the checkout as PAID: wait for the checkout-status-changed webhook, or call Get Checkout.

Handle payment failures

When a payment fails, checkout-failed gives you an errorReason (the category) and sometimes an extendedErrorReason (the specific cause). Use the category to decide what to tell the buyer, and the extended reason to be more specific when it is present.

element.on("checkout-failed", ({ errorReason, extendedErrorReason, unresolved }) => {
  if (unresolved) {
    show("We are confirming your payment. Please don't pay again.");
    return;
  }
  switch (errorReason) {
    case "MISSING_INSTRUMENT":
      show("Please choose a payment method to continue.");
      break;
    case "INVALID_INSTRUMENT":
      show("Those payment details look off. Please double-check them.");
      break;
    case "CHARGE_INSTRUMENT_ERROR":
      show("Your card was declined. Try another card or contact your bank.");
      break;
    case "APPLICATION_ERROR":
      show("Something went wrong on our side. Please try again shortly.");
      break;
    default:
      show("Payment couldn't be completed. Please try again.");
  }

  if (extendedErrorReason) console.debug("Decline detail:", extendedErrorReason);
});

Every errorReason and extendedErrorReason value, with its meaning, is listed in Checkout Statuses and Failure Reasons. New extendedErrorReason values can be added in minor releases, so fall back to errorReason for values you do not recognise.

Two results are not declines. unresolved: true, and the UNEXPECTED_ERROR reason, mean the payment has not been confirmed as succeeded or failed yet. Don't ask the buyer to pay again and don't cancel the order; check the checkout on your server.

Let the buyer try again

When a payment is declined, the element can let the buyer pay the same checkout again. You don't need to create a new checkout.

The failure screen keeps the decline reason and adds a Try another payment method button. When the buyer selects it, the payment form comes back with the details they entered: they can correct the card that was declined, or choose another payment method, and pay again with your Pay button.

Turn it on

Retries are off by default: a checkout accepts one charge. To turn them on, ask your Payment Labs contact to set the number of charge attempts for your account.

Charge attemptsWhat the buyer gets
1 (default)One charge. A decline is final.
2 to 10The first charge plus up to 9 more. For example, 3 allows the first charge and 2 retries.

The number counts every charge on the checkout, including the first one and any you make yourself with Charge Checkout. There's nothing to change in your code: once attempts are set, the element offers the retry by itself. It needs SDK 1.10.2 or later; the CDN URL always serves it.

When the element offers a retry

The element shows Try another payment method only when all of these are true:

  • the checkout is FAILED;
  • it has charge attempts left;
  • it has not expired.

Otherwise the failure screen is final. Some details to know:

  • Failure redirect. If the checkout has a failureRedirectUrl, the element follows it only when the failure is final, so the buyer can still see the retry button.
  • Page reload. A buyer who opens a checkout that already failed sees the failure screen with the retry button. The form starts empty, and the buyer may need to sign in again.

Update your page

Two event fields and one event tell you about retries:

Field or eventWhat it tells you
checkout-failed → canRetrytrue when the element is showing the retry button. false when the failure is final.
checkout-failed → remainingChargeAttemptsHow many charges the checkout still accepts.
checkout-retryThe buyer selected Try another payment method and the form is back. Payload: { checkoutId, remainingChargeAttempts }.

Keep your Pay button in step with the form: disable it while the failure screen is showing, and enable it again on checkout-retry.

The element checks the checkout when the buyer selects Try another payment method. If it can't take another payment — its last charge attempt was used, or it was paid, for example from another browser tab — the element shows the checkout as it stands and fires checkout-failed (with canRetry: false) or checkout-completed. Your Pay button stays disabled. This needs SDK 1.45.1 or later.

element.on("checkout-failed", ({ canRetry, errorReason, unresolved }) => {
  payButton.disabled = true; // the form is hidden behind the failure screen

  if (unresolved) return; // not a decline, see Handle payment failures

  if (canRetry) {
    // The element shows the decline reason and the retry button. Nothing else to show.
    return;
  }

  showFinalFailure(errorReason); // the checkout can't be paid again
});

element.on("checkout-retry", () => {
  payButton.disabled = false; // the form is back: the buyer can pay again
});

On your server

With retries on, FAILED is not final. A checkout can go from FAILED back to PROCESSING and then to PAID. Don't cancel the order or send a "payment failed" email on the first FAILED webhook.

Get Checkout and the checkout-status-changed webhook return remainingChargeAttempts. Keep the order open on FAILED while it is above 0 and expiresAt has not passed. See Charging a failed checkout again.

SDK errors (sdk-error)

checkout-failed tells you a payment was declined. Everything else that can go wrong is reported on one event, sdk-error: the payment form failing to load, the API rejecting the charge, the session expiring, a wallet being unavailable, a 3D Secure step failing, or the payment status never being confirmed.

element.on("sdk-error", (error) => {
  // error.code      stable code, e.g. "CHARGE_REJECTED" — switch on this
  // error.category  "load" | "authentication" | "validation" | "payment" | "three_d_secure"
  //                 | "processing" | "wallet" | "address_autocomplete" | "internal"
  // error.severity  "fatal" | "recoverable" | "degraded"
  // error.message   fixed English sentence for the code — for your logs, not for buyers
  analytics.track("payment_sdk_error", error);

  if (error.code === "PAYMENT_STATUS_UNKNOWN") {
    // The payment may still go through. Don't cancel the order — check its status later.
  }
});

severity tells you how much it matters:

SeverityMeaning
fatalThe flow stopped. The buyer can't continue without reloading the page.
recoverableThe buyer can fix something and try again.
degradedA feature is unavailable (e.g. address suggestions), but paying still works.

The payload never contains sensitive data: no card or bank details, names, emails, addresses, tokens or server responses. Besides code, category, severity, message and timestamp, it can only carry:

FieldWhen
paymentMethod"payment-card", "bank-transfer", "saved-instrument", "wallet-balance", "apple-pay" or "google-pay"
httpStatusAn API call failed with this status
reasonThe 3D Secure step that failed (e.g. "authentication-denied")
fieldsIds of the invalid fields, never their values
checkoutId, transactionIdIds to quote when contacting support

submitPayment() also resolves with the same code on failure, if you prefer await to events:

const result = await element.submitPayment();
if (!result.success) {
  console.log(result.code); // e.g. "CHARGE_VALIDATION"
}

Error codes

CodeSeverityWhen
PAYMENT_FORM_LOAD_FAILEDfatalThe card form failed to load
CHECKOUT_RELOAD_FAILEDfatalThe checkout couldn't be loaded for a signed-in buyer
SUBSCRIPTION_OFFER_LOAD_FAILEDfatalThe subscription offer couldn't be loaded
SUBSCRIPTION_CREATE_FAILEDdegradedThe subscription couldn't be created for the chosen plan
SUBSCRIPTION_HANDOVER_FAILEDdegradedThe subscription was created, but its checkout couldn't be shown
SAVED_METHODS_LOAD_FAILEDdegradedThe buyer's saved payment methods couldn't be loaded
SESSION_EXPIREDfatalThe session expired (HTTP 401) — the page must be reloaded
SESSION_FORBIDDENfatalThe session was refused (HTTP 403)
VERIFICATION_CODE_REQUEST_FAILEDrecoverableThe sign-in code or password-reset email couldn't be sent
NO_PAYMENT_METHOD_SELECTEDrecoverablePayment was submitted with no method selected
FIELD_VALIDATIONrecoverableFields are invalid — fields lists them
CHARGE_VALIDATIONrecoverableThe API rejected fields (HTTP 400) — fields lists them
BILLING_ADDRESS_REQUIREDfatalAddress fields are disabled and submitPayment() got no address
CHARGE_REJECTEDfatalThe charge request was rejected
CHECKOUT_LOCKEDfatalThe checkout is already being paid (HTTP 423)
CHARGE_NO_RESPONSEfatalThe charge request returned nothing
THREE_DS_FAILEDrecoverableA 3D Secure step failed — the outcome still follows on checkout-failed or checkout-completed
THREE_DS_TIMEOUTrecoverableThe 3D Secure challenge timed out
PAYMENT_STATUS_UNKNOWNfatalThe payment status couldn't be confirmed — it may still complete
WALLET_UNAVAILABLEdegradedApple Pay / Google Pay couldn't be offered
WALLET_MERCHANT_VALIDATION_FAILEDrecoverableApple Pay merchant validation failed
WALLET_PAYMENT_FAILEDrecoverableThe wallet payment couldn't be completed
WALLET_CANCELLEDrecoverableThe buyer closed the wallet sheet
ADDRESS_AUTOCOMPLETE_UNAVAILABLEdegradedAddress suggestions are off — the buyer can still type the address
RETRY_FAILEDfatalThe buyer selected Try another payment method and the form couldn't be shown — the page must be reloaded
UNEXPECTED_ERRORfatalAnything else

Good to know

  • Codes are stable. New codes may be added; existing ones are never renamed or removed. Always keep a default branch for codes you don't recognise.
  • No duplicates. The same error is reported once, and an unavailable feature (a wallet, address suggestions) once per element.
  • Your listener can't break the payment. If an sdk-error listener throws, the error is ignored and the payment flow carries on.
  • Older SDK versions. SDK versions released before sdk-error existed throw Invalid event type from element.on("sdk-error", …). If you support them, wrap the call in try.
  • payment-failed is deprecated. Use sdk-error instead.

Customize the element

Collecting the billing address

By default the SDK renders the billing address fields inside the element, and you don't have to do anything. If you already collect the address elsewhere (say, a shipping form), turn the SDK's fields off and pass the address at submit time.

fields.addressWho renders the addressWhere the data comes from
true (default)The SDKThe element's own fields
falseYouThe submitPayment() argument

Option A — Let the SDK collect it (default)

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
});

await element.submitPayment(); // address comes from the SDK-rendered fields

The form starts compact: the buyer sees only Address. The other fields (Address 2, Country, State, City, Postal Code) show once the buyer picks a suggestion or enters the address manually.

To show every address field from the start, set the optional billingDetails.addressLayout to "full" (the default is "compact"):

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
  billingDetails: {
    addressLayout: "full", // every address field from the start
  },
});

Option B — Provide it yourself

Set billingDetails.fields.address to false, then include the address in submitPayment():

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
  billingDetails: {
    fields: { address: false }, // SDK won't render address fields
  },
});

await element.submitPayment({
  billingDetails: {
    address: {
      address1: "123 Main Street",
      address2: "Apt 4B", // optional
      city: "New York",
      state: "NY",
      postalCode: "10001",
      country: "US", // ISO 3166-1 alpha-2
    },
  },
});
⚠️

If you turn the fields off, you must pass the address. When address is false and you call submitPayment() without one, the call fails with "Address information is required but not provided".

Showing the fee

When a checkout carries a fee, the element shows the buyer the subtotal, the fee and the total above the payment methods. Each payment method can carry its own fee, so the figures follow the method the buyer selects. On a subscription they show in the plan card: what the buyer pays today, then each cycle. With no fee there is no breakdown: a one-time checkout shows no summary, a subscription shows only its plan card, and payableAmount equals amount.

To show the fee in your own layout instead, for example in an order summary beside the element, set hideFeeSummary and listen to payment-method-changed. It fires when the form shows and each time the buyer selects or clears a payment method. It can fire before checkout-loaded, so do not reset your summary there. On a subscription the plan card stays, without the fee lines.

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
  hideFeeSummary: true,
});

element.on("payment-method-changed", ({ amount, payableAmount }) => {
  // No total yet: the methods cost different amounts and the buyer has not picked one.
  showOrderSummary({ subtotal: amount, total: payableAmount });
});
FieldWhat it is
paymentMethodpayment-card, bank-transfer, third-party-token or wallet-balance (SDK 1.45.1 or later). Set when a method is selected, or when the form offers only one.
amountThe subtotal: what the checkout asks for, before any fee.
payableAmountThe total with paymentMethod. Without a method, present only when every method pays the same.
recurringA subscription's cycle: its amount and payableAmount, figured the same way.
summaryThe lines to show for the current selection, in order (subtotal, fee or card fee, total), each with label, amount and formattedAmount, and the total. Absent while only the subtotal is known. SDK 1.45.1 or later.
submitYour Pay button: its label (for example "Pay $12.50") and enabled, which is false until the buyer selects a method. Always present from SDK 1.45.1.
balanceThe buyer's wallet balance, when the checkout offers it. SDK 1.45.1 or later. See Paying with a wallet balance.

The fee is payableAmount minus amount. Apple Pay and Google Pay list the subtotal and the fee above their total in the wallet sheet, also with hideFeeSummary: true. On a subscription they show no fee lines.

Paying with a wallet balance

When a one-time checkout accepts it, a buyer who holds a wallet balance with you can pay the whole order with it, with no fee. It needs SDK 1.45.1 or later. To offer it on your checkouts, ask your Payment Labs contact.

  • A guest is asked for the code sent to their email first. Once signed in, the balance shows above the other payment methods, under Pay with wallet, with the amount available.
  • When the balance covers the order, it is selected first. The buyer can still pick another method.
  • When it doesn't, it shows as unavailable ("Not enough for this order") and the buyer pays another way. The balance never pays part of an order.

There's nothing to change in your code: the element handles the selection and the payment. If you build your own order summary or Pay button, payment-method-changed carries balance:

FieldWhat it is
balance.payorNameWhose balance it is: your business name as the buyer knows it.
balance.availableThe amount available.
balance.coverage"full" when it pays the whole order, "insufficient" when it doesn't.
balance.selectedtrue when the balance is the selected method.

With the balance selected, paymentMethod is wallet-balance, and submit.label reads, for example, "Pay $12.00 from your balance".

Theming

Match the element to your brand with a small set of seed colors. The SDK derives a full, accessible palette from them.

const element = await sdk.createElement("checkout", checkoutId, {
  containerId: "checkout-container",
  theme: {
    mode: "light", // "light" (default) or "dark"
    colors: {
      seed: {
        brand: "#5b3df5",    // your primary color — buttons, accents
        neutral: "#1f2937",  // text and neutral UI
        info: "#2563eb",
        positive: "#16a34a", // success
        negative: "#dc2626", // errors
        warning: "#d97706",
      },
      // Optional fine-grained overrides
      overrides: {
        "bg-main": "#ffffff",
      },
    },
  },
});

You can switch between light and dark at runtime from the SDK instance:

await sdk.updateTheme("dark");
⚠️

Switching the theme re-renders the secure fields. The card fields (cardholder name, number, expiry, CVC) live in isolated iframes that are rebuilt when the theme changes, so anything the buyer already typed into them is cleared. Switch the theme before the buyer starts entering card details.

Saving a card without charging it

Sometimes you don't want to charge the buyer now, only let them add a payment method to their account, so they can choose it instead of typing it again on later checkouts.

To do this, create the checkout with an amount of 0. There is no frontend flag: your code is the same as for a normal checkout. When the amount is zero, the element becomes a save-card form:

  • The buyer enters their card as usual.
  • On submit, the card is tokenized and saved. No money moves.
  • The wording adapts: progress reads "Your request is being processed" and success reads "Your request has been successfully completed".
  • The optional "save this card for later" checkbox is hidden, because saving the card is the purpose of the checkout.
  • The buyer must sign in or create an account, and only cards and bank accounts are offered.
// Backend created this checkout with an amount of 0.
const element = await sdk.createElement("checkout", savecardCheckoutId, {
  containerId: "checkout-container",
});

const saveButton = document.getElementById("save-card-button");

element.on("checkout-loaded", () => {
  saveButton.disabled = false;
});

// The card is now saved and reusable.
element.on("checkout-completed", () => {
  showCardSavedScreen();
});

saveButton.addEventListener("click", () => element.submitPayment());
<div id="checkout-container"></div>
<button id="save-card-button" disabled>Save card</button>

Label your button "Save card" or "Add payment method" rather than "Pay", to match the SDK's own wording. To charge the saved card later from your server, follow Saved Cards and Merchant-Initiated Charges.

Subscriptions

Recurring billing follows the same shape as a one-time payment, with two differences: you pass a plan offer ID instead of a checkout ID, and you create the element with the "subscription" type.

const element = await sdk.createElement("subscription", planOfferId, {
  containerId: "subscription-container",
});

element.on("subscription-loaded", () => {
  const offer = element.getSubscriptionOfferData();
  console.log("Plans available:", offer?.subscriptionPlans);
});

// The buyer picked a different plan from the offer.
element.on("subscription-plan-changed", (plan) => {
  updatePlanSummary(plan); // plan.name, plan.amount.formattedValue, plan.schedule…
});

element.on("subscription-completed", () => {
  showSubscriptionActiveScreen();
});

element.on("subscription-failed", ({ errorReason }) => {
  showError(errorReason);
});

// The chosen plan's checkout is on screen: only now can the buyer pay.
element.on("checkout-loaded", () => {
  const submitButton = document.getElementById("subscribe-button");
  submitButton.hidden = false;
  submitButton.addEventListener("click", () => element.submitPayment());
});

A plan offer can contain several plans (e.g. Monthly and Annual). The element renders them for the buyer to choose from, and fires subscription-plan-changed whenever the selection changes so you can keep your own summary in sync. When the buyer selects Continue to payment, the element shows that plan's checkout and fires checkout-loaded. An offer with one plan goes straight to its checkout.

The buyer must sign in to subscribe. When the subscription is created, subscription-completed fires; a plan with a free trial completes the same way, without a charge. Follow the subscription on your server with the subscription-status-changed webhook. Subscriptions covers plans, offers, billing and cancellation.

Framework guides

The SDK is framework-agnostic. The pattern is always the same: create the SDK, create the element into a container, listen for events, call submitPayment().

React

import { useEffect, useRef, useState } from "react";
import { PaymentLabs, PaymentElement } from "@paymentlabs/paymentlabs-js";

export function Checkout({
  checkoutId,
  onSuccess,
  onFailure,
}: {
  checkoutId: string;
  onSuccess: () => void;
  onFailure: (reason: string) => void;
}) {
  const elementRef = useRef<PaymentElement | null>(null);
  const [ready, setReady] = useState(false);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    let element: PaymentElement | undefined;

    (async () => {
      const sdk = PaymentLabs.create({
        environment: "sandbox",
        merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
      });

      element = await sdk.createElement("checkout", checkoutId, {
        containerId: "checkout-container",
      });
      elementRef.current = element;

      element.on("checkout-loaded", () => setReady(true));
      element.on("payment-processing", setLoading);
      element.on("checkout-completed", onSuccess);
      element.on("checkout-failed", ({ errorReason, canRetry }) => {
        setReady(false);
        if (!canRetry) onFailure(errorReason);
      });
      element.on("checkout-retry", () => setReady(true));
    })();

    return () => element?.destroy();
  }, [checkoutId, onSuccess, onFailure]);

  return (
    <div>
      <div id="checkout-container" />
      {ready && (
        <button onClick={() => elementRef.current?.submitPayment()} disabled={loading}>
          {loading ? "Processing…" : "Pay"}
        </button>
      )}
    </div>
  );
}

Vue

<template>
  <div>
    <div id="checkout-container"></div>
    <button v-if="ready" @click="submit" :disabled="loading">
      {{ loading ? "Processing…" : "Pay" }}
    </button>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { PaymentLabs, PaymentElement } from "@paymentlabs/paymentlabs-js";

const props = defineProps<{ checkoutId: string }>();
const emit = defineEmits<{ success: []; failure: [reason: string] }>();

const element = ref<PaymentElement | null>(null);
const ready = ref(false);
const loading = ref(false);

onMounted(async () => {
  const sdk = PaymentLabs.create({
    environment: "sandbox",
    merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
  });

  element.value = await sdk.createElement("checkout", props.checkoutId, {
    containerId: "checkout-container",
  });

  element.value.on("checkout-loaded", () => (ready.value = true));
  element.value.on("payment-processing", (p) => (loading.value = p));
  element.value.on("checkout-completed", () => emit("success"));
  element.value.on("checkout-failed", ({ errorReason, canRetry }) => {
    ready.value = false;
    if (!canRetry) emit("failure", errorReason);
  });
  element.value.on("checkout-retry", () => (ready.value = true));
});

onUnmounted(() => element.value?.destroy());

const submit = () => element.value?.submitPayment();
</script>

Vanilla JavaScript (module import)

<div id="checkout-container"></div>
<button id="pay-button" disabled>Pay</button>

<script type="module">
  import { PaymentLabs } from "@paymentlabs/paymentlabs-js";

  const sdk = PaymentLabs.create({
    environment: "sandbox",
    merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
  });

  const element = await sdk.createElement("checkout", "your-checkout-id", {
    containerId: "checkout-container",
  });

  const payButton = document.getElementById("pay-button");
  element.on("checkout-loaded", () => (payButton.disabled = false));
  element.on("checkout-completed", () => alert("Payment complete"));
  element.on("checkout-failed", ({ errorReason, canRetry }) => {
    payButton.disabled = true;
    if (!canRetry) alert(`Failed: ${errorReason}`);
  });
  element.on("checkout-retry", () => (payButton.disabled = false));
  payButton.addEventListener("click", () => element.submitPayment());
</script>

CDN (script tag)

<!DOCTYPE html>
<html>
  <body>
    <div id="checkout-container"></div>
    <button id="pay-button" disabled>Pay</button>

    <script src="https://sdk.paymentlabs.io/payin/latest/paymentlabs.js"></script>
    <script>
      (async () => {
        // PaymentLabs is available globally from the CDN.
        const sdk = PaymentLabs.create({
          environment: "production",
          merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
        });

        const element = await sdk.createElement("checkout", "your-checkout-id", {
          containerId: "checkout-container",
        });

        const payButton = document.getElementById("pay-button");
        element.on("checkout-loaded", () => (payButton.disabled = false));
        element.on("payment-processing", (busy) => {
          payButton.disabled = busy;
          payButton.textContent = busy ? "Processing…" : "Pay";
        });
        element.on("checkout-completed", () => alert("Payment complete"));
        element.on("checkout-failed", (e) => {
          payButton.disabled = true;
          if (!e.canRetry) alert(`Failed: ${e.errorReason}`);
        });
        element.on("checkout-retry", () => (payButton.disabled = false));

        payButton.addEventListener("click", () => element.submitPayment());
      })();
    </script>
  </body>
</html>

API reference

PaymentLabs.create(config)

Creates an SDK instance. Call it once and reuse it.

const sdk = PaymentLabs.create({
  environment: "sandbox", // "sandbox" | "production"
  merchantId: "C-01JBX2Q8F4H6K8M0N2P4R6T8VW",
});
FieldTypeDescription
environment"sandbox" | "production"Which Payment Labs environment to talk to
merchantIdstringYour merchant identifier

Returns a PaymentLabs instance.

sdk.createElement(type, id, options)

Creates and mounts a payment element. Returns Promise<PaymentElement>.

ParameterTypeDescription
type"checkout" | "subscription"The kind of element to render
idstringA checkout ID (for "checkout") or plan offer ID (for "subscription")
optionsElementOptionsSee below
interface ElementOptions {
  containerId: string;            // ID of the DOM element to render into (required)
  theme?: ThemeConfig;            // Brand colors and light/dark mode
  billingDetails?: BillingDetailsConfig; // Address collection behavior
  hideFeeSummary?: boolean;       // Hide the subtotal, fee and total; default false (see Showing the fee)
}

sdk.updateTheme(mode)

Switches the element between "light" and "dark". It re-renders the secure card fields (see Theming).

element.submitPayment(request?)

Submits the payment (or saves the card, for a zero-amount checkout). After a retry, call it again to charge the same checkout. Returns a result object.

const result = await element.submitPayment();
// { success: boolean; instrumentId?: string; transactionId?: string; error?: string; code?: SdkErrorCode }

On failure, code is the same code the sdk-error event carries.

Pass request.billingDetails.address only when you've disabled the SDK's address fields. See Collecting the billing address.

element.on(event, callback)

Registers an event listener. See Events for the full list.

element.getCheckoutData()

Returns the current Checkout (or null). Most useful inside checkout-loaded.

element.getSubscriptionOfferData()

Returns the current SubscriptionOffer (or null). Most useful inside subscription-loaded.

element.destroy()

Tears down the element and cleans up listeners. Call this when your component unmounts to avoid leaks.

Events

Register a handler with element.on(event, callback).

Checkout events

EventFires whenPayload
checkout-loadedThe form is rendered and readyThe checkout details
payment-method-changedThe form shows, and each time the buyer selects or clears a payment methodPaymentMethodChangedPayload, see Showing the fee
payment-processingSubmission starts or stopsboolean (true = busy)
checkout-processingThe charge is underway—
checkout-completedPayment (or card save) succeeded—
checkout-failedPayment failed, or could not be confirmed{ errorReason?, extendedErrorReason?, canRetry?, remainingChargeAttempts?, unresolved? }
checkout-retryThe buyer selected Try another payment method and the form is back{ checkoutId, remainingChargeAttempts? }, see Let the buyer try again
checkout-expiredThe checkout's time window passed—
sdk-errorAnything other than a declined payment went wrongSdkErrorPayload, see SDK errors
payment-failedDeprecated, never fires. Use sdk-error.—

If the checkout has redirectLinks, the element follows them. After checkout-completed, checkout-failed or checkout-expired, it sends the browser to the matching URL. After a failure it waits until the failure is final, so a buyer who can retry stays on the page. Leave redirectLinks out of checkouts you render with the SDK unless you want that navigation.

unresolved: true is not a decline. The payment has not been confirmed as succeeded or failed, and the checkout is UNKNOWN. Do not ask the buyer to pay again and do not cancel the order; check the checkout on your server.

Subscription events

EventFires whenPayload
subscription-loadedThe offer is rendered and ready—
subscription-plan-changedThe buyer selects a different planSubscriptionPlan
checkout-loadedThe chosen plan's checkout is on screen. submitPayment() can only succeed after it, so keep your pay button hidden until it firesThe checkout details
payment-method-changedThe plan's checkout shows, and each time the buyer selects or clears a payment methodPaymentMethodChangedPayload, see Showing the fee
subscription-processingThe subscription is being set up—
payment-processingSubmission starts or stopsboolean
subscription-completedThe subscription was created—
subscription-failedSubscription setup failed{ errorReason?, extendedErrorReason? }
subscription-expiredThe offer's time window passed—
sdk-errorAnything other than a declined payment went wrongSdkErrorPayload
// Register many at once
["checkout-loaded", "checkout-completed", "checkout-failed", "checkout-retry"].forEach((event) =>
  element.on(event, (data) => analytics.track(event, data)),
);

Type reference

The SDK ships full TypeScript definitions. The types you'll touch most often:

import {
  PaymentLabs,
  PaymentElement,
  Checkout,
  SubscriptionOffer,
  SubscriptionPlan,
  CheckoutErrorReason,
  ThemeConfig,
  SdkErrorPayload,
  SdkErrorCode,
} from "@paymentlabs/paymentlabs-js";

Checkout

Returned by getCheckoutData().

interface Checkout {
  id: string;
  buyerEmail: string;
  buyerId: string;
  merchantId: string;
  merchantName: string;
  alias: string;                 // your reference / description
  amount: { value: number; currency: string }; // value: 0 ⇒ save-card checkout
  payableAmount?: { value: number; currency: string }; // see Showing the fee
  payableAmounts?: Partial<Record<InstrumentType, { value: number; currency: string }>>;
  recurring?: {                  // a subscription's cycle, priced like the fields above
    payableAmount?: { value: number; currency: string };
    payableAmounts?: Partial<Record<InstrumentType, { value: number; currency: string }>>;
  };
  status: CheckoutStatus;
  currentTransactionStatus?: CheckoutTransactionStatus;
  remainingChargeAttempts?: number; // charges the checkout still accepts; see Let the buyer try again
  errorTransaction?: {
    transactionId: string;
    errorReason: CheckoutErrorReason;
    extendedErrorReason?: CheckoutExtendedErrorReason;
    checkoutInstrumentId: string;
    instrumentType: InstrumentType;
  };
  enabledCheckoutInstruments: InstrumentType[];
  requiresFraudSession: boolean;
  expiresAt: string;             // ISO 8601
  links: { embeddable: string; checkout: string };
  redirectLinks?: {
    successRedirectUrl?: string;
    failureRedirectUrl?: string;
    expiredRedirectUrl?: string;
  };
  termsOfServiceUrl: string;
  privacyPolicyUrl: string;
}

type CheckoutStatus =
  | "PENDING"     // awaiting payment
  | "PROCESSING"  // payment in progress
  | "PAID"        // succeeded
  | "FAILED"      // failed; can be paid again while charge attempts are left and it has not expired
  | "EXPIRED"     // time window passed
  | "UNKNOWN"     // the charge was never confirmed; do not charge again
  | "CREATING" | "ACTIVE" | "TRIAL"; // subscription statuses; a checkout never has them

SubscriptionOffer & SubscriptionPlan

interface SubscriptionOffer {
  id: string;
  merchantId: string;
  userId: string;
  subscriptionPlanIds: string[];
  subscriptionPlans: SubscriptionPlan[];
  expiresAt: string;
  links: { embeddable: string; self: string };
}

interface SubscriptionPlan {
  id: string;
  merchantId: string;
  merchantDisplayName: string;
  sort: string;                  // ordering/grouping hint, e.g. "RegularPlan#100"
  name: string;
  description?: string;
  enabledCheckoutInstruments: InstrumentType[];
  schedule: Schedule;
  amount: Money;
  trialPeriod?: Schedule;
  status: "ACTIVE" | "CANCELED";
  termsOfServiceUrl?: string;
  privacyPolicyUrl?: string;
}

interface Schedule {
  interval: "daily" | "weekly" | "monthly" | "yearly";
  intervalCount: number;         // e.g. interval "monthly" + count 2 = every 2 months
}

interface Money {
  value: number;                 // amount as a decimal value
  currency: string;              // ISO 4217
  formattedValue: string;        // e.g. "$9.99"
  digits: number;                // decimal digits for the currency
  wholeValue: number;            // value with no decimals (usually cents)
}

SdkErrorPayload

Passed to sdk-error listeners. See SDK errors for the codes.

interface SdkErrorPayload {
  code: SdkErrorCode;            // e.g. "CHARGE_REJECTED"
  category: "load" | "authentication" | "validation" | "payment" | "three_d_secure"
    | "processing" | "wallet" | "address_autocomplete" | "internal";
  severity: "fatal" | "recoverable" | "degraded";
  message: string;               // fixed English sentence for the code
  timestamp: string;             // ISO 8601
  paymentMethod?: "payment-card" | "bank-transfer" | "saved-instrument" | "apple-pay" | "google-pay";
  httpStatus?: number;
  reason?: string;               // 3D Secure step that failed
  fields?: string[];             // invalid field ids, never values
  checkoutId?: string;
  transactionId?: string;
}

ThemeConfig

interface ThemeConfig {
  mode?: "dark" | "light";       // defaults to "light"
  colors?: {
    seed?: {
      brand: string;
      neutral: string;
      info: string;
      positive: string;
      negative: string;
      warning: string;
    };
    overrides?: {
      "bg-main": string;         // main background override
    };
  };
}

BillingDetailsConfig & SubmitPaymentRequest

interface BillingDetailsConfig {
  fields?: {
    address?: boolean;           // false ⇒ you provide the address at submit time
  };
  addressLayout?: "compact" | "full"; // optional, default "compact"
}

interface SubmitPaymentRequest {
  billingDetails: {
    address?: {
      address1: string;          // required
      address2?: string;
      city: string;              // required
      state: string;             // required
      postalCode: string;        // required
      country: string;           // required, ISO 3166-1 alpha-2
    };
  };
}

Going live

A checklist before you switch environment to "production":

  1. Choose how you take updates. Read the SDK Versioning and Support Policy and decide between the CDN (automatic 1.x updates) and an exact npm version.
  2. Create checkouts server-side. Never trust an amount that came from the browser.
  3. Confirm payments server-side. Fulfil an order only after the checkout-status-changed webhook or Get Checkout shows PAID.
  4. Handle every outcome. checkout-completed, checkout-failed and checkout-expired should each lead somewhere sensible. Log sdk-error so you can see failures that never reach a decline.
  5. Decide on retries. If buyers should be able to try again after a decline, ask your Payment Labs contact to set your charge attempts, and follow Let the buyer try again.
  6. Show progress. Use payment-processing to disable the button while a payment is in flight.
  7. Clean up. Call element.destroy() when your component unmounts.
  8. Register your domain for Apple Pay. Send the domain that hosts the element to your Payment Labs contact. See Apple Pay and Google Pay.
  9. Test the edges. A decline, a retry after a decline, an unconfirmed payment, an expired checkout, and (if you use it) the zero-amount save-card flow. Use Credit Card Payin Test Values.

When you contact Payment Labs support about an element, quote the checkoutId and transactionId from the sdk-error payload.

Next steps


Did this page help you?