# Create a checkout

Generate a hosted payment page for an order.

POST`/checkout/create.php`

Terminal
```
curl "https://circus-pay.com/api/checkout/create.php" \
  -H "Authorization: Bearer $CIRCUSPAY_SECRET_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "amount": 2499,
  "currency": "EUR",
  "productName": "Ma commande",
  "orderId": "ORDER-DEMO-001",
  "successUrl": "https://example.com/payment/return",
  "cancelUrl": "https://example.com/payment/cancel"
}'
```

JavaScript
```
const response = await fetch("https://circus-pay.com/api/checkout/create.php", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.CIRCUSPAY_SECRET_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
  "amount": 2499,
  "currency": "EUR",
  "productName": "Ma commande",
  "orderId": "ORDER-DEMO-001",
  "successUrl": "https://example.com/payment/return",
  "cancelUrl": "https://example.com/payment/cancel"
})
});
const result = await response.json();
if (!response.ok || !result.success) {
  throw new Error(result.error || "API request failed");
}
console.log(result);
```

PHP
```
<?php
$ch = curl_init("https://circus-pay.com/api/checkout/create.php");
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 20,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer " . getenv("CIRCUSPAY_SECRET_KEY"),
    "Content-Type: application/json",
  ],
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => '{
  "amount": 2499,
  "currency": "EUR",
  "productName": "Ma commande",
  "orderId": "ORDER-DEMO-001",
  "successUrl": "https://example.com/payment/return",
  "cancelUrl": "https://example.com/payment/cancel"
}',
]);
$raw = curl_exec($ch);
if ($raw === false) { throw new RuntimeException(curl_error($ch)); }
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
if ($status >= 400 || empty($result["success"])) {
  throw new RuntimeException($result["error"] ?? "API request failed");
}
```

Python
```
import os, requests

response = requests.post(
    "https://circus-pay.com/api/checkout/create.php",
    headers={"Authorization": "Bearer " + os.environ["CIRCUSPAY_SECRET_KEY"]},
    json={'amount': 2499, 'currency': 'EUR', 'productName': 'Ma commande', 'orderId': 'ORDER-DEMO-001', 'successUrl': 'https://example.com/payment/return', 'cancelUrl': 'https://example.com/payment/cancel'},
    timeout=20,
)
response.raise_for_status()
result = response.json()
if not result.get("success"):
    raise RuntimeError(result.get("error", "API request failed"))
print(result)
```

## Parameters

| Parameter | Description | 
| --- | --- |

| `amount · integer · required` | Positive amount in minor units. Example: `15000` for PLN 150. | 
| `currency · string` | Currency, default `EUR`. Availability depends on the account and configured processing. | 
| `orderId · string` | Your order reference. Reusing it may return the existing link; do not use it for a new order. | 
| `productName · string` | Label shown to the customer. | 
| `productDescription · string` | Text shown under the product name in the order summary. Up to 5,000 characters. | 
| `productImage · string` | Product image, shown as a thumbnail in the order summary. Public `https://` URL (up to 2,048 characters) or a `data:image/…;base64` image (PNG, JPEG, WebP, GIF, AVIF) up to 8 MB. An unusable value is ignored and reported in `warnings`. | 
| `productSku · string` | Your product reference, stored with the link and not shown to the customer. Up to 100 characters. | 
| `customerEmail / customerName` | Payer email and name. Sent on their own, they are suggested in the form and the payer can still edit them. | 
| `billingAddress · object` | Payer billing address: `countryCode` (2-letter ISO country code), `streetLine1`, `streetLine2`, `city`, `region`, `postcode`. | 
| `prefillCustomer · boolean` | `true` stores the email, name and address with the link so the payer goes straight to payment. An invalid value returns HTTP 400. `false` keeps the contact form. Rejected on a `reusable` link. | 
| `paymentMethods · string[]` | Restricts offered methods. Does not enable a method unavailable to your account. | 
| `theme_id · string` | ID of an active theme belonging to your account. See Checkout Studio. | 
| `successUrl / cancelUrl` | Return addresses for your store. Payment confirmation happens server-side. | 
| `testMode · boolean` | Enables the test flow. A test key also forces this mode. | 

## Response

Illustrative excerpt · fictional domain
```
{
  "success": true,
  "token": "pay_EXAMPLE",
  "url": "https://checkout.example.com/checkout/pay_EXAMPLE",
  "orderId": "ORDER-DEMO-001",
  "test": false,
  "mode": "live"
}
```

The response may also include `fallbackUrl`, `routing`, `ghostSite` and `theme_id`. Use `url` as the destination and retain your order reference.

The response also includes `product`, the stored product details, and `warnings` when a value was adjusted: text too long and truncated (`truncated`), image ignored (`image_ignored`) or image served over `http://` (`image_not_https`). The link is created in every case.

Direct payment: when the request contains a valid `customerEmail`, a `customerName` of at least 2 characters and a complete `billingAddress` (`countryCode`, `streetLine1`, `city`, `postcode`), the checkout skips the contact step and opens payment directly, even without `prefillCustomer`. The response then shows `customerPrefill: "auto"`, `"explicit"` with `prefillCustomer: true`, and `null` otherwise. Incomplete or invalid details never cause an error: the usual form is shown. This mode does not apply to `reusable` links.

## Retry without duplicating

For a non-reusable link and an existing reference, the API may return `reused: true` and `alreadyPaid`. Check these fields and the status before restarting payment. This is not a general idempotency guarantee across all endpoints.
