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

# Collect with Java

> Collect a payment via invoice (orders use the same flow) using the Dollr API with Java HttpClient.

<Note>
  **Invoices vs orders:** These guides walk through **invoices** (formal billing, line items, publish). The collect flow is the same for **orders** — use `/v1/orders/*` instead of `/v1/invoices/*` and set `source_type: "ORDER"` on the checkout session. See [Orders](/api/orders) and [Parties & counterparties](/concepts/parties-and-counterparties).

  **Hosted checkout:** To skip party, invoice, session, and execute steps entirely, see [Hosted checkout](/guides/hosted-checkout).
</Note>

End-to-end **invoice collection** with Java 11+ `java.net.http.HttpClient` and Jackson (or Gson) for JSON.

<Note>
  **API Reference:** [Obtain token](/api-reference/jwt/client-obtain-token) · [Collect](/api-reference/executions/collect)
</Note>

## Prerequisites

* Java 11+
* Jackson `ObjectMapper` (or equivalent) on the classpath
* Store credentials in environment variables or a secrets manager

## Steps

<Steps>
  <Step title="HTTP client and token">
    ```java theme={null}
    import java.net.URI;
    import java.net.http.*;
    import java.nio.charset.StandardCharsets;

    String baseUrl = "https://api.heydollr.app";
    HttpClient client = HttpClient.newHttpClient();

    String authBody = """
        {"client_id":"%s","client_secret":"%s"}
        """.formatted(System.getenv("DOLLR_CLIENT_ID"), System.getenv("DOLLR_CLIENT_SECRET"));

    HttpRequest tokenReq = HttpRequest.newBuilder()
        .uri(URI.create(baseUrl + "/v1/jwt/client/obtain/token"))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(authBody))
        .build();

    String tokenJson = client.send(tokenReq, HttpResponse.BodyHandlers.ofString()).body();
    // Parse access_token from JSON with your mapper
    ```
  </Step>

  <Step title="Authenticated POST helper">
    ```java theme={null}
    HttpRequest.Builder authed(String path, String bearer) {
        return HttpRequest.newBuilder()
            .uri(URI.create(baseUrl + path))
            .header("Authorization", "Bearer " + bearer)
            .header("Content-Type", "application/json");
    }

    // POST /v1/parties/create, /v1/counterparties/create, /v1/invoices/create
    // Store party id, counterparty id, invoice id from responses
    ```
  </Step>

  <Step title="Publish and execute">
    ```java theme={null}
    // PUT /v1/invoices/publish/{invoiceId}
    // POST /v1/sessions/checkout  body: {"source_id":101,"source_type":"INVOICE"}
    // POST /v1/payment-accounts/create?operation_type=COLLECTION

    String referenceId = java.util.UUID.randomUUID().toString();

    String executeBody = """
        {"session_id":"%s","payment_account_id":"%s","currency":"USD","reference_id":"%s"}
        """.formatted(sessionId, paymentAccountId, referenceId);

    HttpRequest execute = authed("/v1/executions/collection", bearer)
        .POST(HttpRequest.BodyPublishers.ofString(executeBody))
        .build();
    ```

    Persist `referenceId` **before** `client.send(execute, ...)`.
  </Step>

  <Step title="Poll status">
    ```java theme={null}
    HttpRequest status = HttpRequest.newBuilder()
        .uri(URI.create(baseUrl + "/v1/status/collection/" + referenceId))
        .header("Authorization", "Bearer " + bearer)
        .GET()
        .build();
    ```
  </Step>
</Steps>

## Try it yourself

<CardGroup cols={3}>
  <Card title="Hosted checkout" icon="window-maximize" href="/guides/hosted-checkout">
    Mobile money and card on a Dollr-hosted page — fastest path.
  </Card>

  <Card title="Orders" icon="box" href="/api/orders">
    Same collect flow with `source_type: ORDER` and `/v1/orders/*`.
  </Card>

  <Card title="Direct checkout" icon="cart-shopping" href="/guides/collect-via-checkout">
    One API call to create source — API-embedded flow.
  </Card>
</CardGroup>

## Related

* [Hosted checkout](/guides/hosted-checkout) · [Orders](/api/orders)
* [Collect via checkout](/guides/collect-via-checkout)
* [Choose your integration](/guides/choose-integration)
* [Error handling](/guides/error-handling)
* [Sessions & executions](/concepts/sessions-and-executions)
