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

# Configure the Okapay PHP SDK in a Laravel Application

> Configure the Okapay PHP SDK in Laravel. Set credentials via .env, register in config/services.php, and use the client in controllers and jobs.

The Okapay PHP SDK integrates cleanly into Laravel applications. Configure credentials in your `.env` file and instantiate the client anywhere in your application using Laravel's config helper. No service provider is required — the SDK works with standard Laravel conventions out of the box.

## Requirements

* PHP 8.1+
* Laravel 10+
* [Composer](https://getcomposer.org/)

## Installation

Install the SDK with Composer:

```bash theme={null}
composer require okapay/okapay-php
```

## Configuration

**1. Add credentials to .env**

Add your Okapay API base URL and secret key to your `.env` file. Retrieve your secret key from the [merchant dashboard](https://dashboard.okapay.net/):

```env theme={null}
OKAPAY_BASE_URL=https://api.okapay.net
OKAPAY_SECRET_KEY=sk_live_xxx
```

Never commit this file to version control.

**2. Register in config/services.php**

Add a `okapay` entry to your `config/services.php` file so the SDK config is accessible via Laravel's config helper:

```php theme={null}
'okapay' => [
    'base_url'   => env('OKAPAY_BASE_URL'),
    'secret_key' => env('OKAPAY_SECRET_KEY'),
],
```

**3. Instantiate the client**

Wherever you need the SDK, create a `Client` instance by passing the registered config:

```php theme={null}
use Okapay\Client;

$okapay = new Client(config('services.okapay'));
```

## Using in a Controller

The example below shows a controller method that creates a Okapay checkout session and redirects the customer to the payment page:

```php theme={null}
<?php

namespace App\Http\Controllers;

use Okapay\Client;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function create(Request $request)
    {
        $okapay         = new Client(config('services.okapay'));
        $merchantId = config('services.okapay.merchant_id');

        $session = $okapay->sessions()->create($merchantId, [
            'amount'      => $request->input('amount'),
            'currency'    => 'USD',
            'description' => 'Order #' . $request->input('order_id'),
            'source'      => 'laravel',
        ]);

        return redirect($session['checkout_url']);
    }
}
```

Add your Merchant ID to your `.env` and `config/services.php` alongside your secret key:

```env theme={null}
OKAPAY_MERCHANT_ID=your_merchant_id
```

```php theme={null}
'okapay' => [
    'base_url'    => env('OKAPAY_BASE_URL'),
    'secret_key'  => env('OKAPAY_SECRET_KEY'),
    'merchant_id' => env('OKAPAY_MERCHANT_ID'),
],
```

## Listing Products

Use the same pattern to call any SDK method. The example below lists your first 20 products:

```php theme={null}
$okapay       = new Client(config('services.okapay'));
$products = $okapay->products()->list(['per_page' => 20]);
```

> [!TIP]
>
> Bind `Okapay\Client` in your `AppServiceProvider` as a singleton to avoid re-instantiating the client on every request:
>
> ```php theme={null}
> $this->app->singleton(\Okapay\Client::class, function () {
>     return new \Okapay\Client(config('services.okapay'));
> });
> ```
>
> You can then resolve it via dependency injection or `app(\Okapay\Client::class)`.

> [!NOTE]
>
> See the [PHP SDK reference](https://docs.okapay.net/integrations/php.md) for the full list of available methods, including product management, checkout sessions, and error handling.


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