// Get all transactions
function getAllTransactions(accountRef = "100abc007") {
const startDate = new Date(2022, 7, 1);
const now = new Date();
const startTimestamp = startDate.getTime();
const nowTimestamp = now.getTime();
// authenticate first
fetch('https://sandbox.monnify.com/api/v1/auth/login', {
method: 'POST',
headers: {
"Authorization": `Basic ${basicAuth}`
}
}).then((encoded) => {
return encoded.json();
})
.then((res) => {
const { responseBody } = res;
console.log(responseBody);
// send reserved account details
fetch(`https://sandbox.monnify.com/api/v1/transactions/search?from=${startTimestamp}&to=${nowTimestamp}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${responseBody.accessToken}`,
}
})
.then((main) => {
return main.json();
})
.catch((err) => {
console.log(err);
})
.then((main) => {
mainTransactions = main.responseBody.content.filter((item) => {
return item.paymentDescription===accountRef;
});
// Sample Data to be deleted
mainTransactions=[
{
"customerDTO": {
"name": "Segun Olamide",
"merchantCode": "ALJKHDALASD"
},
"providerAmount": 0.21,
"paymentMethod": "ACCOUNT_TRANSFER",
"createdOn": "2019-07-24T14:12:27.000+0000",
"amount": 100.00,
"flagged": false,
"providerCode": "98271",
"fee": 0.79,
"currencyCode": "NGN",
"completedOn": "2019-07-24T14:12:28.000+0000",
"paymentDescription": "Test Reserved Account",
"paymentStatus": "PAID",
"transactionReference": "MNFY|20190724141227|003374",
"paymentReference": "MNFY|20190724141227|003374",
"merchantCode": "ALJKHDALASD",
"merchantName": "Test Limited",
"payableAmount": 100.00,
"amountPaid": 100.00,
"completed": true
},
{
"customerDTO": {
"name": "Joy Orji",
"merchantCode": "ALJKHDALASD"
},
"providerAmount": 0.11,
"paymentMethod": "ACCOUNT_TRANSFER",
"createdOn": "2019-07-24T11:14:45.000+0000",
"amount": 50.00,
"flagged": false,
"providerCode": "98271",
"fee": 0.79,
"currencyCode": "NGN",
"completedOn": "2019-07-24T11:14:45.000+0000",
"paymentDescription": "Test Reserved Account",
"paymentStatus": "PAID",
"transactionReference": "MNFY|20190724111444|003259",
"paymentReference": "MNFY|20190724111444|003259",
"merchantCode": "ALJKHDALASD",
"merchantName": "Test Limited",
"settleInstantly": true,
"payableAmount": 50.00,
"amountPaid": 50.00,
"completed": true
}
];
localStorage.removeItem('mainTransactions');
// console.log("Main",mainTransactions)
// var mainAccount=main.responseBody.content;
localStorage.setItem('mainTransactions', JSON.stringify(mainTransactions));
})
})
// authenticate first
fetch('https://sandbox.monnify.com/api/v1/auth/login', {
method: 'POST',
headers: {
"Authorization": `Basic ${basicAuth}`
}
}).then((encoded) => {
return encoded.json();
})
.then((res) => {
const { responseBody } = res;
console.log(responseBody);
// send reserved account details
fetch(`https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts/transactions?accountReference=${accountRef}&page=0&size=1000`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${responseBody.accessToken}`,
}
})
.then((reserved) => {
return reserved.json();
})
.catch((err) => {
console.log(err);
})
.then((reserved) => {
console.log("Reserved",reserved.responseBody.content)
var reservedAccount=reserved.responseBody.content;
// console.log("From Main",mainAccount)
// Sample data to be deleted
reservedAccount=[
{
"customerDTO": {
"name": "Test Reserved Account",
"merchantCode": "ALJKHDALASD"
},
"providerAmount": 0.21,
"paymentMethod": "ACCOUNT_TRANSFER",
"createdOn": "2019-07-24T14:12:27.000+0000",
"amount": 100.00,
"flagged": false,
"providerCode": "98271",
"fee": 0.79,
"currencyCode": "NGN",
"completedOn": "2019-07-24T14:12:28.000+0000",
"paymentDescription": "Test Reserved Account",
"paymentStatus": "PAID",
"transactionReference": "MNFY|20190724141227|003374",
"paymentReference": "MNFY|20190724141227|003374",
"merchantCode": "ALJKHDALASD",
"merchantName": "Test Limited",
"payableAmount": 100.00,
"amountPaid": 100.00,
"completed": true
},
{
"customerDTO": {
"name": "Test Reserved Account",
"merchantCode": "ALJKHDALASD"
},
"providerAmount": 0.11,
"paymentMethod": "ACCOUNT_TRANSFER",
"createdOn": "2019-07-24T11:14:45.000+0000",
"amount": 50.00,
"flagged": false,
"providerCode": "98271",
"fee": 0.79,
"currencyCode": "NGN",
"completedOn": "2019-07-24T11:14:45.000+0000",
"paymentDescription": "Test Reserved Account",
"paymentStatus": "PAID",
"transactionReference": "MNFY|20190724111444|003259",
"paymentReference": "MNFY|20190724111444|003259",
"merchantCode": "ALJKHDALASD",
"merchantName": "Test Limited",
"settleInstantly": true,
"payableAmount": 50.00,
"amountPaid": 50.00,
"completed": true
}
];
var mainTransactions = JSON.parse(localStorage.getItem('mainTransactions'));
// console.log('mainTransactions: ', JSON.parse(mainTransactions));
allTransactions = mainTransactions.concat(reservedAccount);
// Put the object into storage
localStorage.setItem('allTransactions', JSON.stringify(allTransactions));
console.log('allTransactions: ', allTransactions);
console.log('allTransactions Length: ', allTransactions.length);
var col = [];
var col = ['customerDTO','transactionReference','Vote','amount','paymentStatus','createdOn'];
// Create a table.
var table = document.createElement("table");
table.setAttribute("id","history");
// Create table header row using the extracted headers above.
var tr = table.insertRow(-1); // table row.
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th"); // table header.
th.innerHTML = col[i];
tr.appendChild(th);
}
// add json data to the table as rows.
for (var i = 0; i < allTransactions.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
if(j==0){
tabCell.innerHTML =allTransactions[i][col[j]]['email'];
}else if(j==2){
var cost=allTransactions[i][col[3]]/50;
tabCell.innerHTML =allTransactions[i][col[j]]=cost;
}
else{
tabCell.innerHTML = allTransactions[i][col[j]];
}
}
}
// Now, add the newly created table with json data, to a container.
var divShowData = document.getElementById('history-wrapper');
divShowData.innerHTML = "";
divShowData.appendChild(table);
})//END THEN
})//END THEN
}