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 ordermodel = require('./models/order');
const pricemodel = require('./models/price'); const pricemodel = require('./models/price');
const txmodel = require('./models/tx'); const txmodel = require('./models/tx');
const paymentmodel = require('./models/payment');
const zecTxModel = require('./models/zectxs.js'); const zecTxModel = require('./models/zectxs.js');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const stdrpc = require('stdrpc'); const stdrpc = require('stdrpc');
@ -98,6 +99,7 @@ var blockInterval = setInterval( function() {
console.log('Node periodic Zcash scan'); console.log('Node periodic Zcash scan');
rpc.z_listreceivedbyaddress(fullnode.addr, 1).then(txs => { rpc.z_listreceivedbyaddress(fullnode.addr, 1).then(txs => {
var re = /.*ZGO::(.*)\sReply-To:\s(z\w+)/; var re = /.*ZGO::(.*)\sReply-To:\s(z\w+)/;
var pay = /.*ZGOp::(.*)/;
async.each (txs, function(txData, callback) { async.each (txs, function(txData, callback) {
var memo = hexToString(txData.memo).replace(/\0/g, ''); var memo = hexToString(txData.memo).replace(/\0/g, '');
if (!txData.change) { if (!txData.change) {
@ -126,12 +128,6 @@ var blockInterval = setInterval( function() {
usermodel.findOne({address: address, session: session, blocktime: blocktime}).then(function(doc){ usermodel.findOne({address: address, session: session, blocktime: blocktime}).then(function(doc){
if (doc == null){ if (doc == null){
console.log('User not found', session, blocktime, amount); 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 n = crypto.randomInt(0, 10000000);
const pin = n.toString().padStart(6, '0'); const pin = n.toString().padStart(6, '0');
sendPin(pin, address); sendPin(pin, address);
@ -139,7 +135,6 @@ var blockInterval = setInterval( function() {
address: address, address: address,
session: session, session: session,
blocktime: blocktime, blocktime: blocktime,
expiration: expiration,
pin: pin, pin: pin,
validated: false validated: false
}); });
@ -152,27 +147,47 @@ 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'); 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');
}); });
} }
}).catch((err) => {
console.log(err);
}); });
} }
} });
} }
}, function (err) { }, function (err) {
if (err) { if (err) {

View file

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

View file

@ -7,4 +7,10 @@ export interface Owner {
taxValue: number; taxValue: number;
vat: boolean; vat: boolean;
vatValue: number; 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;
}