From fdfbb781f40775f39dbaa10269f559695f670b37 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 4 Sep 2025 08:20:11 -0700 Subject: create external payment --- index.ts | 1 + lib/square.ts | 1 - routes/square.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 02a6e0b5..d4748059 100644 --- a/index.ts +++ b/index.ts @@ -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/square.ts b/lib/square.ts index e00f0a44..11710774 100644 --- a/lib/square.ts +++ b/lib/square.ts @@ -153,7 +153,6 @@ async function getOrCacheLocationId(uid: string, client: SquareClient): Promise< return firstActiveId; } -// ----- Public entry: syncSquare ----- export async function syncSquare(inv: CoinosInv, p: CoinosPayment, user: CoinosUser) { // 1) Load/refresh per-user OAuth creds const auth = await ensureFreshAccessToken(user.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; -- cgit v1.2.3