diff options
| author | Adam Soltys <asoltys@gmail.com> | 2024-10-01 15:50:46 -0700 |
|---|---|---|
| committer | Adam Soltys <asoltys@gmail.com> | 2024-10-01 15:50:46 -0700 |
| commit | 80470a2c66c17e4992f176438025b2b021581d11 (patch) | |
| tree | 0f541e280fcaa86f52bc6578af4ce62901ba2518 /routes/shopify.ts | |
| parent | 426127506daa5f5e4c7ec851361bb54d4f769400 (diff) | |
| download | coinos-server-80470a2c66c17e4992f176438025b2b021581d11.tar.gz coinos-server-80470a2c66c17e4992f176438025b2b021581d11.zip | |
getpayment helper
Diffstat (limited to 'routes/shopify.ts')
| -rw-r--r-- | routes/shopify.ts | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/routes/shopify.ts b/routes/shopify.ts index b28f556f..f5c26208 100644 --- a/routes/shopify.ts +++ b/routes/shopify.ts @@ -1,6 +1,7 @@ import got from "got"; import { g } from "$lib/db"; import { err } from "$lib/logging"; +import { getPayment } from "$lib/utils"; let query = `mutation orderMarkAsPaid($input: OrderMarkAsPaidInput!) { orderMarkAsPaid(input: $input) { @@ -17, import { db, g, s } from "$lib/db";
import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { handleZap } from "$lib/nostr";
import { credit } from "$lib/payments";
import { PaymentType } from "$lib/types";
import { getInvoice, getPayment, getUser } from "$lib/utils";
export async function listenForLightning() {
const inv = await ln.waitanyinvoice((await g("pay_index")) || 0);
const {
local_offer_id,
bolt11,
bolt12,
description,
pay_index,
payment_hash,
amount_received_msat,
payment_preimage: preimage,
} = inv;
await s("pay_index", pay_index);
setTimeout(listenForLightning);
const received = Math.round(amount_received_msat / 1000);
try {
if (!preimage) return;
const invoice = await getInvoice(bolt11 ?? local_offer_id ?? bolt12);
if (!invoice) return warn("received lightning with no invoice", bolt11);
const p = await getPayment(bolt11 || bolt12);
if (p) return warn("already processed", bolt11 || bolt12);
if (invoice?.memo) {
try {
if (JSON.parse(description).kind === 9734) {
const { pubkey } = await getUser(invoice.uid);
handleZap(inv, pubkey);
}
} catch (e) {
if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
await credit({
hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
type: bolt12 ? PaymentType.bolt12 : PaymentType.lightning,
payment_hash,
});
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
export async function replay(index) {
const inv = await ln.waitanyinvoice(index - 1);
const {
local_offer_id,
bolt11,
bolt12,
description,
amount_received_msat,
payment_preimage: preimage,
} = inv;
const received = Math.round(amount_received_msat / 1000);
try {
if (!preimage) return;
const invoice = await getInvoice(bolt11 ?? local_offer_id ?? bolt12);
if (!invoice) return warn("received lightning with no invoice", bolt11);
let p = await getPayment(bolt11 || bolt12);
if (p) return warn("already processed", bolt11 || bolt12);
if (invoice?.memo) {
try {
if (JSON.parse(description).kind === 9734) {
const { pubkey } = await getUser(invoice.uid);
handleZap(inv, pubkey);
}
} catch (e) {
if (!e.message.includes("Unexpected"))
warn("failed to handle zap", e.message);
}
}
p = await credit({
hash: bolt11 || bolt12,
amount: received,
memo: invoice.memo,
ref: preimage,
type: bolt12 ? PaymentType.bolt12 : PaymentType.lightning,
});
return p;
} catch (e) {
console.log(e);
err("problem receiving lightning payment", e.message);
}
}
export const fixBolt12 = async (_, res) => {
for await (const k of db.scanIterator({ MATCH: "payment:*" })) {
const p = await g(k);
if (p.type === "bolt12") {
console.log(k);
const { invoices } = await ln.listinvoices({ invstring: p.hash });
const { local_offer_id } = invoices[0];
const oid = await g(`payment:${local_offer_id}`);
const op = await g(`payment:${oid}`);
if (op) {
db.del(`payment:${oid}`);
db.del(`payment:${local_offer_id}`);
db.decrBy(`balance:${op.uid}`, op.amount);
}
}
}
res.send({});
};
|
