PHP code example of yasser-elgammal / tabby-php

1. Go to this page and download the library: Download yasser-elgammal/tabby-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/ */

    

yasser-elgammal / tabby-php example snippets


use YasserElgammal\TabbyPhp\Client\Configuration;
use YasserElgammal\TabbyPhp\Client\TabbyClient;
use YasserElgammal\TabbyPhp\DTOs\TabbyAddressData;
use YasserElgammal\TabbyPhp\DTOs\TabbyBuyerData;

$tabby = new TabbyClient(new Configuration(
    secretKey: $_ENV['TABBY_SECRET_KEY'],
    baseUrl: $_ENV['TABBY_BASE_URL'],
));

$checkout = $tabby->checkout()
    ->amount('250.00')
    ->currency('SAR')
    ->referenceId('order-1')
    ->buyer(new TabbyBuyerData('+966500000000', '[email protected]', 'Buyer Name'))
    ->shippingAddress(new TabbyAddressData('Riyadh', 'Street 1'))
    ->item($tabby->item()
        ->title('Product Name')
        ->quantity(1)
        ->unitPrice('250.00')
        ->referenceId('product-1')
        ->build())
    ->merchantUrls(
        'https://shop.test/success',
        'https://shop.test/cancel',
        'https://shop.test/failure',
    )
    ->build();

$response = $tabby->checkouts()->create($checkout);

if ($response->isAvailable()) {
    header('Location: ' . $response->url());
    exit;
}

use YasserElgammal\TabbyPhp\Client\Configuration;
use YasserElgammal\TabbyPhp\Client\TabbyClient;

$config = new Configuration(
    secretKey: $_ENV['TABBY_SECRET_KEY'],
    baseUrl: $_ENV['TABBY_BASE_URL'],
    merchantCode: $_ENV['TABBY_MERCHANT_CODE'] ?? null,
    webhookSecret: $_ENV['TABBY_WEBHOOK_SECRET'] ?? null,
);

$tabby = new TabbyClient($config);

use YasserElgammal\TabbyPhp\Client\TabbyClient;
use YasserElgammal\TabbyPhp\Client\Http\HttpClientInterface;

/** @var HttpClientInterface $httpClient */
$tabby = new TabbyClient($config, $httpClient);

$tabby->checkouts();
$tabby->payments();
$tabby->webhooks();

$tabby->checkout();
$tabby->item();

use YasserElgammal\TabbyPhp\DTOs\TabbyAddressData;
use YasserElgammal\TabbyPhp\DTOs\TabbyBuyerData;

$firstItem = $tabby->item()
    ->title('Product 1')
    ->quantity(1)
    ->unitPrice('150.00')
    ->referenceId('prod-1')
    ->build();

$secondItem = $tabby->item()
    ->title('Product 2')
    ->quantity(2)
    ->unitPrice('50.00')
    ->referenceId('prod-2')
    ->build();

$checkout = $tabby->checkout()
    ->amount('250.00')
    ->currency('SAR')
    ->referenceId('order-1')
    ->buyer(new TabbyBuyerData('+966500000000', '[email protected]', 'Buyer Name'))
    ->shippingAddress(new TabbyAddressData('Riyadh', 'Street 1'))
    ->items([$firstItem, $secondItem])
    ->merchantUrls(
        'https://shop.test/success',
        'https://shop.test/cancel',
        'https://shop.test/failure',
    )
    ->build();

$response = $tabby->checkouts()->create($checkout);

use YasserElgammal\TabbyPhp\DTOs\PaymentListFilters;
use YasserElgammal\TabbyPhp\DTOs\PaymentUpdateData;

// Retrieve a payment as an array.
$payment = $tabby->payments()->get('payment-id');

// Retrieve a strongly typed payment response.
$payment = $tabby->payments()->getResponse('payment-id');
echo $payment->status;
echo $payment->amount;
echo $payment->raw['currency'] ?? '';

// Update the merchant order reference associated with the payment.
$tabby->payments()->update(
    'payment-id',
    'new-order-id',
);

// The same update can be expressed explicitly with a DTO.
$tabby->payments()->update(
    'payment-id',
    new PaymentUpdateData(referenceId: 'new-order-id'),
);

// List payments with filters.
$payments = $tabby->payments()->all(new PaymentListFilters(
    createdAtGte: '2025-01-01',
    limit: 20,
));

// Close a payment.
$tabby->payments()->close('payment-id');

// Capture the complete remaining amount.
$tabby->payments()->capture('payment-id', 'capture-ref-123');

// Capture a specific amount.
$tabby->payments()->capture('payment-id', 'capture-ref-124', '100.00');

// Issue a refund.
$tabby->payments()->refund(
    'payment-id',
    'refund-ref-456',
    '50.00',
    'Customer requested refund',
);

$webhook = $tabby->webhooks()->create('https://shop.example/webhooks/tabby');

// Optionally register a custom header to be sent with webhook requests.
$webhook = $tabby->webhooks()->create(
    'https://shop.example/webhooks/tabby',
    headers: ['title' => 'X-Webhook-Token', 'value' => 'expected-value'],
);

$webhooks = $tabby->webhooks()->all();
$webhook = $tabby->webhooks()->get('webhook-id');
$webhookResponse = $tabby->webhooks()->getResponse('webhook-id');

$tabby->webhooks()->update('webhook-id', 'https://shop.example/webhooks/tabby');
$tabby->webhooks()->delete('webhook-id');

$providedSecret = $_SERVER['HTTP_X_WEBHOOK_SECRET'] ?? null;

if (! $tabby->webhookVerifier()->verify($providedSecret)) {
    http_response_code(401);
    exit('Invalid webhook secret');
}

// The secret is valid; process the webhook body now.
$payload = json_decode(file_get_contents('php://input'), true, flags: JSON_THROW_ON_ERROR);

use YasserElgammal\TabbyPhp\Exceptions\ApiException;
use YasserElgammal\TabbyPhp\Exceptions\TabbyException;
use YasserElgammal\TabbyPhp\Exceptions\TransportException;
use YasserElgammal\TabbyPhp\Exceptions\ValidationException;

try {
    $tabby->payments()->get('invalid-id');
} catch (ValidationException $e) {
    // Local validation failure.
} catch (ApiException $e) {
    // Tabby API error.
    echo $e->getMessage();
    echo $e->getCode();
    print_r($e->response);
} catch (TransportException $e) {
    // Network error.
} catch (TabbyException $e) {
    // Any other SDK error.
}

use YasserElgammal\TabbyPhp\Exceptions\ValidationException;

try {
    $checkout = $tabby->checkout()
        ->amount('-10')
        ->build();
} catch (ValidationException $e) {
    echo $e->getMessage();
}

use YasserElgammal\TabbyPhp\Client\Configuration;
use YasserElgammal\TabbyPhp\Client\Http\HttpClientInterface;
use YasserElgammal\TabbyPhp\Client\Http\Response;
use YasserElgammal\TabbyPhp\Client\TabbyClient;

$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient->method('request')->willReturn(
    new Response(200, [], '{"id":"payment-id"}'),
);

$tabby = new TabbyClient(
    new Configuration(secretKey: 'test-secret'),
    $httpClient,
);

$payment = $tabby->payments()->get('payment-id');

$this->assertSame('payment-id', $payment['id']);
bash
composer