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 /lib/square.ts | |
| parent | f57530bde82864d02386c1bbefc950bfff8711d3 (diff) | |
| parent | b2f88134a3828d2df7271c7d0ae4219e5e6f7d56 (diff) | |
| download | coinos-server-440991e905586242e5b9b1bb53b4dbdb36f5f3fb.tar.gz coinos-server-440991e905586242e5b9b1bb53b4dbdb36f5f3fb.zip | |
Merge branch 'square'
Diffstat (limited to 'lib/square.ts')
| -rw-r--r-- | lib/square.ts | 55 |
1 files changed, 55 insertions, 0 deletions
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}`, + }); +}; |
