PHP code example of keeal / keeal-php

1. Go to this page and download the library: Download keeal/keeal-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keeal / keeal-php example snippets




use Keeal\Checkout\KeealCheckout;

$checkout = new KeealCheckout([
    'apiKey' => getenv('KEEAL_API_KEY'),
    'baseUrl' => getenv('KEEAL_BASE_URL'),
]);

$session = $checkout->createSession([
    'line_items' => [[
        'price_data' => [
            'currency' => 'usd',
            'product_data' => ['name' => 'Pro plan'],
            'unit_amount' => 2900,
        ],
        'quantity' => 1,
    ]],
    'success_url' => 'https://yoursite.com/thanks',
    'cancel_url' => 'https://yoursite.com/cart',
    'client_reference_id' => 'order-123',
]);

header('Location: ' . $session['url']);
exit;



use Keeal\Checkout\WebhookVerifier;

$rawBody = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_KEEAL_SIGNATURE'] ?? '';

$event = WebhookVerifier::constructEvent(
    $rawBody,
    $signature,
    getenv('KEEAL_WEBHOOK_SECRET'),
);

if ($event['type'] === 'checkout.session.completed') {
    $order = $event['data']['object'];
    // $order['client_reference_id'], $order['transaction_id'], ...
}

http_response_code(200);
echo 'ok';

// Stripe-style line_items (recommended)
$checkout->createSession([
    'mode' => 'subscription',
    'line_items' => [['price' => 'price_abc123', 'quantity' => 1]],
    'success_url' => 'https://yoursite.com/welcome',
    'cancel_url' => 'https://yoursite.com/pricing',
]);

// Or subscription_data (line_items expanded server-side)
$checkout->createSession([
    'mode' => 'subscription',
    'subscription_data' => ['price_id' => 'price_abc123'],
    'success_url' => 'https://yoursite.com/welcome',
    'cancel_url' => 'https://yoursite.com/pricing',
]);
bash
composer