1 | type Shopify = { |
2 | token: string; |
3 | store_name: string; |
4 | }; |
5 | |
6 | * Creates a usage charge |
7 | * Creates a usage charge |
8 | */ |
9 | export async function main( |
10 | auth: Shopify, |
11 | api_version: string = "2023-10", |
12 | recurring_application_charge_id: string, |
13 | body: { |
14 | usage_charge?: { |
15 | description?: string; |
16 | price?: number; |
17 | [k: string]: unknown; |
18 | }; |
19 | [k: string]: unknown; |
20 | } |
21 | ) { |
22 | const url = new URL( |
23 | `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/recurring_application_charges/${recurring_application_charge_id}/usage_charges.json` |
24 | ); |
25 |
|
26 | const response = await fetch(url, { |
27 | method: "POST", |
28 | headers: { |
29 | "Content-Type": "application/json", |
30 | "X-Shopify-Access-Token": auth.token, |
31 | }, |
32 | body: JSON.stringify(body), |
33 | }); |
34 | if (!response.ok) { |
35 | const text = await response.text(); |
36 | throw new Error(`${response.status} ${text}`); |
37 | } |
38 | return await response.json(); |
39 | } |
40 |
|