-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.js
120 lines (96 loc) · 3.97 KB
/
payment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const {MOLLIE_KEY} = process.env;
const mollie_client = require('@mollie/api-client')({ apiKey: MOLLIE_KEY });
const {createDB} = require('./db');
const payment_database = createDB('mollie_db')
// invoice: object with invoice data (straight from moneybird)
// payment: object with payment data (straight from mollie)
// invoice_payment: object used with the local DB, essentially a join table: {_id: invoice.id, payment_id: payment.id}
// TODO: this currently only allows for one payment per invoice.
const createPaymentData = (invoice) => new Promise(async (resolve, reject) => {
try {
const payment = await createPaymentForInvoice(invoice);
const invoice_payment = {_id: invoice.id, payment_id: payment.id};
await payment_database.insert(invoice_payment, (err, docs) => {
if (err) {
reject(err);
}
resolve(payment);
});
} catch (err) {
reject(err);
}
});
const createPaymentForInvoice = (invoice) => new Promise ( async (resolve, reject) => {
let value;
// if the string only has a single character after the dot, add a zero
// e.g. 10.0 -> 10.00, 12.5 -> 12.50
if (invoice.total_unpaid.match(/\..$/)) {
value = `${invoice.total_unpaid}0`;
} else {
value = invoice.total_unpaid;
}
try {
const payment = await mollie_client.payments.create({
amount: {
value,
currency: invoice.currency,
},
// Mollie also uses the description in e.g. bank statements, so we put the payment reference here for moneybird to pick-up later
description: `${invoice.payment_reference} HyperDev invoice ${invoice.invoice_id}`,
redirectUrl: `http://dashboard.hyperdev.local:8081/portal/invoices?payment_invoice_id=${invoice.id}`,
});
resolve(payment);
} catch (err) {
console.debug(err)
reject(Error("Could not create payment with payment provider"));
}
});
const getPaymentUpdate = (payment_id) => mollie_client.payments.get(payment_id)
const getPaymentFromDB = (invoiceId) => new Promise( (resolve, reject) => {
console.debug("looking for invoice: ", invoiceId)
payment_database.findOne({_id: invoiceId}, (err, invoice_payment) => {
if (err) {
console.debug("Did not find invoice payment: ", err)
reject(err);
}
console.debug("found invoice payment: ", invoice_payment)
resolve(invoice_payment);
});
});
const removePaymentFromDB = (invoiceId) => new Promise( (resolve, reject) => {
console.debug("looking for invoice: ", invoiceId)
payment_database.remove({_id: invoiceId}, {multi: false}, (err, numRemoved) => {
if (err) {
console.debug("Did not remove invoice payment: ", err)
}
if (numRemoved === 1) {
console.debug("removed invoice payment: ", invoiceId)
resolve();
} else {
reject(`Did not delete invoice ${invoiceId}`);
}
});
});
exports.getPaymentForInvoice = (invoice) => new Promise( async (resolve, reject) => {
// look in DB for invoice_payment
let paymentData = await getPaymentFromDB(invoice.id).catch(null);
const payment = await (async () => {
if (paymentData === null) {
// no previous payment exists
return await createPaymentData(invoice);
} else {
// a previous payment exists, so we check it's status
const {payment_id} = paymentData;
const payment_update = await getPaymentUpdate(payment_id);
if (payment_update.status == 'expired') {
console.debug("payment is expired");
// Payment is expired, so we create a new one.
await removePaymentFromDB(invoice.id);
return await createPaymentData(invoice);
} else {
return payment_update;
}
}
})()
resolve(payment)
});