Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const start = () => {
if (!order) return;

if (order.status === 'PENDING') {
// if we already have a holdInvoice we cancel it and return the money
// If we already have a holdInvoice we cancel it and return the money
if (!!order.hash) {
await cancelHoldInvoice({ hash: order.hash });
}
Expand Down
29 changes: 20 additions & 9 deletions ln/subscribe_invoice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { subscribeToInvoice, pay } = require('lightning');
const { Order, User } = require('../models');
const { Order, User, PendingPayment } = require('../models');
const lnd = require('./connect');
const messages = require('../bot/messages');

Expand Down Expand Up @@ -27,30 +27,41 @@ const subscribeInvoice = async (ctx, bot, id) => {
}
if (invoice.is_confirmed) {
const order = await Order.findOne({ hash: invoice.id });
order.status = 'CLOSED';
order.save();
order.status = 'PAID_HOLD_INVOICE';
const payment = await pay({ lnd, request: order.buyer_invoice });
if (payment.is_confirmed) {
// el bot envia un mensaj
order.status = 'SUCCESS';
const orderUser = await User.findOne({ _id: order.creator_id });
if (order.type === 'sell') {
const buyerUser = await User.findOne({ _id: order.buyer_id });
await messages.doneTakeSellMessage(bot, orderUser, buyerUser);
buyerUser.trades_completed++;
buyerUser.save();
await buyerUser.save();
} else if (order.type === 'buy') {
const sellerUser = await User.findOne({ _id: order.seller_id });
await messages.doneTakeBuyMessage(bot, orderUser, sellerUser);
sellerUser.trades_completed++;
sellerUser.save();
}
orderUser.trades_completed++;
orderUser.save();
await orderUser.save();
} else {
// TODO: guardo esto en una tabla de pagos pendientes,
// puedo correr luego un cronjob que haga estos pagos cada cierto tiempo
console.log('el pago a bob fallo pero guardo esto en una tabla para intentarlo mas tarde');
// TODO: cronjob que haga estos pagos cada cierto tiempo y con cada intento incremente 'attempts'
// si attemps > 3 el admin se debe comunicar directamente con el usuario para hacer el pago manualmente
const buyerUser = await User.findOne({ _id: order.buyer_id });
const message = 'No he podido pagar tu invoice, en unos minutos intentaré pagarla nuevamente, asegúrate que tu nodo/wallet esté online';
await messages.customMessage(bot, buyerUser, message);
const pp = new PendingPayment({
amount: order.amount,
payment_request: order.buyer_invoice,
user_id: buyerUser._id,
description: order.description,
hash: order.hash,
order_id: order._id,
});
await pp.save();
}
await order.save();
}
});
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const User = require('./user');
const Order = require('./order');
const PendingPayment = require('./pending_payment');

module.exports = {
User,
Order,
PendingPayment,
};
2 changes: 1 addition & 1 deletion models/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const OrderSchema = new mongoose.Schema({
canceled_by: { type: String },
status: {
type: String,
enum: ['WAITING_PAYMENT', 'PENDING', 'ACTIVE', 'CLOSED', 'DISPUTE', 'CANCELED'],
enum: ['WAITING_PAYMENT', 'PENDING', 'ACTIVE', 'CLOSED', 'DISPUTE', 'CANCELED', 'SUCCESS', 'PAID_HODL_INVOICE'],
default: 'WAITING_PAYMENT',
},
type: { type: String },
Expand Down
21 changes: 21 additions & 0 deletions models/pending_payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require('mongoose');

const PendingPaymentSchema = new mongoose.Schema({
description: { type: String },
amount: { // amount in satoshis
type: Number,
min: [100, 'Minimum amount is 100 sats'],
validate : {
validator : Number.isInteger,
message : '{VALUE} is not an integer value'
}
},
attempts: { type: Number, min: 1, default: 0 },
payment_request: { type: String },
hash: { type: String },
created_at: { type: Date, default: Date.now },
user_id: { type: String },
order_id: { type: String },
});

module.exports = mongoose.model('PendingPayment', PendingPaymentSchema);