1.1.0 (@litesa/js-sdk)
The Instruments SDK saves a customer’s card without taking a payment. You get back an instrument: an id you can pay with later, plus the card’s scheme, issuing bank, issuing country and funding source. Use it when your price or eligibility depends on the card, for example a discount for one scheme or different terms for domestic cards, or when you want to keep a card on file before the first payment.
It is part of the same @litesa/js-sdk package as the Checkout SDK. Card details are entered in lite’s secure fields and encrypted inside lite-hosted iframes: your page’s code and your server never see them.
Secure fields, elements and the hosted page
lite offers three ways to collect a payment method on the web. A tokenization session supports two of them:How it works
- Your server creates a tokenization session with the lite API and gets back its
client_secret. - Your page starts the SDK with that
client_secret. - The customer enters a card in the secure fields, or authorizes Apple Pay.
- The SDK encrypts the card and saves it with lite. Nothing is charged.
- Your page receives the instrument and sends its
idto your server. - Your server fetches the instrument from lite, decides what to do, and optionally charges it.
Before you begin
You need:- A lite account with a terminal that has cards enabled. To save cards with Apple Pay, enable Apple Pay on the same terminal. A tokenization session offers only the payment methods enabled there.
- lite API keys, kept on your server.
- A backend endpoint that creates tokenization sessions.
- A page served over HTTPS, and a bundler that installs from npm.
1. Install the SDK
2. Create a tokenization session
A tokenization session is a checkout session that saves one card instead of taking a payment. Your server creates it with your API key, the same way as a payment session, withtype: "tokenization" and the customer’s id. It has no amount, currency or order.
terminal_id is optional and sets the card networks and wallets the session offers. Without it, the session uses your default online terminal. To use another, send its ID: open the terminal under Acceptance → Online in the dashboard, and the ID is the last part of the page address. See Create a checkout session for every field.
Create the session when the customer reaches the card step, not when the page loads. It expires after expiry seconds.
3. Pass the client secret to the SDK
Theclient_secret is a short-lived credential for this one session: a signed token that expires with the session. It lets the SDK read the session and save one card on it. It cannot read other sessions, and it cannot pay. Return it from your server to the page. Keep the session id on your server.
Do not log the client_secret.
Start the SDK with it:
isTokenizationSession(lite.session) is true for a tokenization session.
4. Collect the card
Secure fields (cards)
Mount the secure fields in your page and add your own Save card button. Enable it once every field is valid.tokenizesaves the card and never charges it. Passstore_for_future: trueonly with the customer’s consent to keep the card for later payments. Without it, the instrument is for a single payment.- Mount at most one field of each type. Each field’s
changereportsvalidonly, and never the card data. Style the fields as in the Checkout SDK guide. - Calling
tokenizeagain while a save is running returns the same promise, so a double click saves once. - A tokenization session has nothing to pay:
lite.pay()resolves with a failure whoseerrorCodeissession_type_unsupported, and it lists no saved cards.
Elements: Apple Pay
On a tokenization session, the Apple Pay button saves the card the customer authorizes instead of paying. The sheet reads Save card, with the amount shown as pending, and nothing is charged. Listen fortokenize. It carries the same result as lite.tokenize(), once the sheet closes:
create('apple_pay')throws when Apple Pay isn’t configured or active on the session’s terminal. Catch it, so that the card fields still mount.- Once created, the button shows only when the browser can pay with Apple Pay. Saving a card also needs Apple Pay version 4, which iOS 12.1 and macOS 10.14.1 and later have; on older devices the button doesn’t show. It never emits
resulton a tokenization session. - If the customer tries again in the same sheet after a failure,
tokenizefires once per attempt: the failure first, then the retry’s result. Treat the last one as final. cancelfires if the customer closes the sheet without saving.- The button saves the card for one payment, without
future_usage, so charge it within about 20 minutes.
5. Handle the tokenization result
Bothtokenize() and the Apple Pay button’s tokenize event give you a TokenizeResult:
result.instrument is a LiteInstrument. Its fields carry the API’s names:
Any card detail lite doesn’t know is
null. If you pass store_for_future: true and the customer already has this card stored, lite returns the instrument they already have, with its id.
On failure, result.errorCode says why, result.error describes it, and result.apiErrorCode carries lite’s own code when there is one:
tokenize() doesn’t throw for these: it resolves with the failure.
A session saves one card, whether from the secure fields or Apple Pay. A refused attempt doesn’t use the session up, so the customer can retry until it expires. After a success, disable your Save button.
6. Charge the instrument (optional)
Saving a card authorizes nothing, so the card can still be declined when you charge it. A card saved withoutstore_for_future: true is for one payment: charge it within about 20 minutes. There are two ways to charge an instrument.
With the Checkout SDK
Use this when the customer confirms the final price on your page. Your server creates a normal payment session for the same customer with the final amount. Start the SDK with that session’sclient_secret and pay with the instrument id. The customer doesn’t enter the card again, so this step needs no card fields.
result is the Checkout SDK’s payment result, with 3D Secure handled as for a new card: see Handle the result.
With the Payments API
A card saved with this SDK is stored asunscheduled, with no consent record, so charge it with POST /payments only while the customer is on your page, passing its id as payment_instrument.id with the amount, currency and the customer’s device details. To charge a customer who isn’t there, store the card with an agreement through the instruments API. See the Payments API guide.
7. Clean up
When the customer leaves the page without saving, for example on a route change in a single-page app, remove the fields and the SDK:lite.destroy() removes only lite’s hidden card iframe and clears the session: fields you didn’t destroy stay on the page. To save another card, create a new tokenization session and call Lite.init again.
What’s new in 1.1.0
- Tokenization sessions.
lite.tokenize()saves the card in the secure fields without paying. - Save cards with Apple Pay. On a tokenization session, the Apple Pay button saves the card and reports it on its
tokenizeevent. - Pay with an instrument.
lite.pay({ instrument_id })on a payment session takes the instrumenttokenize()returned.