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

# PHP SDK

> SDK PHP pour SahelPay

# PHP SDK

Le SDK PHP officiel pour intégrer SahelPay dans vos applications PHP et Laravel.

## Installation

```bash theme={null}
composer require sahelpay/php
```

<Note>
  En attendant la publication sur Packagist, clonez le repo GitHub :

  ```bash theme={null}
  git clone https://github.com/dione24/sahelpay-sdks.git
  ```
</Note>

## Configuration

```php theme={null}
use SahelPay\SahelPay;

$sahelpay = new SahelPay('sk_test_xxx');
```

### Laravel

```php theme={null}
// config/services.php
'sahelpay' => [
    'secret_key' => env('SAHELPAY_SECRET_KEY'),
    'webhook_secret' => env('SAHELPAY_WEBHOOK_SECRET'),
],
```

## Créer un lien de paiement

```php theme={null}
$link = $sahelpay->paymentLinks->create([
    'title' => 'Commande #123',
    'price' => 5000,
    'redirect_url' => 'https://votre-site.com/checkout/return',
]);

// Rediriger le client
header('Location: ' . $link->url);
exit;
```

## Vérifier le statut

```php theme={null}
$payment = $sahelpay->payments->retrieve('txn_abc123');

echo $payment->status; // 'PENDING', 'SUCCESS', 'FAILED'
echo $payment->amount;
```

## Vérifier un webhook

```php theme={null}
// Dans votre controller webhook
$rawBody = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_SAHELPAY_SIGNATURE'] ?? '';

$isValid = $sahelpay->webhooks->verify($rawBody, $signature);

if (!$isValid) {
    http_response_code(401);
    echo json_encode(['error' => 'Invalid signature']);
    exit;
}

$event = json_decode($rawBody, true);

if ($event['event'] === 'payment.success') {
    // Mettre à jour la commande
}

http_response_code(200);
echo json_encode(['received' => true]);
```

## Laravel Controller

```php theme={null}
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SahelPay\SahelPay;

class WebhookController extends Controller
{
    public function handle(Request $request)
    {
        $sahelpay = new SahelPay(config('services.sahelpay.secret_key'));
        
        $isValid = $sahelpay->webhooks->verify(
            $request->getContent(),
            $request->header('X-SahelPay-Signature')
        );

        if (!$isValid) {
            return response()->json(['error' => 'Invalid signature'], 401);
        }

        $event = $request->all();

        if ($event['event'] === 'payment.success') {
            // Mettre à jour la commande
        }

        return response()->json(['received' => true]);
    }
}
```
