Facebook
From segun olamide, 1 Year ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 108
  1.  // Get all transactions
  2.         function getAllTransactions(accountRef = "100abc007") {
  3.             const startDate = new Date(2022, 7, 1);
  4.             const now = new Date();
  5.  
  6.             const startTimestamp = startDate.getTime();
  7.             const nowTimestamp = now.getTime();
  8.  
  9.             // authenticate first
  10.             fetch('https://sandbox.monnify.com/api/v1/auth/login', {
  11.                 method: 'POST',
  12.                 headers: {
  13.                     "Authorization": `Basic ${basicAuth}`
  14.                 }
  15.             }).then((encoded) => {
  16.                 return encoded.json();
  17.             })
  18.             .then((res) => {
  19.                     const { responseBody } = res;
  20.  
  21.                     console.log(responseBody);
  22.  
  23.                     // send reserved account details
  24.                     fetch(`https://sandbox.monnify.com/api/v1/transactions/search?from=${startTimestamp}&to=${nowTimestamp}`, {
  25.                         method: 'GET',
  26.                         headers: {
  27.                             "Content-Type": "application/json",
  28.                             "Authorization": `Bearer ${responseBody.accessToken}`,
  29.                         }
  30.                     })
  31.                     .then((main) => {  
  32.                         return  main.json();
  33.  
  34.                     })
  35.                     .catch((err) => {
  36.                         console.log(err);
  37.                     })
  38.                     .then((main) => {  
  39.                    
  40.                        
  41.  
  42.  
  43.                         mainTransactions = main.responseBody.content.filter((item) => {
  44.                             return item.paymentDescription===accountRef;
  45.                         });
  46.  
  47.  
  48.  
  49.                         // Sample Data to be deleted
  50.                         mainTransactions=[
  51.                             {
  52.                                 "customerDTO": {
  53.                                     "email": "[email protected]",
  54.                                     "name": "Segun Olamide",
  55.                                     "merchantCode": "ALJKHDALASD"
  56.                                 },
  57.                                 "providerAmount": 0.21,
  58.                                 "paymentMethod": "ACCOUNT_TRANSFER",
  59.                                 "createdOn": "2019-07-24T14:12:27.000+0000",
  60.                                 "amount": 100.00,
  61.                                 "flagged": false,
  62.                                 "providerCode": "98271",
  63.                                 "fee": 0.79,
  64.                                 "currencyCode": "NGN",
  65.                                 "completedOn": "2019-07-24T14:12:28.000+0000",
  66.                                 "paymentDescription": "Test Reserved Account",
  67.                                 "paymentStatus": "PAID",
  68.                                 "transactionReference": "MNFY|20190724141227|003374",
  69.                                 "paymentReference": "MNFY|20190724141227|003374",
  70.                                 "merchantCode": "ALJKHDALASD",
  71.                                 "merchantName": "Test Limited",
  72.                                 "payableAmount": 100.00,
  73.                                 "amountPaid": 100.00,
  74.                                 "completed": true
  75.                             },
  76.                             {
  77.                                 "customerDTO": {
  78.                                     "email": "[email protected]",
  79.                                     "name": "Joy Orji",
  80.                                     "merchantCode": "ALJKHDALASD"
  81.                                 },
  82.                                 "providerAmount": 0.11,
  83.                                 "paymentMethod": "ACCOUNT_TRANSFER",
  84.                                 "createdOn": "2019-07-24T11:14:45.000+0000",
  85.                                 "amount": 50.00,
  86.                                 "flagged": false,
  87.                                 "providerCode": "98271",
  88.                                 "fee": 0.79,
  89.                                 "currencyCode": "NGN",
  90.                                 "completedOn": "2019-07-24T11:14:45.000+0000",
  91.                                 "paymentDescription": "Test Reserved Account",
  92.                                 "paymentStatus": "PAID",
  93.                                 "transactionReference": "MNFY|20190724111444|003259",
  94.                                 "paymentReference": "MNFY|20190724111444|003259",
  95.                                 "merchantCode": "ALJKHDALASD",
  96.                                 "merchantName": "Test Limited",
  97.                                 "settleInstantly": true,
  98.                                 "payableAmount": 50.00,
  99.                                 "amountPaid": 50.00,
  100.                                 "completed": true
  101.                             }
  102.                         ];
  103.  
  104.  
  105.                         localStorage.removeItem('mainTransactions');
  106.  
  107.                         // console.log("Main",mainTransactions)
  108.                         // var mainAccount=main.responseBody.content;
  109.                         localStorage.setItem('mainTransactions', JSON.stringify(mainTransactions));                            
  110.  
  111.  
  112.                     })
  113.  
  114.             })
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.               // authenticate first
  122.               fetch('https://sandbox.monnify.com/api/v1/auth/login', {
  123.                 method: 'POST',
  124.                 headers: {
  125.                     "Authorization": `Basic ${basicAuth}`
  126.                 }
  127.                 }).then((encoded) => {
  128.                     return encoded.json();
  129.                 })
  130.                 .then((res) => {
  131.                         const { responseBody } = res;
  132.  
  133.                         console.log(responseBody);
  134.  
  135.                         // send reserved account details
  136.                         fetch(`https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts/transactions?accountReference=${accountRef}&page=0&size=1000`, {
  137.                                     method: 'GET',
  138.                                     headers: {
  139.                                         "Content-Type": "application/json",
  140.                                         "Authorization": `Bearer ${responseBody.accessToken}`,
  141.                                     }
  142.  
  143.                         })
  144.                         .then((reserved) => {  
  145.                             return  reserved.json();
  146.  
  147.                         })
  148.                         .catch((err) => {
  149.                             console.log(err);
  150.                         })
  151.                         .then((reserved) => {  
  152.                        
  153.                             console.log("Reserved",reserved.responseBody.content)
  154.                             var reservedAccount=reserved.responseBody.content;
  155.                             // console.log("From Main",mainAccount)
  156.  
  157.  
  158.                             // Sample data to be deleted
  159.  
  160.                             reservedAccount=[
  161.                                 {
  162.                                     "customerDTO": {
  163.                                         "email": "[email protected]",
  164.                                         "name": "Test Reserved Account",
  165.                                         "merchantCode": "ALJKHDALASD"
  166.                                     },
  167.                                     "providerAmount": 0.21,
  168.                                     "paymentMethod": "ACCOUNT_TRANSFER",
  169.                                     "createdOn": "2019-07-24T14:12:27.000+0000",
  170.                                     "amount": 100.00,
  171.                                     "flagged": false,
  172.                                     "providerCode": "98271",
  173.                                     "fee": 0.79,
  174.                                     "currencyCode": "NGN",
  175.                                     "completedOn": "2019-07-24T14:12:28.000+0000",
  176.                                     "paymentDescription": "Test Reserved Account",
  177.                                     "paymentStatus": "PAID",
  178.                                     "transactionReference": "MNFY|20190724141227|003374",
  179.                                     "paymentReference": "MNFY|20190724141227|003374",
  180.                                     "merchantCode": "ALJKHDALASD",
  181.                                     "merchantName": "Test Limited",
  182.                                     "payableAmount": 100.00,
  183.                                     "amountPaid": 100.00,
  184.                                     "completed": true
  185.                                 },
  186.                                 {
  187.                                     "customerDTO": {
  188.                                         "email": "[email protected]",
  189.                                         "name": "Test Reserved Account",
  190.                                         "merchantCode": "ALJKHDALASD"
  191.                                     },
  192.                                     "providerAmount": 0.11,
  193.                                     "paymentMethod": "ACCOUNT_TRANSFER",
  194.                                     "createdOn": "2019-07-24T11:14:45.000+0000",
  195.                                     "amount": 50.00,
  196.                                     "flagged": false,
  197.                                     "providerCode": "98271",
  198.                                     "fee": 0.79,
  199.                                     "currencyCode": "NGN",
  200.                                     "completedOn": "2019-07-24T11:14:45.000+0000",
  201.                                     "paymentDescription": "Test Reserved Account",
  202.                                     "paymentStatus": "PAID",
  203.                                     "transactionReference": "MNFY|20190724111444|003259",
  204.                                     "paymentReference": "MNFY|20190724111444|003259",
  205.                                     "merchantCode": "ALJKHDALASD",
  206.                                     "merchantName": "Test Limited",
  207.                                     "settleInstantly": true,
  208.                                     "payableAmount": 50.00,
  209.                                     "amountPaid": 50.00,
  210.                                     "completed": true
  211.                                 }
  212.                             ];
  213.  
  214.  
  215.                             var mainTransactions = JSON.parse(localStorage.getItem('mainTransactions'));
  216.                          
  217.                             // console.log('mainTransactions: ', JSON.parse(mainTransactions));
  218.  
  219.                             allTransactions = mainTransactions.concat(reservedAccount);
  220.                             // Put the object into storage
  221.                             localStorage.setItem('allTransactions', JSON.stringify(allTransactions));  
  222.                             console.log('allTransactions: ', allTransactions);
  223.                             console.log('allTransactions Length: ', allTransactions.length);
  224.  
  225.  
  226.  
  227.  
  228.                             var col = [];
  229.                                     var col = ['customerDTO','transactionReference','Vote','amount','paymentStatus','createdOn'];
  230.  
  231.                                     // Create a table.
  232.                                     var table = document.createElement("table");
  233.                                        table.setAttribute("id","history");
  234.  
  235.                                     // Create table header row using the extracted headers above.
  236.                                     var tr = table.insertRow(-1);                   // table row.
  237.  
  238.                                     for (var i = 0; i < col.length; i++) {
  239.                                         var th = document.createElement("th");      // table header.
  240.                                         th.innerHTML = col[i];
  241.                                         tr.appendChild(th);
  242.                                     }
  243.  
  244.                                     // add json data to the table as rows.
  245.                                     for (var i = 0; i < allTransactions.length; i++) {
  246.  
  247.                                         tr = table.insertRow(-1);
  248.  
  249.                                         for (var j = 0; j < col.length; j++) {
  250.                                             var tabCell = tr.insertCell(-1);
  251.  
  252.                                             if(j==0){
  253.                                                 tabCell.innerHTML =allTransactions[i][col[j]]['email'];
  254.  
  255.                                             }else if(j==2){
  256.                                             var cost=allTransactions[i][col[3]]/50;
  257.                                             tabCell.innerHTML =allTransactions[i][col[j]]=cost;
  258.                                             }
  259.                                             else{
  260.                                             tabCell.innerHTML = allTransactions[i][col[j]];
  261.                                             }
  262.  
  263.  
  264.                                         }
  265.                                     }
  266.  
  267.                                     // Now, add the newly created table with json data, to a container.
  268.                                     var divShowData = document.getElementById('history-wrapper');
  269.                                     divShowData.innerHTML = "";
  270.                                     divShowData.appendChild(table);
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.                         })//END THEN
  279.  
  280.                 })//END THEN
  281.  
  282.  
  283.                            
  284.  
  285.  
  286.        }