aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-02-15 22:52:01 -0800
committerAdam Soltys <[email protected]>2023-02-15 22:52:01 -0800
commitb0c778addeeeb72181c7f2f431004c8b9b42aa35 (patch)
treecd5722be914bf20cc3a624c219a16c724d7ba703
parentd80d0b158156f398de4fa3488c9a2079bdca9710 (diff)
downloadcoinos-server-b0c778addeeeb72181c7f2f431004c8b9b42aa35.tar.gz
coinos-server-b0c778addeeeb72181c7f2f431004c8b9b42aa35.zip
buy endpoint
-rw-r--r--lib/payments.js4
-rw-r--r--routes/payments.js90
2 files changed, 59 insertions, 35 deletions
diff --git a/lib/payments.js b/lib/payments.js
index e946e41e..657590ee 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -4,7 +4,7 @@ import { emit } from "$lib/sockets";
import { v4 } from "uuid";
import { notify } from "$lib/notifications";
import { db, g, s, t } from "$lib/db";
-import { l, err } from "$lib/logging";
+import { l, err, warn } from "$lib/logging";
import { fail } from "$lib/utils";
import { callWebhook } from "$lib/webhooks";
@@ -81,7 +81,7 @@ export let debit = async (
export let credit = async (hash, amount, memo, ref, type = types.internal) => {
amount = parseInt(amount);
let invoice = await g(`invoice:${hash}`);
- if (!invoice) return;
+ if (!invoice) return warn("failed to credit", ref, ", invoice not found");
if (!memo) ({ memo } = invoice);
let user = await g(`user:${invoice.uid}`);
diff --git a/routes/payments.js b/routes/payments.js
index 6619c031..a120ec33 100644
--- a/routes/payments.js
+++ b/routes/payments.js
@@ -32,7 +32,7 @@ export default {
await got
.post(`${config.classic}/admin/credit`, {
json: { username, amount, source },
- headers: { authorization: `Bearer ${config.admin}` },
+ headers: { authorization: `Bearer ${config.admin}` }
})
.json();
} else if (payreq) {
@@ -88,7 +88,7 @@ export default {
let payments = (await db.lRange(`${id}:payments`, 0, -1)) || [];
payments = (
await Promise.all(
- payments.map(async (id) => {
+ payments.map(async id => {
let p = await g(`payment:${id}`);
if (p.created < start || p.created > end) return;
if (p.type === types.internal) p.with = await g(`user:${p.ref}`);
@@ -96,7 +96,7 @@ export default {
})
)
)
- .filter((p) => p)
+ .filter(p => p)
.sort((a, b) => b.created - a.created);
let total = payments.length;
@@ -123,7 +123,7 @@ export default {
let twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14));
let decoded = await ln.decodepay(payreq);
let { msatoshi, payee } = decoded;
- let node = nodes.find((n) => n.nodeid === payee);
+ let node = nodes.find(n => n.nodeid === payee);
let alias = node ? node.alias : payee.substr(0, 12);
res.send({ alias, amount: Math.round(msatoshi / 1000) });
@@ -133,13 +133,13 @@ export default {
let amount = await g(`pot:${name}`);
if (!amount) fail("pot not found");
let payments = (await db.lRange(`pot:${name}:payments`, 0, -1)) || [];
- payments = await Promise.all(payments.map((hash) => g(`payment:${hash}`)));
+ payments = await Promise.all(payments.map(hash => g(`payment:${hash}`)));
await Promise.all(
- payments.map(async (p) => (p.user = await g(`user:${p.uid}`)))
+ payments.map(async p => (p.user = await g(`user:${p.uid}`)))
);
- payments = payments.filter((p) => p);
+ payments = payments.filter(p => p);
res.send({ amount, payments });
},
@@ -147,13 +147,16 @@ export default {
amount = parseInt(amount);
await t(`pot:${name}`, async (balance, db) => {
if (balance < amount) fail("Insufficient funds");
- await db.multi().decrBy(`pot:${name}`, amount).exec();
+ await db
+ .multi()
+ .decrBy(`pot:${name}`, amount)
+ .exec();
});
let hash = v4();
await s(`invoice:${hash}`, {
uid: user.id,
- received: 0,
+ received: 0
});
let payment = await credit(hash, amount, "", name, types.pot);
@@ -194,7 +197,7 @@ export default {
let tx = await bc.fundRawTransaction(raw, {
feeRate,
subtractFeeFromOutputs,
- replaceable,
+ replaceable
});
if (config.bitcoin.walletpass)
@@ -233,7 +236,7 @@ export default {
for (let {
scriptPubKey: { address },
- value,
+ value
} of tx.vout) {
total += sats(value);
if (
@@ -254,33 +257,54 @@ export default {
res.send({ txid });
},
- async buy({ body: { amount, number, month, year, cvc } }, res) {
+ async buy({ body: { amount, number, year, month, cvc }, user }, res) {
let stripe = "https://api.stripe.com/v1";
let { STRIPE: username } = process.env;
- let { id: source } = await got.post(`${stripe}/tokens`, {
- form: {
- "card[number]": number,
- "card[exp_month]": month,
- "card[exp_year]": year,
- "card[cvc]": cvc
- },
- username
- });
+ let form = {
+ "card[number]": number,
+ "card[exp_month]": month,
+ "card[exp_year]": year,
+ "card[cvc]": cvc
+ };
- let { status } = await got.post(`${stripe}/charges`, {
- form: {
- amount,
- currency: "cad",
- source,
- description: "starter coupon"
- },
- username
- });
+ let { id: source } = await got
+ .post(`${stripe}/tokens`, {
+ form,
+ username
+ })
+ .json();
+
+ let currency = "CAD";
+ form = {
+ amount,
+ currency,
+ source,
+ description: "starter coupon"
+ };
+
+ let r = await got
+ .post(`${stripe}/charges`, {
+ form,
+ username
+ })
+ .json();
- if (status !== "succeeded")
- throw error(500, { message: "Card payment failed" });
+ let { status } = r;
+ if (status === "succeeded") {
+ let hash = r.id;
+ let memo = "stripe";
+ await s(`invoice:${hash}`, {
+ uid: user.id,
+ received: 0
+ });
+ amount = sats(amount / store.rates[currency]);
+ let uid = await g("user:coinos");
+ let coinos = await g(`user:${uid}`);
+ let p = await debit(hash, amount, 0, memo, coinos);
+ await credit(hash, amount, memo, coinos.id);
+ } else fail("Card payment failed");
- res.send(status);
+ res.send({ status });
}
};