diff options
| author | John Barratt <[email protected]> | 2023-02-28 13:55:59 -0800 |
|---|---|---|
| committer | John Barratt <[email protected]> | 2023-02-28 13:55:59 -0800 |
| commit | e47d50a443f0a396321716a506e1df325dd4a1fb (patch) | |
| tree | 1c010c5970e9cbfbf8671e6f00a97d6518446fdd /lib/payments.js | |
| parent | 58e996a4ac227162f171e84530fe898cb504e045 (diff) | |
| parent | e4702b4d13e4d966d735c2ba4657e10d7c436a57 (diff) | |
| download | coinos-server-e47d50a443f0a396321716a506e1df325dd4a1fb.tar.gz coinos-server-e47d50a443f0a396321716a506e1df325dd4a1fb.zip | |
Merge branch 'master' of https://github.com/coinos/coinos-server into notifications
Diffstat (limited to 'lib/payments.js')
| -rw-r--r-- | lib/payments.js | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/lib/payments.js b/lib/payments.js index 2c0eb0c0..aaf39210 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"; @@ -13,7 +13,7 @@ export const types = { bitcoin: "bitcoin", lightning: "lightning", pot: "pot", - classic: "classic", + classic: "classic" }; export let debit = async ( @@ -25,9 +25,11 @@ export let debit = async ( type = types.internal, id = v4() ) => { - let ref; + let ref, tip; let invoice = await g(`invoice:${hash}`); - if (invoice) ref = invoice.uid; + if (invoice) { + ref = invoice.uid; + } let { id: uid, currency, username } = user; @@ -44,16 +46,21 @@ export let debit = async ( let covered = Math.min(credit, ourfee) || 0; ourfee -= covered; - l("debiting", user.username, balance, amount, ourfee, amount + ourfee); - if (balance < amount + ourfee) fail("Insufficient funds"); + l("debiting", user.username, balance, amount, fee, ourfee, amount + fee + ourfee); + if (balance < amount + fee + ourfee) fail("Insufficient funds"); await db .multi() .decrBy(`credit:${type}:${uid}`, covered) - .decrBy(`balance:${uid}`, amount + ourfee) + .decrBy(`balance:${uid}`, amount + fee + ourfee) .exec(); }); }); + if (invoice?.tip) { + tip = invoice.tip; + amount -= tip; + } + let p = { id, amount: -amount, @@ -66,7 +73,8 @@ export let debit = async ( currency, type, ref, - created: Date.now(), + tip, + created: Date.now() }; await s(`payment:${id}`, p); @@ -81,7 +89,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", hash, ", invoice not found"); if (!memo) ({ memo } = invoice); let user = await g(`user:${invoice.uid}`); @@ -98,7 +106,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { ref, type, confirmed: true, - created: Date.now(), + created: Date.now() }; let { tip } = invoice; |
