Skip to main content

Initialize the device for payments

Connect your device to the Stripe Terminal reader to start accepting in-person card payments. This process involves setting up a connection token provider and running the reader discovery flow.

Prerequisites​

You need all of the following to initialize your device.

Step 1: Implement a connection token provider​

The Stripe Terminal SDK requires a connection token to connect to the reader. You obtain this token from Swan's API using the requestTerminalConnectionToken mutation. The SDK handles token refresh automatically. Do not cache the token yourself.

Get a connection token from Swan​

Call requestTerminalConnectionToken from your server and pass the returned connectionToken to your provider.

mutation GetConnectionToken {
requestTerminalConnectionToken(
input: {
merchantProfileId: "$YOUR_MERCHANT_PROFILE_ID"
}
) {
... on RequestTerminalConnectionTokenSuccessPayload {
connectionToken
}
... on ForbiddenRejection {
__typename
message
}
... on InternalErrorRejection {
__typename
message
}
... on NotFoundRejection {
__typename
message
}
}
}

Wire up the provider​

Pass your tokenProvider function as a prop to StripeTerminalProvider. The function must return a connection token fetched from Swan's API.

import { StripeTerminalProvider } from '@stripe/stripe-terminal-react-native';

const fetchTokenProvider = async () => {
const connectionToken = "$YOUR_CONNECTION_TOKEN"; // fetch from Swan's API
return connectionToken;
};

function Root() {
return (
<StripeTerminalProvider
logLevel="verbose"
tokenProvider={fetchTokenProvider}
>
<App />
</StripeTerminalProvider>
);
}

Then call initialize from a component nested inside StripeTerminalProvider.

function App() {
const { initialize } = useStripeTerminal();

useEffect(() => {
initialize();
}, [initialize]);

return <View />;
}
note

Call initialize from a component nested inside StripeTerminalProvider, not from the component that contains the provider itself.

Step 2: Discover and connect to the reader​

With the token provider configured, discover the device's reader and connect to it.

Debug mode

The Stripe Terminal SDK blocks reader connections in debug builds as a security measure. Use a simulated reader for development (see code examples below).

iOS Terms and Conditions

On iOS, the first time a user connects to a reader, Apple's Terms and Conditions screen appears automatically. The user must accept to continue. This is handled by the SDK.

Use discoverReaders to find available readers, then connectReader to connect.

export default function MainScreen() {
const [status, setStatus] = useState<Reader.ConnectionStatus>("notConnected");
const { discoverReaders, connectReader, connectedReader, cancelDiscovering } =
useStripeTerminal({
onDidChangeConnectionStatus: setStatus,
onUpdateDiscoveredReaders: async (readers: Reader.Type[]) => {
const tapToPayReader = readers.find(
(reader) => reader.deviceType === "tapToPay"
);
if (tapToPayReader != null) {
const { error } = await connectReader(
{
reader: tapToPayReader,
locationId: "$LOCATION_ID",
},
"tapToPay"
);
if (error != null) {
Alert.alert(error.code, error.message);
}
}
},
});

const hasConnectedReader = connectedReader != null;

useEffect(() => {
if (!hasConnectedReader) {
discoverReaders({
discoveryMethod: "tapToPay",
simulated: false,
}).then(({ error }) => {
if (error != null) {
Alert.alert(error.code, error.message);
}
});
return () => {
cancelDiscovering();
};
}
}, [hasConnectedReader, discoverReaders, cancelDiscovering]);

return <Text>{status}</Text>;
}
Location ID

The $LOCATION_ID is the mainLocationId field on your InPersonCardMerchantPaymentMethod object. Query it after your payment method is Enabled.

Troubleshooting​

Firewall or restricted network​

If your app runs behind a firewall, configure it to allow access to the hosts required by Tap to Pay on iPhone. Refer to the Tap to Pay section of Apple's network configuration guide.

Reset Apple Terms and Conditions acceptance​

The first time a user connects to a reader on iOS, Apple's Terms and Conditions screen appears automatically. Once accepted, it won't appear again for that user.

To reset acceptance for testing:

  1. Go to Apple Business Register for Tap to Pay on iPhone.
  2. Sign in with the Apple ID used to accept the Terms and Conditions.
  3. From the list of Merchant IDs, select Remove and confirm to unlink the Apple ID.
  4. The Terms and Conditions screen appears again on the next connection attempt.

If the user closes the Terms and Conditions screen without accepting, the connection fails with tapToPayReaderTOSAcceptanceFailed. Prompt the user to accept and retry.

Next steps​

→ Create in-person card payments