1.0.20 • Published 9 months ago

@nearpaydev/web-sdk v1.0.20

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

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-sdk

Remote 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 connection

USB 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 connection

Terminal

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 terminal

transaction 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 successed

States

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 StateDescription
CONNECTION_STATE.LOGEDOUTInitial state, the SDK is fully disconnected from the POS device
CONNECTION_STATE.CONNECTINGSDK is trying to connect to the POS device
CONNECTION_STATE.CONNECTEDSDK is connected to the POS device and ready to do transactions
CONNECTION_STATE.DISCONNECTEDThe 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 StateDescription
connectedthe terminal is responding
disconnectedthe terminal is not responding
1.0.20

9 months ago

1.0.19

12 months ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago