1 | |
2 |
|
3 | |
4 | * Send Order Invoice |
5 | * Email an invoice with a payment link for an existing order's outstanding balance (e.g. an order created with financial status PENDING). Leave to empty to use the order's email; subject and custom_message override the store's invoice template. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | order_id: string, |
10 | to: string | undefined, |
11 | subject: string | undefined, |
12 | custom_message: string | undefined, |
13 | bcc: string[] | undefined |
14 | ) { |
15 | const id = order_id.startsWith("gid://") |
16 | ? order_id |
17 | : `gid://shopify/Order/${order_id}` |
18 | const response = await fetch( |
19 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
20 | { |
21 | method: "POST", |
22 | headers: { |
23 | "X-Shopify-Access-Token": auth.token, |
24 | "Content-Type": "application/json", |
25 | Accept: "application/json", |
26 | }, |
27 | body: JSON.stringify({ |
28 | query: `mutation ($id: ID!, $email: EmailInput) { orderInvoiceSend(id: $id, email: $email) { order { id legacyResourceId name email displayFinancialStatus totalOutstandingSet { shopMoney { amount currencyCode } } } userErrors { field message } } }`, |
29 | variables: { |
30 | id, |
31 | email: { |
32 | to: to || undefined, |
33 | subject: subject || undefined, |
34 | customMessage: custom_message || undefined, |
35 | bcc: bcc?.length ? bcc : undefined, |
36 | }, |
37 | }, |
38 | }), |
39 | } |
40 | ) |
41 |
|
42 | if (!response.ok) { |
43 | throw new Error(`${response.status} ${await response.text()}`) |
44 | } |
45 |
|
46 | const { data, errors } = await response.json() |
47 | if (errors) { |
48 | throw new Error(JSON.stringify(errors)) |
49 | } |
50 |
|
51 | if (data.orderInvoiceSend.userErrors.length) { |
52 | throw new Error(JSON.stringify(data.orderInvoiceSend.userErrors)) |
53 | } |
54 |
|
55 | return data.orderInvoiceSend |
56 | } |
57 |
|