aboutsummaryrefslogtreecommitdiffstats
path: root/lib/webhooks.js
blob: 4741932abc544aff24186dea075bdb97bc9acfd3 (plain) (blame)
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
import config from "$config";
import wretch from "./wretch";
import { l } from "$lib/logging";

export const callWebhook = async (invoice, payment) => {
  try {
    if (!invoice || !payment) return;

    let { address, received, text, webhook } = invoice;

    if (webhook) {
      let { amount, confirmed, hash, memo, account } = payment;
      let { asset } = account;
      let secret = config.webhooks[webhook];

      l("calling webhook", webhook, amount, hash, address, text);
      let res = await wretch()
        .headers({ "x-webhook": secret })
        .url(webhook)
        .post({
          address,
          amount,
          asset,
          confirmed,
          hash,
          memo,
          received,
          text,
          webhook
        })
        .res();
      return res;
    }
  } catch (e) {
    console.log("problem calling webhook", e);
  }
};
const prefix = "(#" + seqno + ") "; msg.on("body", function(stream, info) { let buffer = ""; stream.on("data", function(chunk) { //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'})); buffer += chunk.toString("utf8"); }); stream.once("end", async function() { let accounts = config.imap.accounts; let a = Object.keys(accounts).find(a => buffer.includes(a)); let user_id = accounts[a]; if (!emails[seqno] && user_id) { let cad = parseFloat( buffer .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") .replace(",", "") ); emails[seqno] = cad; try { await db.transaction(async transaction => { const account = await db.Account.findOne({ where: { user_id, asset: config.liquid.btcasset, pubkey: null }, include: { model: db.User, as: "user" }, lock: transaction.LOCK.UPDATE, transaction }); let { user } = account; let rate = app.get("rates")["CAD"] * 1.02; let amount = Math.round((cad / rate) * 100000000); await account.increment({ balance: amount }, { transaction }); let payment = await db.Payment.create({ account_id: account.id, user_id: account.user_id, hash: `Interac deposit: ${cad} CAD`, amount, currency: "CAD", rate, received: true, tip: 0, confirmed: true, address: "", network: "COINOS" }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); emit(user.username, "account", account); emit(user.username, "payment", payment); l.info( "interac deposit detected for", user.username, cad, amount ); }); } catch (e) { console.log(e); } } }); }); msg.once("end", function() { // console.log(prefix + 'Finished'); }); imap.seq.addFlags(seqno, ["Seen"]); }); }, 10000); }); }); imap.connect();