lithic-embed
v0.1.0-beta.2
Published
Lithic's Embed SDK
Readme
lithic-embed
This library provides convenient access to the Lithic Embed API to easily create a modern card experience.
Installation
# npm
npm install lithic-embed
# bun
bun add lithic-embed
# pnpm
pnpm add lithic-embedGetting Started
Create a Card Embed
import LithicEmbed, {
EmbedMountError,
Environment,
} from "lithic-embed";
// Using your api-key generate a session token from your backend
// See https://docs.lithic.com/reference/createcardembedsession for more information.
const sessionToken = await fetchSessionToken();
const lithicEmbed = new LithicEmbed(Environment.PRODUCTION);
const cardEmbed = lithicEmbed.card(sessionToken);
try {
await cardEmbed.mountPan("#pan");
} catch (error) {
if (error instanceof EmbedMountError) {
// The iframe did not complete its render handshake in time.
}
}Using callbacks
You can configure callbacks to handle asynchronous notifications from the embeds.
import LithicEmbed, {
CardDetailsRequestError,
Environment,
PinSubmissionError,
EmbedUpdateState,
} from "lithic-embed";
// Using your api-key generate a session token from your backend
const sessionToken = await fetchSessionToken();
const lithicEmbed = new LithicEmbed(Environment.PRODUCTION);
// Individual callbacks are optional and are not required
const cardEmbed = lithicEmbed.card(sessionToken, {
onCopy: (cardEmbedType: CardEmbedType, success: boolean) => {
// Handle the whether the copy was successful or not
},
onUpdate: (state: EmbedUpdateState) => {
if (state === EmbedUpdateState.EmbedRendered) {
// Handle the embed rendered state
}
if (state === EmbedUpdateState.EmbedCardDetailsLoaded) {
// Handle the embed card details loaded state
}
},
});Revealing card details
By default, Lithic embeds are masked with only the pan showing the last 4 digits. To reveal the card details, call toggleMasking. toggleMasking will fetch and reveal the card details for all mounted card embeds.
await cardEmbed.mount({
pan: { selector: "#pan" },
cvv: { selector: "#cvv" },
expMonth: { selector: "#expMonth" },
expYear: { selector: "#expYear" },
});
try {
await cardEmbed.toggleMasking();
} catch (error) {
if (error instanceof CardDetailsRequestError) {
// Handle a card-details request failure or SDK timeout. Locally
// generated timeout errors do not include a code or request ID.
console.error(error.code);
console.error(error.requestId);
}
}Automatic style capture and syncing
By default, the lithic-embed SDK will sync the styles applied to the mount target.
const cardEmbed = lithicEmbed.card(sessionToken, {
syncStyles: true // Default true. Set to false to disable auto syncing of styles.
});
// Any styles applied #pan will be applied to the embed
await cardEmbed.mountPan("#pan");
// Explicit styles take precedence over computed values
await cardEmbed.mountPan("#pan", {
"color": "white",
"letter-spacing": "2px"
});Pseudo-selector styles
Explicit styles can include supported pseudo-selectors as one-level maps of kebab-case CSS declarations:
await cardEmbed.mountPan("#pan", {
"color": "black",
":hover": {
"color": "blue",
"background-color": "white"
},
":focus-visible": {
"outline": "2px solid blue"
}
});
await pinSettingEmbed.mount("#pin", {
"::placeholder": {
"color": "gray"
}
});The supported pseudo-selectors, in increasing cascade priority, are
:first-child, :last-child, :enabled, :read-write, :read-only,
::placeholder, :placeholder-shown, :valid, :invalid, :disabled,
:hover, :focus-within, :focus, :focus-visible, and :active. The
::placeholder pseudo-element styles placeholder text in PIN setting embeds.
Other pseudo-elements, functional selectors, selector chains, arbitrary
selectors, and nested pseudo-selectors are not supported.
Structural, focus, and form pseudo-classes apply only when the embedded element can match them. Explicit pseudo-selector styles are preserved when automatic style syncing updates the captured base declarations.
Styles are refreshed once the iframe renders and whenever observed properties change.
Create a PIN-setting Embed
Configure the placeholder and input layout when creating a PIN-setting embed:
const pinSettingEmbed = lithicEmbed.pinSetting(sessionToken, {
placeholder: "Enter your PIN",
layout: "SINGLE_INPUT",
});
await pinSettingEmbed.mount("#pin");The defaults are an empty placeholder and SINGLE_INPUT. Set layout to
MULTI_INPUT to render individual digit inputs.
The placeholder applies only to SINGLE_INPUT. These options apply when mounting
a new iframe and do not update an existing iframe.
const pinSettingEmbed = lithicEmbed.pinSetting(sessionToken, {});
await pinSettingEmbed.mount("#pin");
try {
await pinSettingEmbed.submit();
} catch (error) {
if (error instanceof PinSubmissionError) {
// Handle a PIN submission failure or SDK timeout. Locally generated
// timeout errors do not include a code or request ID.
console.error(error.code);
console.error(error.requestId);
}
}