aboutsummaryrefslogtreecommitdiffstats
path: root/routes/payments.js
blob: bc9e22d5e3a6926e375f9406ba19225b875813ff (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import app from "app";
import config from "config";
import store from "lib/store";

import { auth, adminAuth, optionalAuth } from "lib/passport";
import fs from "fs";
import { join } from "path";
import send from "./send";
import { err, l, warn } from "lib/logging";
import { fail } from "lib/utils";
import { emit } from "lib/sockets";

import lnRoutes from "./lightning/index";
import "./lightning/receive";

app.post("/send", auth, send);

app.post("/lightning/parse", lnRoutes.parse);
app.post("/lightning/query", auth, lnRoutes.query);
app.post("/lightning/send", auth, lnRoutes.send);

app.get(
  "/payments",
  auth,
  async ({ uuid, query: { start, end, limit, offset } }, res) => {
    if (limit) limit = parseInt(limit);
    if (offset) offset = parseInt(offset);

    // if (start || end) where.createdAt = {};
    // if (start) where.createdAt[Op.gte] = new Date(parseInt(start));
    // if (end) where.createdAt[Op.lte] = new Date(parseInt(end));

    let payments = await redis.get(`${uuid}:payments`);
    res.send({ payments, total });
  }
);

// app.get("/voucher/:redeemcode", async (req, res) => {
//   try {
//     const { redeemcode } = req.params;
//     let payment = await db.Payment.findOne({
//       where: {
//         redeemcode
//       },
//       include: { all: true }
//     });
//
//     payment = payment.get({ plain: true });
//     payment.redeemer = payment["with"];
//
//     if (!payment) fail("invalid code");
//
//     res.send(payment);
//   } catch (e) {
//     res.code(500).send(e.message);
//   }
// });

// let redeeming = {};
// app.post("/redeem", optionalAuth, async function(req, res) {
//   const { redeemcode } = req.body;
//   try {
//     await db.transaction(async transaction => {
//       if (redeeming[redeemcode]) fail("redemption in progress");
//       redeeming[redeemcode] = true;
//       if (!redeemcode) fail("no code provided");
//
//       let { user } = req;
//
//       const source = await db.Payment.findOne({
//         where: {
//           redeemcode: req.body.redeemcode
//         },
//         include: {
//           model: db.Account,
//           as: "account"
//         },
//         lock: transaction.LOCK.UPDATE,
//         transaction
//       });
//
//       l("redeeming", redeemcode);
//
//       if (!source) fail("Invalid code");
//       if (source.redeemed) fail("Voucher has already been redeemed");
//       let { amount } = source;
//       amount = -amount;
//
//       if (!user) {
//         const ip =
//           req.headers["x-forwarded-for"] || req.connection.remoteAddress;
//
//         user = await register(
//           {
//             username: redeemcode.substr(0, 8),
//             password: ""
//           },
//           ip,
//           false
//         );
//
//         let payload = { username: user.username };
//         let token = jwt.sign(payload, config.jwt);
//         res.cookie("token", token, {
//           expires: new Date(Date.now() + 432000000)
//         });
//
//         delete redeeming[redeemcode];
//         return res.send({ user });
//       }
//
//       let account = await getAccount(source.account.asset, user, transaction);
//       let { hash, memo, confirmed, fee, network } = source;
//
//       source.redeemed = true;
//       (source.with_id = user.id), await source.save({ transaction });
//
//       let payment = await db.Payment.create(
//         {
//           amount,
//           account_id: account.id,
//           user_id: user.id,
//           hash: "Voucher " + redeemcode,
//           memo,
//           rate: store.rates[user.currency],
//           currency: user.currency,
//           confirmed,
//           network,
//           received: true,
//           fee,
//           with_id: source.user_id
//         },
//         { transaction }
//       );
//
//       await account.increment({ balance: amount }, { transaction });
//       await account.reload({ transaction });
//
//       payment = payment.get({ plain: true });
//       payment.account = account.get({ plain: true });
//       emit(user.username, "payment", payment);
//       emit(user.username, "account", account);
//
//       res.send({ payment });
//     });
//   } catch (e) {
//     delete redeeming[redeemcode];
//     console.log(e);
//     err("problem redeeming", e.message);
//     return res.code(500).send("There was a problem redeeming the voucher");
//   }
// });
//
// app.post("/checkRedeemCode", auth, async function(req, res) {
//   const { redeemcode } = req.body;
//
//   const payment = await db.Payment.findOne({ where: { redeemcode } });
//   res.send(payment);
// });
//
// app.get("/payments/:hash", auth, async function(req, res) {
//   try {
//     let payment = await db.Payment.findOne({
//       where: { user_id: req.user.id, hash: req.params.hash }
//     });
//
//     return payment.get({ plain: true });
//   } catch (e) {
//     console.log(e);
//   }
// });