Update API server to process logins and pays

This commit is contained in:
Rene Vergara 2022-01-17 13:39:26 -06:00
parent 57781690a0
commit c9f84f1a28
4 changed files with 54 additions and 27 deletions

View File

@ -10,6 +10,7 @@ const itemmodel = require('./models/item');
const ordermodel = require('./models/order');
const pricemodel = require('./models/price');
const txmodel = require('./models/tx');
const paymentmodel = require('./models/payment');
const zecTxModel = require('./models/zectxs.js');
const mongoose = require('mongoose');
const stdrpc = require('stdrpc');
@ -98,6 +99,7 @@ var blockInterval = setInterval( function() {
console.log('Node periodic Zcash scan');
rpc.z_listreceivedbyaddress(fullnode.addr, 1).then(txs => {
var re = /.*ZGO::(.*)\sReply-To:\s(z\w+)/;
var pay = /.*ZGOp::(.*)/;
async.each (txs, function(txData, callback) {
var memo = hexToString(txData.memo).replace(/\0/g, '');
if (!txData.change) {
@ -126,12 +128,6 @@ var blockInterval = setInterval( function() {
usermodel.findOne({address: address, session: session, blocktime: blocktime}).then(function(doc){
if (doc == null){
console.log('User not found', session, blocktime, amount);
if (amount >= 0.001 && amount < 0.005){
expiration = blocktime + 3600;
} else if (amount >= 0.005){
expiration = blocktime + 24*3600;
}
console.log('exp', expiration);
const n = crypto.randomInt(0, 10000000);
const pin = n.toString().padStart(6, '0');
sendPin(pin, address);
@ -139,7 +135,6 @@ var blockInterval = setInterval( function() {
address: address,
session: session,
blocktime: blocktime,
expiration: expiration,
pin: pin,
validated: false
});
@ -152,28 +147,48 @@ var blockInterval = setInterval( function() {
});
}
});
ownermodel.findOne({address: address}).then(function (oDoc) {
if (oDoc == null) {
console.log('Owner not found', session);
var owner = new ownermodel({
address: address,
name: 'Z-Go-'.concat(address.substring(0,5))
});
owner.save().then(function(err) {
if (err) {
console.log(err);
}
console.log('Owner saved!');
}).catch(() => {
console.log('Owner exists');
});
}
}).catch((err) => {
console.log(err);
});
}
}
}
var exptime = 0;
if (pay.test(memo) && txData.confirmations < 100) {
var match2 = pay.exec(memo);
if (match2 != null) {
var session = match[1];
}
if (txData.amount >= 0.001 && txData.amount < 0.005){
exptime = blocktime + 3600;
} else if (txData.amount >= 0.005 && txData.amount < 0.025){
exptime = blocktime + 24*3600;
} else if (txData.amount >= 0.025 && txData.amount < 0.1) {
exptime = blocktime + 7*24*3600;
} else if (txData.mount >= 0.1) {
exptime = 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){
if(payments == null){
var payment = new paymentmodel({
address: doc.address,
blocktime: txData.blocktime,
expiration: new Date(exptime * 1000),
amount: txData.amount
});
payment.save(function(error) {
if (error) {
console.log(error);
}
console.log('Payment saved');
});
}
});
}
});
}
}, function (err) {
if (err) {
console.log(err);

View File

@ -4,7 +4,6 @@ const userSchema = mongoose.Schema({
address: {type: String, required:true},
session: {type: String, required:true},
blocktime: {type: Number, required:true},
expiration: {type: Number, required:true, default:0},
pin: {type: String, required:true},
validated: {type: Boolean, required:true}
});

View File

@ -7,4 +7,10 @@ export interface Owner {
taxValue: number;
vat: boolean;
vatValue: number;
email: string;
street: string;
city: string;
state: string;
postal: string;
phone: string;
}

7
src/app/payment.model.ts Normal file
View File

@ -0,0 +1,7 @@
export interface Payment {
_id?: string;
address: string;
blocktime: number;
expiration: string;
amount: number;
}