Payrails class
Reference for thePayrails class and package exports.
The Payrails class is the entry point of @payrails/web-sdk. You never
construct it directly — call the static Payrails.init() with the SDK
configuration returned by your server, then use the resulting instance to create
UI elements, payment buttons, and to read data from the payment session.
TEST / PRODUCTION) and merchant identifier are sourced from
the server-provided SDK configuration inside initResponse.data. There is no
client-side override.
Initialization
Payrails.init(initResponse, options?) (static)
init is
asynchronous: it loads the SDK bundle and stylesheet from the Payrails CDN
(assets.payrails.io — allow it in script-src and style-src if you set a
Content-Security-Policy) and resolves once the instance is ready. It requires a
DOM — call it in the browser, not during server-side rendering.
PayrailsClientOptions:
Rejects with a
PayrailsError (PAYRAILS_INIT_FAILED) when the configuration
string is invalid or when the SDK bundle cannot be loaded (blocked CDN, offline,
or a 15-second timeout).
update(updateOptions)
setState({ instrument })
cardList) lets the shopper pick a saved card.
instrument is a stored instrument object as returned by
getStoredInstruments().
UI elements
All elements returned by these methods exposemount(selector: string) and
unmount().
dropin(options)
DropinOptions:
Subscribe to drop-in events via
payrails.on(...) (instance-level events like
success) and the drop-in’s own .on(...) (element-level events like
paymentOptionSelected).
cardForm(options?)
Payrails instance; a mounted paymentButton is wired to it
automatically. Key CardFormOptions fields: showCardHolderName,
showSingleExpiryDateField, layout (rows of ElementType field names),
translations (placeholders, labels, error texts), appearance
(CardFormAppearance), fonts, installmentConfig, and
enrollInstrumentToNetworkOffers. Subscribe to events (change, ready,
focus, blur, …) via cardForm.on(...).
cardList(options?)
options.onCardChange(selectedCard) fires when the shopper selects a card;
selection also enables a mounted paymentButton. options.appearance styles
the list.
apmForm(options)
apmButton
to pay.
Options: paymentMethodCode (required), appearance, translations, id. The
method throws a PayrailsError when the session has no form for that payment
method. The id defaults to payrails-apm-form-<paymentMethodCode>; pass id
only if you mount two forms for the same method.
form.mount() returns a promise, so await it or attach a .catch. It rejects
when the selector isn’t found, or when the form can’t be drawn, for example
because a field has no label from the configuration or your translations. A
form that can’t be drawn rejects with a PayrailsError whose context.reason
is 'unrenderable-schema'. Branch on that to offer the shopper another way to
pay:
appearance takes the same rules as other elements, plus actionScreen for
the screen the form shows while an approve-in-app method waits; see
ApmFormAppearance.
dynamicElement(options)
apmForm instead, which renders in a Payrails-hosted frame.
options.paymentMethod is required; the method
throws a PayrailsError when the session has no form for that payment
method. Other options: appearance, translations, fieldOverrides. Subscribeto events via
element.on(...) .
collectContainer(containerOptions)
cardForm. Lets you
create and mount individual secure fields (createCollectElement), validate,
and collect encrypted card data yourself. CollectContainerOptions:
containerId, fonts. Each secure field styles itself via its own appearance
option on createCollectElement.
Payment buttons
apmButton(options)
form from
apmForm, or a paymentMethodCode on its own for a method
with nothing to collect. Passing both, or neither, throws. Methods that redirect
the shopper still use genericRedirectButton;
apmButton stops at pending for them.
With a form, a click validates it first. If a field is invalid, the form shows
the error, the button emits validate with isValid: false, and nothing is
submitted. Otherwise the button runs the buttonClicked and requestStart
gates and submits the payment, the same as the card payment button.
Options: form or paymentMethodCode (exactly one), translations.label,
appearance, returnInfo, id, alwaysStoreInstrument. The id defaults to
payrails-apm-button-<paymentMethodCode>. alwaysStoreInstrument (default
false) saves the instrument for future payments. The button shows no save
checkbox, so set it only when you already have the shopper’s consent, for
example for a SEPA mandate. The SDK ignores it on a merchant of record
workspace. See
Save the instrument for future payments.
Subscribe to outcomes via
payrails.on('success' | 'failed' | 'pending', ...), and to validate via the
returned button’s .on(...). See the
event reference.
Approve-in-app methods
Some methods, such as MB WAY, have the shopper approve the payment in another app. For these the button doesn’t stop atpending. The form switches to a wait
screen with the payment method’s logo and instructions for the shopper, the
button is removed, and the SDK waits for the shopper to approve:
- Approved: the wait screen goes away and
successfires. Show your own confirmation. - Declined, or not approved in time: the form comes back with what the
shopper typed, the button comes back,
sessionExpiredfires, and thenfailedwithAUTHORIZATION_ERROR. Return fresh init options fromsessionExpiredso the shopper’s retry starts a new payment. - The SDK couldn’t confirm the result, for example because the session
expired during the wait: the same happens, but
failedcarriesAUTHENTICATION_ERRORorUNKNOWN_ERROR. The shopper may still approve in their app, so check the payment’s status before letting them retry.
paymentButton(options)
cardList/setState.
Options: translations.label, appearance, redirectFor3DS, and
disabledByDefault (default true; the button enables once the card form is
valid or a saved card is selected). Subscribe to payment outcomes via
payrails.on(...) and to button-specific events (stateChanged, validate)
via the returned button’s .on(...)
googlePayButton(options)
merchantName (overrides the display name
from the backend config; the merchant ID is always taken from the backend),
redirectFor3DS, styles (buttonColor, buttonType, buttonSizeMode,
locale), returnInfo, and store-instrument checkbox options. The environment
is inherited from Payrails.init(...). Read await button.isAvailable (a
Promise<boolean>) for wallet availability and subscribe to payrails.on(...)
for payment outcomes.
isGooglePayAvailable(merchantName?)
true when the shopper’s browser/device can pay with Google Pay for
the current session configuration. Pass merchantName to override the display
name that Google Pay resolves during the availability check.
applePayButton(options)
abortAfterAuthorizeFailed,
translations, styles (type, e.g. 'buy'/'checkout'; style, e.g.
'black'; locale), and store-instrument checkbox options. Read
await button.isAvailable (a Promise<boolean>) for wallet availability,
subscribe to payrails.on('deliveryAddressChanged', …) for the express-checkout
address sheet, and to payrails.on(...) for payment outcomes.
isApplePayAvailable()
true when the browser supports Apple Pay (ApplePaySession) and the
Apple Pay SDK loads; false otherwise (never rejects).
paypalButton(options?)
styles (color, height, label, shape,
tagline, locale), and store-instrument checkbox options. Read
await button.isAvailable (a Promise<boolean>) for wallet availability,
subscribe to payrails.on('deliveryAddressChanged', …) for express-checkout
address changes (PayPal payload shape), and to payrails.on(...) for payment
outcomes. In PayPal express mode the store-instrument checkbox is forced off.
leanButton(options)
id, translations.label,
appearance (the button), dialogCustomization (the Lean-hosted dialog’s theme
config), and returnInfo. Subscribe to payment outcomes via payrails.on(...).
genericRedirectButton(options)
options.paymentMethod
(the payment method configuration, required) selects the method; other options:
translations.label, appearance (for Revolut Pay, pass revolutOptions — the
RevolutPayStyles shape — instead), openInNewTab, and returnInfo. Subscribe
to payment outcomes via payrails.on(...); the redirect itself can be
intercepted via the actionRequired event (see
events).
Stored instruments and payment methods
Stored instruments are plain objects withid, status, paymentMethod,
displayName?, data? (e.g. bin, suffix, network for cards, email for
PayPal), and default?.
getSavedCreditCards()
Returns the shopper’s stored card instruments.
getSavedGooglePayAccounts()
Returns the shopper’s stored Google Pay instruments with status enabled.
getSavedApplePayAccounts()
Returns the shopper’s stored Apple Pay instruments.
getSavedPaypalAccounts()
Returns the shopper’s stored PayPal instruments with status enabled, or [].
getStoredInstruments()
Returns stored instruments across all payment methods. Google Pay and PayPal
instruments are included only when their status is enabled.
getStoredInstrumentsByPaymentMethod(paymentMethod)
getStoredInstruments() filtered to one payment method
code (a PAYMENT_METHOD_CODES value such as 'card' or 'payPal').
getAvailablePaymentMethods()
PAYMENT_METHOD_CODES).
Data and API
api(config)
query(key, params?)
null when the value or
key is unknown. Supported keys: holderReference, amount, executionId,
binLookup, instrumentDelete, instrumentUpdate (API links),
paymentMethodConfig and paymentMethodInstruments (both require
params.paymentMethodCode; paymentMethodConfig also accepts 'all' or
'redirect' to return arrays).
binLookup()
bin, network, and when available
localNetwork (co-branded cards), issuer, issuerCountry, type. Logs a
warning and resolves undefined when no container exists, and resolves null
when BIN lookup is not enabled for the session.
Instance events
on(name, handler)
success, failed, pending,
buttonClicked, requestStart, actionRequired, sessionExpired,
deliveryAddressChanged) and returns an unsubscribe function.
off(name, handler)
on.
Other package exports
Values and enums:
Types: