> ## Documentation Index
> Fetch the complete documentation index at: https://docs.waffo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Give Customers Their Invoices From Your Own Site

> Fetch a shareable invoice link for any successful payment with your API Key and show it on your own order page

## What You'll Build

An **Invoice** button on your own order history page. Your server looks up the payment through GraphQL, reads its `invoiceToken`, and builds a link. When the customer clicks it, the invoice opens in the browser, already authorized, with a **Download PDF** button. No sign-in, no email verification.

```mermaid theme={"system"}
sequenceDiagram
    participant C as Customer Browser
    participant S as Your Server
    participant W as Waffo Pancake API
    participant I as Invoice Page

    C->>S: Open order history
    S->>W: GraphQL payments { id invoiceToken } (API Key)
    W->>S: PAY_xxx + invoiceToken
    S->>C: Render link /invoice/PAY_xxx?token=...
    C->>I: Click "Invoice"
    I->>C: Invoice with Download PDF
```

> [!NOTE]
>
> There is no endpoint that returns the PDF file itself. The PDF is generated in the customer's browser on the invoice page. If you need the file on your server, open the link in a headless browser, or point your customers to the page.

***

## Prerequisites

* An API Key (Dashboard → API & Development). Use a test key while you build
* The Waffo Payment ID (`PAY_xxx`) of the payment, or a way to find it: your own business number ([`orderMerchantExternalId`](https://docs.waffo.ai/api-reference/endpoints/graphql/orders-and-payments#look-up-payment-by-your-business-number-ordermerchantexternalid)), the buyer's email, or the order ID
* The payment must be **succeeded**. Pending and failed payments have no invoice

***

## Step 1: Read the `invoiceToken`

Query `payments` (or `payment` by ID) and include `invoiceToken`. The field is visible to the merchant role only, so API Key and Dashboard sessions can read it; customer portal sessions cannot.

```typescript TypeScript (SDK) theme={"system"}
import { WaffoPancake } from "@waffo/pancake-ts";

const client = new WaffoPancake({
  merchantId: process.env.WAFFO_MERCHANT_ID!,
  privateKey: process.env.WAFFO_PRIVATE_KEY!,
  environment: "test",
});

interface PaymentQuery {
  payment: { id: string; status: string; invoiceToken: string | null };
}

const { payment } = await client.graphql.query<PaymentQuery>({
  query: `query($id: String!) {
    payment(id: $id) { id status invoiceToken }
  }`,
  variables: { id: "PAY_6eYCunG3IMmIgcQOnaXdoA" },
});
```

```graphql GraphQL theme={"system"}
query($id: String!) {
  payment(id: $id) {
    id
    status
    invoiceToken
    snapshotAmountDetails { total currency }
  }
}
```

```json Response theme={"system"}
{
  "data": {
    "payment": {
      "id": "PAY_6eYCunG3IMmIgcQOnaXdoA",
      "status": "succeeded",
      "invoiceToken": "0f46a30b08e5c88e0b5795e44290bbd3",
      "snapshotAmountDetails": { "total": "9.00", "currency": "USD" }
    }
  }
}
```

To list every invoice a customer can download, filter by the buyer's email and `status: succeeded`:

```graphql theme={"system"}
query($email: String!) {
  payments(
    filter: { buyerEmail: { eq: $email }, status: { eq: "succeeded" } }
    orderBy: created_at_desc
    limit: 50
  ) {
    id
    createdAt
    invoiceToken
    snapshotAmountDetails { total currency }
  }
}
```

| Field | Meaning |
| - | - |
| `invoiceToken` | 32-character hex token. Set once the payment is `succeeded`; `null` before that |
| `status` | Only `succeeded` payments have an invoice |

***

## Step 2: Build the Link

```
https://pancake.waffo.ai/invoice/{paymentId}?token={invoiceToken}
```

```typescript theme={"system"}
function invoiceUrl(paymentId: string, invoiceToken: string | null): string | null {
  if (!invoiceToken) return null; // not succeeded yet
  return `https://pancake.waffo.ai/invoice/${paymentId}?token=${encodeURIComponent(invoiceToken)}`;
}
```

Render it as a link or button on your order page. Anyone holding the link can open that one invoice and download the PDF; the token does not grant access to anything else.

***

## Step 3: Keep It Current

The token is stable for the life of the payment, so you can store it next to your order record. Two convenient moments to fetch it:

* **On webhook**: `order.completed` and `subscription.payment_succeeded` carry the Payment ID as `eventId`. Query `payment(id:)` for the `invoiceToken` and save it with the order. See [Webhooks](https://docs.waffo.ai/guides/webhooks.md)
* **On demand**: query `payments` by `buyerEmail` or `orderMerchantExternalId` when the customer opens their order history

***

## What the Invoice Shows

| Section | Content |
| - | - |
| Store | Name, logo, support email |
| Payment | Date, status, amount and tax breakdown, payment method (brand and last four digits) |
| Order | Product name, one-time or subscription, billing period |
| Refunds | Any refunds issued against the payment |
| Download | **Download PDF** button, in the page language |

Customers can also edit their invoice details (company name, address, tax ID) before downloading when they open the same invoice from the [customer portal](https://docs.waffo.ai/customers/billing.md#invoices).

***

## Alternatives

| Need | Use |
| - | - |
| Let customers browse all orders, invoices, and subscriptions in one place | [Customer Portal Sign-In](https://docs.waffo.ai/guides/customer-portal-sso.md) |
| Email the invoice automatically after payment | Already done: the receipt email Waffo Pancake sends to the buyer contains this same link |
| Reconcile payments in your own system | [GraphQL: Orders and Payments](https://docs.waffo.ai/api-reference/endpoints/graphql/orders-and-payments.md) |

***

## FAQ

> [accordion] Why is invoiceToken null?
>
> The payment is not `succeeded` yet. Pending and failed payments never get a token. Check `status` first. A `0.00` card-verification payment that starts a free trial is `succeeded` and does get one.

> [accordion] Can I fetch the PDF from my server?
>
> Not through the API. The PDF is produced in the browser on the invoice page. Use the link as-is, or render the page in a headless browser if you must archive a copy.

> [accordion] Does the token expire or rotate?
>
> No. It stays valid for the life of the payment, and refunds do not change it. The invoice page shows refunds that happen later.

> [accordion] Does this work in test mode?
>
> Yes. Test payments get an `invoiceToken` too, and the link opens the invoice with a test-mode banner.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.