DtubeAuthProxy/peroidicActions/checkPayment.js
2021-05-30 01:58:06 +03:00

65 lines
3.2 KiB
JavaScript

mongo = require("../mongoHelp.js")
config = require("../config.js")
javalon = require("javalon")
function checkPayment(){
javalon.getAccountHistory(config.accountCreator.username, 0, (err, blocks) => {
if (err) {
console.log(err)
} else {
blocks.forEach(element => {
if (element.txs[0].type == 3) {
if (element.txs[0].data.memo.length > 8){
mongo.get(config.database, "users", {_id: element.txs[0].data.memo}).then(userArr => {
if (userArr.length != 1){
console.log("UserArray too long, this could indicate duplicate IDs which would be bad")
} else {
if (element.txs[0].data.memo == userArr[0]._id && element.txs[0].data.amount >= userArr[0].price && userArr[0].status == "Inactive" ){
tx = {
type: 0,
data: {
pub: userArr[0].keys.pub,
name: userArr[0].username
}
}
signedTx = javalon.sign(config.accountCreator.privKey,config.accountCreator.username, tx)
javalon.sendTransaction(signedTx, function(err, res) {
if (err){
console.log(err)
} else {
userArr[0].status = "Active"
mongo.update(config.database, "users", {_id: userArr[0]._id}, userArr[0])
console.log(res)
tx2 = {
type: 3,
data: {
amount: (Math.floor(parseInt(config.fee))/2),
memo: "Welcome to DTube",
receiver: userArr[0].username
}
}
signedTx2 = javalon.sign(config.accountCreator.privKey,config.accountCreator.username, tx2)
javalon.sendTransaction(signedTx2, function(err, res) {
if (err){
console.log(err)
} else {
console.log(res)
}
})
}
})
}
}
})
}
}
})
}
})
}
setInterval(checkPayment, 60000)