aboutsummaryrefslogtreecommitdiffstats
path: root/lib/square.ts
blob: 3fbc87743ac0de42fdcc0d5f145342c82cead02c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { g, s } from "$lib/db";
import config from "$config";
import { SquareClient } from "square";
import { v4 } from "uuid";

const { appId, environment } = config.square;

export const squarePayment = async (user) => {
  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);
};