Skip to main content

PHP SDK

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

Installation

composer require sahelpay/php
En attendant la publication sur Packagist, clonez le repo GitHub :
git clone https://github.com/dione24/sahelpay-sdks.git

Configuration

use SahelPay\SahelPay;

$sahelpay = new SahelPay('sk_test_xxx');

Laravel

// config/services.php
'sahelpay' => [
    'secret_key' => env('SAHELPAY_SECRET_KEY'),
    'webhook_secret' => env('SAHELPAY_WEBHOOK_SECRET'),
],

Créer un lien de paiement

$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

$payment = $sahelpay->payments->retrieve('txn_abc123');

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

Vérifier un webhook

// 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

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]);
    }
}