Init and connect
Initialize the SDK and connect the phone as a Tap to Pay reader.
1. Initialize the SDK
Call init() before mounting <LemonwayTerminalRoot>, for example at the top of your entry file.
import { LemonwayTerminal } from '@lemonway/terminal';
LemonwayTerminal.init({
env: 'sandbox', // 'sandbox' | 'production'
publicKey: 'pk_xxxxxxxxxxx',
tokenProvider: () => myBackend.post('/terminal/connection-token'),
});| Option | Required | Description |
|---|---|---|
env | yes | sandbox or production. Must match your keys. |
publicKey | yes | Your public key (pk_…). init() rejects any other key, so a secret key can never end up in the app. |
tokenProvider | yes | Async function that calls your connection token endpoint. Resolves { secret, locationId, onBehalfOf }, or just the secret string. |
shopId | no | Your shop id (sh_…). Attached to SDK diagnostics. |
locationId | no | Location (tml_…), if tokenProvider does not return it. Also settable with setLocationId(). |
onBehalfOf | no | Connected account (acct_…), iPhone only, if tokenProvider does not return it. |
logLevel | no | none (default) or verbose for native debug logs. |
simulated | no | Use a simulated reader in sandbox. Defaults to false. |
Set simulated: true to use the simulated reader in sandbox. See Test in sandbox.
init() throws a LemonwayTerminalError with code INVALID_CONFIG when an option is missing or invalid.
The SDK calls tokenProvider whenever it needs a connection token, and once before each connection to learn the location and the connected account. Values it returns override those passed to init(). If it fails, returns no secret or takes more than 15 seconds, the operation fails with CONNECTION_TOKEN_FAILED.
2. Mount LemonwayTerminalRoot
<LemonwayTerminalRoot> starts the terminal. Wrap every screen that uses the SDK:
import { LemonwayTerminalRoot } from '@lemonway/terminal';
export default function App() {
return (
<LemonwayTerminalRoot>
<CheckoutScreen />
</LemonwayTerminalRoot>
);
}On Android, the root requests foreground location before initializing the terminal. Subscribe to terminal.error before mounting so the app can handle startup failures. The root emits terminal.ready when initialization completes.
Initialization failureStartup failures preserve specific codes such as
LOCATION_PERMISSION_DENIED,CONNECTION_TOKEN_FAILEDand the iOS setup codes. Other startup failures useINITIALIZATION_FAILED. Resolve the cause, then remountLemonwayTerminalRootto retry.
3. Connect the phone
Call connectTapToPay() to discover and connect the phone in one step. The call resolves with the connected reader. It waits up to 30 seconds for the root to initialize and rejects with the startup error if initialization fails.
import { LemonwayTerminal, LemonwayTerminalError } from '@lemonway/terminal';
async function connectPhone() {
try {
return await LemonwayTerminal.connectTapToPay();
} catch (error) {
if (error instanceof LemonwayTerminalError) {
showError(error.code, error.message, error.hint);
}
throw error;
}
}- Keep the reader connected between sales. Call
disconnectReader()when the merchant signs out and update the UI when that call resolves. - Update the UI from reader events and
status()as the native connection state changes. - On iPhone, the first connection can take several minutes. Show progress from
reader.configuringuntilreader.configured.
Tap to Pay terms on iPhone
Call connectTapToPay({ allowTermsAcceptance: false }) during automatic setup to suppress the terms screen. If the terms still need acceptance, the connection can fail with TERMS_ACCEPTANCE_REQUIRED. Let an authorized merchant representative start a connection with allowTermsAcceptance: true to permit the screen.
Omitting the option preserves the platform default of true. Both connectTapToPay() and connectReader() accept the option. It applies on iOS. An existing reader for the same session can be reused.
To display the connection state, use status():
const { connectionStatus, connectedReader } = await LemonwayTerminal.status();
// connectionStatus: 'notConnected' | 'connecting' | 'connected' | 'discovering' | 'reconnecting'Before the terminal is ready, status() resolves { connectionStatus: 'notConnected', connectedReader: null }.
Advanced: discoverReaders() and connectReader()
Use the two-step flow if the app needs discovery and connection separately. Wait for terminal.ready or check isReady() first.
await LemonwayTerminal.discoverReaders();
const [phone] = LemonwayTerminal.getDiscoveredReaders();
if (phone) {
await LemonwayTerminal.connectReader(phone);
}Call discoverReaders() before connecting. The reader list can lag behind the discovery promise. Use reader.discovered to react when a reader is reported.
Switch shops
Wait for the active payment and reader operation to finish. Call init() with the new shop’s public key, shopId and tokenProvider, then call connectTapToPay() before collecting. The new shop’s token session supplies the location and, on iPhone, the connected account. You can also provide these values in init().
When the connection identity changes, the SDK disconnects the previous native session before clearing its credentials and connecting. Remounting the root alone leaves the native session active. Until reconnection, status().connectedReader is null and collectPayment() rejects with READER_CONNECTION_FAILED.
Updated 8 days ago