diff options
| author | Adam Soltys <[email protected]> | 2025-09-08 09:26:51 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-09-08 09:26:51 -0700 |
| commit | 440991e905586242e5b9b1bb53b4dbdb36f5f3fb (patch) | |
| tree | 90aae1ccbb81a8dacc92a58c220a21310f9f0bed | |
| parent | f57530bde82864d02386c1bbefc950bfff8711d3 (diff) | |
| parent | b2f88134a3828d2df7271c7d0ae4219e5e6f7d56 (diff) | |
| download | coinos-server-440991e905586242e5b9b1bb53b4dbdb36f5f3fb.tar.gz coinos-server-440991e905586242e5b9b1bb53b4dbdb36f5f3fb.zip | |
Merge branch 'square'
| -rw-r--r-- | index.ts | 1 | ||||
| -rw-r--r-- | lib/payments.ts | 4 | ||||
| -rw-r--r-- | lib/square.ts | 55 | ||||
| -rw-r--r-- | routes/square.ts | 59 |
4 files changed, 118 insertions, 1 deletions
@@ -86,6 +86,7 @@ app.post("/replace", auth, payments.replace); app.get("/decode/:bolt11", payments.decode); app.post("/fetchinvoice", payments.fetchinvoice); +app.get("/square/sync", auth, square.sync); app.get("/square/connect", auth, square.connect); app.get("/square/auth", auth, square.auth); app.post("/square/payment", square.payment); diff --git a/lib/payments.ts b/lib/payments.ts index 655786fd..be801622 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -6,6 +6,7 @@ import ln from "$lib/ln"; import { err, l, warn } from "$lib/logging"; import { handleZap } from "$lib/nostr"; import { notify, nwcNotify } from "$lib/notifications"; +import { squarePayment } from "$lib/square"; import { SATS, btc, @@ -320,6 +321,9 @@ export const completePayment = async (inv, p, user) => { nwcNotify(p); notify(p, user, withdrawal); + + squarePayment(p, user); + l(username, "received", p.type, p.amount); callWebhook(inv, p); }; diff --git a/lib/square.ts b/lib/square.ts new file mode 100644 index 00000000..f491effe --- /dev/null +++ b/lib/square.ts @@ -0,0 +1,55 @@ +import config from "$config"; +import { g, s } from "$lib/db"; +import { fiat } from "$lib/utils"; +import { SquareClient } from "square"; +import { v4 } from "uuid"; + +const { appId, environment } = config.square; + +export const squarePayment = async (p, user) => { + let square = await g(`${user.id}:square`); + if (!square) return; + + let client = new SquareClient({ + environment, + }); + + const { refreshToken } = square; + + square = await client.oAuth.obtainToken({ + clientId: appId, + grantType: "refresh_token", + refreshToken, + }); + + await s(`${user.id}:square`, square); + + const { accessToken } = square; + + client = new SquareClient({ + token: accessToken, + environment, + }); + + const locs = await client.locations.list({}); + + await client.payments.create({ + sourceId: "EXTERNAL", + idempotencyKey: v4(), + amountMoney: { + amount: BigInt(parseFloat(fiat(p.amount, p.rate).toFixed(2)) * 100), + currency: p.currency, + }, + tipMoney: p.tip + ? { + amount: BigInt(parseFloat(fiat(p.tip, p.rate).toFixed(2)) * 100), + currency: p.currency, + } + : undefined, + autocomplete: true, + externalDetails: { type: "OTHER", source: `Coinos payment ${p.id}` }, + locationId: locs.locations[0].id, + referenceId: v4(), + note: `Coinos payment ${p.id}`, + }); +}; diff --git a/routes/square.ts b/routes/square.ts index fd8c3abc..eaffff1e 100644 --- a/routes/square.ts +++ b/routes/square.ts @@ -7,8 +7,9 @@ import { sha256 } from "@noble/hashes/sha256"; import { utf8ToBytes } from "@noble/hashes/utils"; import { base64urlnopad as base64 } from "@scure/base"; import { SquareClient } from "square"; +import { v4 } from "uuid"; -const { scopes, url, appId, environment } = config.square; +const { scopes, url, appId, clientSecret, environment } = config.square; export default { async connect(req, res) { @@ -31,6 +32,62 @@ export default { ); }, + async sync(req, res) { + try { + const { user } = req; + let square = await g(`${user.id}:square`); + + let client = new SquareClient({ + environment, + }); + + const { refreshToken } = square; + + square = await client.oAuth.obtainToken({ + clientId: appId, + grantType: "refresh_token", + refreshToken, + }); + + await s(`${user.id}:square`, square); + + const { accessToken } = square; + + client = new SquareClient({ + token: accessToken, + environment, + }); + + // const payments = await authedClient.payments.list({}); + // console.log(payments); + const locs = await client.locations.list({}); + + const pay = await client.payments.create({ + sourceId: "EXTERNAL", + idempotencyKey: v4(), + amountMoney: { + amount: BigInt(1000), + currency: "CAD", + }, + // appFeeMoney: { + // amount: BigInt(10), + // currency: "CAD", + // }, + autocomplete: true, + externalDetails: { type: "OTHER", source: `Coinos ${v4()}` }, + locationId: locs.locations[0].id, + referenceId: v4(), + note: `Brief description ${v4()}`, + }); + + console.log(pay); + + res.send({}); + } catch (e) { + console.log(e); + } + }, + async auth(req, res) { const { user } = req; |
