> ## 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.

# Pay Button Quickstart

> One-click product checkout in three lines.

# Pay Button

The fastest way to sell a single product. Drop `<khaime-pay-button>` onto your page, point it at a product ID, and the SDK handles everything — creating a checkout session, opening the payment modal, and reporting the result.

Use this when you have a **single product per button** (a digital download, a subscription plan, an event ticket, a course). For multi-item baskets, see [Storefront + Cart](https://docs.khaime.com/sdks/embedded/storefront-cart.md).

## 1. Load the SDK

```html theme={null}
<script src="https://api.khaime.com/sdk/v2/khaime.js"></script>
```

## 2. Initialize

```javascript theme={null}
const sdk = KhaimeSDK.init({
  merchantId: 'YOUR_MERCHANT_ID',
  apiKey: 'pub_live_...', // your storefront key: safe in browser code
  sandbox: true, // omit or set to false in production
});
```

## 3. Render the button

> [tab] HTML attribute
>
> ```html theme={null}
> <khaime-pay-button
>   merchant-id="YOUR_MERCHANT_ID"
>   product-id="456"
>   label="Buy now"
>   variant="primary"
> ></khaime-pay-button>
> ```

> [tab] Programmatic
>
> ```html theme={null}
> <div id="pay"></div>
> <script>
>   sdk.payButton('#pay', {
>     productId: 456,
>     label: 'Buy now',
>     variant: 'primary', // 'primary' | 'secondary' | 'outline'
>   });
> </script>
> ```

## 4. Handle the result

```javascript theme={null}
sdk.on({
  onCheckoutInit: (sessionToken) => {
    console.log('Session created:', sessionToken);
  },
  onPaymentSuccess: (result) => {
    // result: { sessionToken, orderId, amount, currency, paymentMethod }
    console.log('Order placed:', result.orderId);
    window.location.href = '/thank-you?order=' + result.orderId;
  },
  onPaymentError: (err) => {
    console.error('Payment failed:', err.error);
  },
});
```

> [!WARNING]
>
> `onPaymentSuccess` means the customer completed the payment UI. Always confirm the order server-side via a [webhook](https://docs.khaime.com/webhooks/overview.md) before fulfilling.

## What happens under the hood

**1. Click**

The customer clicks the button. The SDK calls `POST /api/v1/sdk/checkout/initialize` with `{ product_id, quantity, currency? }` and receives a session token.

**2. Modal opens**

The SDK lazy-loads the checkout modal and fetches `GET /api/v1/sdk/checkout/session/{sessionToken}` to get the payment gateway credentials.

**3. Payment**

The modal renders embedded payment elements (USD, EUR, GBP, etc.) or redirect flows (NGN, GHS, ZAR) based on the transaction currency.

**4. Confirm**

On submit, the SDK calls `POST /api/v1/sdk/checkout/session/{sessionToken}/confirm` and emits `khaime:payment-success` with the order ID.

## Full example

```html theme={null}
<!DOCTYPE html>
<html>
<body>
  <h1>Premium Plan — $50/mo</h1>

  <khaime-pay-button
    merchant-id="1234"
    product-id="456"
    label="Subscribe"
  ></khaime-pay-button>

  <script src="https://api.khaime.com/sdk/v2/khaime.js"></script>
  <script>
    const sdk = KhaimeSDK.init({ merchantId: '1234', apiKey: 'pub_sandbox_...', sandbox: true });

    sdk.on({
      onPaymentSuccess: ({ orderId }) => {
        window.location.href = '/welcome?order=' + orderId;
      },
    });
  </script>
</body>
</html>
```

## Attributes reference

| Attribute | Type | Required | Description |
| - | - | - | - |
| `merchant-id` | string / number | yes | Your Khaime merchant ID. |
| `product-id` | string / number | yes | The product the customer is buying. |
| `label` | string | no | Button text. Defaults to the product's configured CTA. |
| `variant` | `primary` \| `secondary` \| `outline` | no | Visual style. Defaults to `primary`. |
| `sandbox` | boolean attribute | no | Forces sandbox mode on this component. |

## Next steps

* Listen for richer events — see the [events reference](https://docs.khaime.com/sdks/embedded/events.md).
* Need a cart with multiple items? Move to [Storefront + Cart](https://docs.khaime.com/sdks/embedded/storefront-cart.md).
* Creating sessions from your backend (e.g. WordPress)? See [Programmatic checkout](https://docs.khaime.com/sdks/embedded/programmatic.md).


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