diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/payments.ts | 4 | ||||
| -rw-r--r-- | lib/square.ts | 55 |
2 files changed, 59 insertions, 0 deletions
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}`, + }); +}; |
