@nearpaydev/web-sdk v1.0.23
Nearpay Web Sdk
overview
Nearpay Web SDK is responsible for connecting, sending and reving data with the POS device using a web application interface.
Nearpay SDK will offer 2 ways of connecting to the POS RemotePos and USBPos
installation
to install the package use
npm i @nearpaydev/web-sdkRemote connection
to connect with RemotePos first create a pos object
import { RemotePos, Environments } from "@nearpaydev/web-sdk";
const pos = new RemotePos({ environment: Environments.sandbox });this pos object should be created only once then you should create a session with the pos object
const session = await pos.createSession({ pairing_code: "123456" });
console.log("token: ", session.token);
localStorage.setItem("remote-token", session.token);session token will be used in next connections, so you can save it in local storage
to connect with old token use the method pos.getSession
const session = await pos.getSession({ token: "hkjasd678asd8yhkjasd8utg" }); // use token saved from old connectionUSB connection
to connect with USBPos first create a pos object
import { USBPos, Environments } from "@nearpaydev/web-sdk";
const pos = new USBPos({ environment: Environments.sandbox });this pos object should be created only once then you should create a session with the pos
const session = await pos.createSession();
console.log("token: ", session.token);
localStorage.setItem("usb-token", session.token);session token will be used in next connections, so you can save it in local storage
to connect with old token use the method pos.getSession
const session = await pos.getSession({ token: "hkjasd678asd8yhkjasd8utg" }); // use token saved from old connectionTerminal
terminal is the object responsable for all queries and tranasactions
you can get the Terminal object from the method session.getTerminal
here are some examples:
const terminal = await session.getTerminal();here is some brief examples of using the terminal object
terminal.getInfo(); // gets information about this terminal
terminl.purchase({ amount: 100 }); // will generate a purchase transaction job with 1 SR
terminl.refund({
amount: 100,
original_transaction_uuid: "9e5e0d5c-3ce9-414a-88b1-9e09587b7266",
}); // will generate a refund transaction job with 1 SR
terminal.reverse({
original_transaction_uuid: "9e5e0d5c-3ce9-414a-88b1-9e09587b7266",
}); // reverse a transaction with (original_transaction_uuid)
terminal.reconcile({}); // reconcile this terminal
terminal.getTransaction({
transaction_uuid: "9e5e0d5c-3ce9-414a-88b1-9e09587b7266",
}); // get a transaction with uuid
terminal.getTransactionsList({ page: 1, page_size: 10 }); // reconcile this terminal
terminal.getReconciliation({
reconciliation_uuid: "9e5e0d5c-3ce9-414a-88b1-9e09587b7266",
}); // reconcile this terminal
terminal.getReconciliationsList({ page: 1, page_size: 10 }); // reconcile this terminal
terminal.stateListener((state) => console.log(state)); // reconcile this terminaltransaction jobs
transaction jobs will start the reader in the pos to perform a transaciton
transaction job could be of type (purchase) or (refund), nether the taransaction job will behave in the same way
you can listen to the transaction job states
const transactionJob = await terminl.purchase({ amount: 100 }); // will generate a purchase transaction job with 1 SR
transactionJob.stateListener((state) => console.log(state));
const result = await transactionJob.start();you can cancel a transaction using transactionJob.cancel
if it successed the original start function result will return with canceld result
const transactionJob = await terminl.purchase({ amount: 100 }); // will generate a purchase transaction job with 1 SR
const promise = transactionJob.start();
transactionJob.cancel(); // now promise will resolve to canceld result if the cancel successedStates
SDK Stetes
you can access the current Pos object state by using the method pos.state, states will be from the enum CONNECTION_STATE imported form @nearpaydev/web-sdk.
here is a short description of them
| Connection State | Description |
|---|---|
| CONNECTION_STATE.LOGEDOUT | Initial state, the SDK is fully disconnected from the POS device |
| CONNECTION_STATE.CONNECTING | SDK is trying to connect to the POS device |
| CONNECTION_STATE.CONNECTED | SDK is connected to the POS device and ready to do transactions |
| CONNECTION_STATE.DISCONNECTED | The SDK had unexpected disconnection after a successful connection |
transactions and queries can only happen in the CONNECTION_STATE.CONNECTED state
Terminal states
Terminal object alse have states, because the web client could be connected to the channel where the terminal isn't
you can access terminal states from the enum TERMINAL_STATE
here is a short description of them
| POS State | Description |
|---|---|
| connected | the terminal is responding |
| disconnected | the terminal is not responding |
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago