Edits history of script submission #23780 for ' Send Order Invoice (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Send Order Invoice
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      order_id: string,
      to: string | undefined,
      subject: string | undefined,
      custom_message: string | undefined,
      bcc: string[] | undefined
    ) {
      const id = order_id.startsWith("gid://")
        ? order_id
        : `gid://shopify/Order/${order_id}`
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            query: `mutation ($id: ID!, $email: EmailInput) { orderInvoiceSend(id: $id, email: $email) { order { id legacyResourceId name email displayFinancialStatus totalOutstandingSet { shopMoney { amount currencyCode } } } userErrors { field message } } }`,
            variables: {
              id,
              email: {
                to: to || undefined,
                subject: subject || undefined,
                customMessage: custom_message || undefined,
                bcc: bcc?.length ? bcc : undefined,
              },
            },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      if (data.orderInvoiceSend.userErrors.length) {
        throw new Error(JSON.stringify(data.orderInvoiceSend.userErrors))
      }
    
      return data.orderInvoiceSend
    }
    

    Submitted by hugo989 3 hours ago