The Mad Stripe SDK is a Stripe Terminal implementation for React Native. It basically implements a proxy like logic that translates both native implementations and makes them available to use in every React Native project that needs it.
npm install mad-stripe-sdk
import { StripeTerminalSDK } from 'mad-stripe-sdk';
In order to to initialize the SDK, a valid token must be set. This implementation handles this similarly as the native libraries do by fetching a token and the saving it.
const initTerminal = async () =>
// The create method expects a set of options, which basically encapsulate the callback functions described here.
await StripeTerminalSDK.create({
fetchConnectionToken: async () => {
const resp = await fetch(
'{the url where the Stripe SDK is}',
{
method: 'POST',
}
);
const data = await resp.json();
token = data.secret;
return token;
},
onUnexpectedReaderDisconnect: () => {
console.log('Reader unexpectedly disconnected.');
},
});
Discovering readers works using the Observable pattern and notifies when the discovery is done.
public static discoverReaders(
// For the first version, the implementation supports Bluetooth and Internet reader discovery.
discoveryMethod: DiscoveryMethod,
// If you don't have a Reader to test, you can simulate a connection.
simulated: boolean
): Observable<Reader[]> {
this.ensureInitialized();
return new Observable((subscriber) => {
this.listener.addListener(
// The name of the event that lets us know which Readers are nearby.
'readerDiscoveryCompletion',
(event: { readers?: Reader[] }) => {
try {
const readers =
event!.readers!.map((reader: Reader) => {
return reader;
}) || [];
subscriber.next(readers);
} catch (error) {
console.log(error);
}
}
);
// Starts the actual discovery using the provided method.
this.isDiscovering = true;
MadStripeSdk.discoverReaders(discoveryMethod, simulated)
.then(() => {
this.isDiscovering = false;
subscriber.complete();
})
.catch((err: any) => {
this.isDiscovering = false;
subscriber.error(err);
});
return {
unsubscribe: () => {
// Everytime we completed the discovery flow, it's important to tell the Terminal so.
this.cancelDiscoverReaders();
},
};
});
}
It's important to safely save the location id and serial number of the previously discovered reader since we're about to use it here.
public static async connectBluetoothReader(
// The reader identifier
serialNumber: string,
// Where is it
locationId: string
): Promise<Reader> {
this.ensureInitialized();
const data = await MadStripeSdk.connectBluetoothReader(
serialNumber,
locationId
);
return data?.reader ?? null;
}
For further information on how to use the SDK, please check the provided example.