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

# Preview Payment

> Preview payment breakdown before creating payment intent

# Preview Payment

Returns a detailed breakdown of payment charges without creating a payment intent. Use this to show customers the final amount including fees before they confirm payment.

**Requires `cart_unique_id` from `/cart/validate`.**

## Request

<ParamField header="X-API-Key" type="string" required>
  Your API key
</ParamField>

### Body Parameters

<ParamField body="product_id" type="integer" required>
  Primary product ID in the cart
</ParamField>

<ParamField body="cart_unique_id" type="string" required>
  Cart ID from `/cart/validate` response
</ParamField>

<ParamField body="email" type="string" required>
  Customer's email address
</ParamField>

<ParamField body="first_name" type="string" required>
  Customer's first name
</ParamField>

<ParamField body="last_name" type="string" required>
  Customer's last name
</ParamField>

<ParamField body="currency" type="string" required>
  Currency code (must match cart validation)
</ParamField>

## Response

<ResponseExample>
  ```json theme={null}
  {
    "status": true,
    "message": "Payment preview calculated",
    "data": {
      "subtotal": 5998,
      "discount": 600,
      "shipping": 599,
      "tax": 432,
      "transaction_fee": 175,
      "total": 6004,
      "currency": "USD",
      "fee_breakdown": {
        "base_fee": 175,
        "customer_pays_fee": true
      }
    }
  }
  ```
</ResponseExample>

## Use Case

Call this endpoint after `/cart/validate` to show customers a complete breakdown before they commit to payment:

```javascript theme={null}
// 1. Validate cart first
const cartResponse = await fetch('/api/v1/cart/validate', {
  method: 'POST',
  headers: {
    'X-API-Key': apiKey,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    cart: cartItems,
    currency: 'USD',
    delivery_details: shippingAddress
  })
});
const { cart_unique_id, total } = await cartResponse.json();

// 2. Preview payment breakdown
const previewResponse = await fetch('/api/v1/cart-payment/preview', {
  method: 'POST',
  headers: {
    'X-API-Key': apiKey,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    product_id: primaryProductId,
    cart_unique_id,
    email: 'john@example.com',
    first_name: 'John',
    last_name: 'Doe',
    currency: 'USD'
  })
});

// 3. Show breakdown to customer before final payment
const preview = await previewResponse.json();
displayPaymentSummary(preview.data);
```

## Errors

| Error Code          | Description                 |
| ------------------- | --------------------------- |
| `CART_EXPIRED`      | Cart ID is no longer valid  |
| `CART_NOT_FOUND`    | Invalid cart ID             |
| `PRODUCT_NOT_FOUND` | Product no longer available |

## Example

```bash theme={null}
curl -X POST "https://api.khaime.com/api/v1/cart-payment/preview" \
  -H "X-API-Key: pk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": 12345,
    "cart_unique_id": "bc59bc1f-e1b0-4ee5-a9cb-b966042ce38f",
    "email": "john@example.com",
    "first_name": "John",
    "last_name": "Doe",
    "currency": "USD"
  }'
```
