From 323ffcc50b12db1fa22f31faf4cd81e7c25b56e5 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 25 Jan 2022 15:20:01 -0600 Subject: [PATCH] Implement payment checks in API --- backend/app.js | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/backend/app.js b/backend/app.js index eb77cb0..310c7d1 100644 --- a/backend/app.js +++ b/backend/app.js @@ -155,18 +155,18 @@ var blockInterval = setInterval( function() { if (pay.test(memo) && txData.confirmations < 100) { var match2 = pay.exec(memo); if (match2 != null) { - var session = match[1]; + var session = match2[1]; } if (txData.amount >= 0.001 && txData.amount < 0.005){ - exptime = blocktime + 3600; + exptime = txData.blocktime + 3600; } else if (txData.amount >= 0.005 && txData.amount < 0.025){ - exptime = blocktime + 24*3600; + exptime = txData.blocktime + 24*3600; } else if (txData.amount >= 0.025 && txData.amount < 0.1) { - exptime = blocktime + 7*24*3600; + exptime = txData.blocktime + 7*24*3600; } else if (txData.mount >= 0.1) { - exptime = blocktime + 4*7*24*3600; + exptime = txData.blocktime + 4*7*24*3600; } - + usermodel.findOne({session: session}).then(function(doc){ if(doc != null) { paymentmodel.findOne({address: doc.address, blocktime: txData.blocktime, amount: txData.amount}).then(function(payments){ @@ -182,9 +182,9 @@ var blockInterval = setInterval( function() { payment.save(function(error) { if (error) { console.log(error); + } else { + console.log('Payment saved'); } - - console.log('Payment saved'); }); } }); @@ -201,6 +201,32 @@ var blockInterval = setInterval( function() { }); }, 75000); +var payCheck = setInterval( function() { + + ownermodel.find({}).then((documents) => { + if(documents.length > 0){ + //console.log(documents); + async.each (documents, function(document, callback) { + paymentmodel.findOne({ address: document.address, expiration: {$gt: Date.now()}}).then(payment => { + if (payment != null){ + document.paid = true; + document.save(); + } else { + document.paid = false; + document.save(); + } + }); + }, function (err) { + if (err) { + console.log(err); + } else { + console.log("Owners checked for payment"); + } + }); + } + }); +}, 60000); + app.use(cors()); app.options('*', cors());