Creates an application charge
One script reply has been approved by the moderators Verified

Creates an application charge

Created by hugo697 883 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates an application charge
7
 * Creates an application charge
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    application_charge?: {
14
      name?: string;
15
      price?: number;
16
      return_url?: string;
17
      test?: boolean;
18
      [k: string]: unknown;
19
    };
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/application_charges.json`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      "X-Shopify-Access-Token": auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41