Example Usage
Below is an example in Node.js and CPP on how to scan a customer coupon and create a transaction on the k42 platform.Copy
import * as sdk from "k42-sdk"
const main = async () => {
const initResult = sdk.initialize({
platform_endpoint: "https://mesh.transit.dev",
auth_token: "atu_abc123",
pos_id: "pos-1",
location_id: "loc-1"
});
if (!initResult.success) {
console.log(`Failed to initialize ${initResult.message}`);
return;
}
const session = sdk.createSession("customer-ref");
sdk.setSessionBasketTotal(100);
const scanResult = await sdk.tokenScan(30000, 3, "platform");
if (!scanResult.success) {
console.log(`Failed to scan ${scanResult.message}`);
sdk.closeSession();
return;
}
const txResult = await sdk.createTransaction("tx-ref", scanResult.token);
if (!txResult.success) {
console.log(`Failed to create transaction ${txResult.message}`);
sdk.closeSession();
return;
}
const finalizeResult = await sdk.finalizeTransaction(txResult.transaction_id);
if (!finalizeResult.success) {
console.log(`Failed to create transaction ${finalizeResult.message}`);
sdk.closeSession();
return;
}
console.log('Transaction successful');
console.log(`tx id: ${finalizeResult.id}`);
console.log(`tx auth_code: ${finalizeResult.auth_code}`);
console.log(`tx credit amount: ${finalizeResult.effects[0].amount}`);
console.log(`tx coupon id: ${finalizeResult.coupon.id}`);
const closeResult = sdk.closeSession();
}
