aboutsummaryrefslogtreecommitdiffstats
path: root/routes/shopify.ts
blob: abd7c81b9fb1acdf1ec70959d321d3d0a6a264e1 (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
import got from "got";
import { g } from "$lib/db";
import { l, err } from "$lib/logging";
import { getPayment } from "$lib/utils";

const query = `mutation orderMarkAsPaid($input: OrderMarkAsPaidInput!) {
  orderMarkAsPaid(input: $input) {
    order { id }
    userErrors {
      field
      message
    }
  }
}`;

export default async (c) => {
  const body = await c.req.json();
  const { hash } = body;
  const id = c.req.param("id");
  const p = await getPayment(hash);
  const user = await g(`user:${p.uid}`);

  l("marking shopify order paid", id, user.username, p.id, user.shopifyStore);

  try {
    const r = await got
      .post(`https://${user.shopifyStore}.myshopify.com/admin/api/2023-07/graphql.json`, {
        headers: {
          "Content-Type": "application/json",
          "X-Shopify-Access-Token": user.shopifyToken,
        },
        json: {
          query,
          variables: { input: { id: `gid://shopify/Order/${id}` } },
        },
      })
      .json();

    l("shopify success", r);

    return c.json(r);
  } catch (e) {
    err("problem marking shopify order as paid", e.message);
  }
};