Free javascript Hosting


device-service-sdk-v1-0-0.js

Uploaded on May 19 2022 01:18 by leandro_franciscato

var elements

function initSdk(props, htmlElements) {
elements = htmlElements
if (!authorization(props.merchant_id, props.api_key)) {
notify("not authorized");
return
}

// here is the possibility to integrate more gateways
if (props.gateway == "saasconex") {
initSassConexSdk(props);
}
}

// here we're gonna have an authorization route, that will validate mechant_id and api_key
function authorization(merchant_id, api_key) {
if (!api_key) {
return false;
}
return true;
}

// that should be end user friendly...
function notify(msg) {
document.getElementById(elements.sdkOutput).innerHTML = msg;
}

// this log should call a merchant-services route
function logPreTransaction(data, merchant_id, api_key) {
console.log({ auth: { merchant_id, api_key }, data: data });
}

// this log should call a merchant-services route
function logPostTransaction(data, merchant_id, api_key) {
console.log({ auth: { merchant_id, api_key }, data: data });
}


// here we should be able to generate a unique customId to each device transaction, no matter the gateway it should be unique.
function generateCustomTransactionId(merchant_id, api_key) {
return btoa(merchant_id + api_key + new Date().toLocaleString())
}

function initSassConexSdk(props) {
var sdk = new SaaSConex({
device: props.gateway_device_id,
key: props.gateway_sdk_key,
debug: false,
timeout: 60000,
sslTls: true,
environment: props.gateway_environment,
success: function (data) {
notify("SDK Initialization Succeeded");
},
failure: function (data) {
notify("SDK Initialization Failed: check if the device informations are right!");
},
});

var processTransaction = function (cardType) {
var customId = generateCustomTransactionId()
var type = document.getElementById(elements.transactionType).value;
var amount = document.getElementById(elements.transactionAmount).value;
var captureSign =
cardType !== "DEBIT" &&
"true" ===
document.getElementById(elements.transactionCaptureSignature).value;

var transactionParams = {
type: type,
amount: amount,
captureSignature: captureSign,
};

var transaction = sdk.transaction(transactionParams);

// log Pre transaction
logPreTransaction({
transactionId: customId,
preTransaction: { transactionParams, props }
}, props.merchant_id, props.api_key)

sdk
.processTransaction(cardType, transaction)
.success(function (data) {

notify("Successful transaction..." + JSON.stringify(
{
message: data.transaction.gateway.message,
id: data.transaction.trace.terminalTransNum,
}
))

logPostTransaction({
transactionId: customId,
postTransaction: data
}, props.merchant_id, props.api_key)

})
.failure(function (data) {
notify("Failed transaction: " + data[0].message)
logPostTransaction({
transactionId: customId,
postTransaction: data
}, props.merchant_id, props.api_key)
});
};

var processCreditButton = document.getElementById(
elements.processTransactionCreditBtn
);

var processDebitButton = document.getElementById(
elements.processTransactionDebitBtn
);

processCreditButton.onclick = function () {
processTransaction("CREDIT");
};

processDebitButton.onclick = function () {
processTransaction("DEBIT");
};
}

Back to list