PHP code example of tabbyai / laravel

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

    

tabbyai / laravel example snippets


use Tabby\Services\TabbyService;

$tabbyService = new TabbyService(
    merchantCode: 'your_merchant_code',
    publicKey: 'your_public_key',
    secretKey: 'your_secret_key',
    currency: 'SAR' // Optional, default is SAR
);

use Tabby\Models\Buyer;
use Tabby\Models\Order;
use Tabby\Models\ShippingAddress;
use Tabby\Models\OrderItem;

try {
    // Sample buyer data
    $buyer = new Buyer(
        phone: '500000001',
        email: '[email protected]',
        name: 'John Doe',
        dob: '1990-01-01',
    );

    // Sample order data
    $order = new Order(
        referenceId: 'order-001',
        items: [
            new OrderItem(
                title: 'Product Name',
                category: 'electronics',
                unitPrice: 100,
                quantity: 1,
                referenceId: 'prod-001',
                description: 'Product Description',
            ),
        ],
    );

    // Sample shipping address data
    $shippingAddress = new ShippingAddress(
        city: 'Al-Khobar',
        address: 'Street Address',
        zip: '12345',
    );

    // Create a checkout session
    $checkoutSession = $tabbyService->createSession(
        amount: 200,
        buyer: $buyer,
        order: $order,
        shippingAddress: $shippingAddress,
        description: 'order description',
        successCallback: 'https://example.com/success',
        cancelCallback: 'https://example.com/cancel',
        failureCallback: 'https://example.com/failure',
        // lang: 'ar',            // optional
        // buyerHistory: $buyerHistory,   // optional
        // orderHistory: $orderHistory,   // optional
    );

    // Fetch the payment url from the checkout session
    $paymentUrl = $checkoutSession->getPaymentUrl();

    // Redirect to the payment page
    return redirect($paymentUrl);
} catch (Exception $e) {
    // Handle exceptions
    return response()->json(['error' => $e->getMessage()], 500);
}

$checkoutSession = $tabbyService->retrieveCheckoutSession('session_id_here');

$payment = $tabbyService->retrievePayment('payment_id_here');

$updatedPayment = $tabbyService->updatePayment(
    paymentId: 'payment_id_here',  // Payment ID
    referenceId: 'new_reference_id',  // Optional updated reference ID
    deliveryTracking: [
        ['tracking_number' => 'xxx', 'courier_code' => 'yyy']
    ],
);

$capturedPayment = $tabbyService->capturePayment(
    paymentId: 'payment_id_here',  // Payment ID
    amount: 100.00, // Amount
    referenceId: 'reference_id_here' // Optional reference ID
);

$refundedPayment = $tabbyService->refundPayment(
    paymentId: 'payment_id_here',  // Payment ID
    amount: 50.00, // Amount to refund
    referenceId: 'reference_id_here', // Optional reference ID
    reason: 'refund reason' // Optional
);

$closedPayment = $tabbyService->closePayment('payment_id_here');

list($payments, $pagination) = $tabbyService->listPayments(
    createdAtGte: '', // Optional
    createdAtLte: '', // Optional
    status: null, // Optional
    limit: 10, // Optional
    offset: 0, // Optional
);

$webhook = $tabbyService->registerWebhook(
    url: 'https://your-domain.com/webhook-url',  // URL to receive webhook notifications
    isTest: true,  // Test mode (optional)
    headerTitle: 'Custom-Header-Title',  // Optional
    headerValue: 'Custom-Header-Value'  // Optional
);

$webhooks = $tabbyService->retrieveAllWebhooks();

$webhook = $tabbyService->retrieveWebhook('webhook_id_here');

$webhook = $tabbyService->updateWebhook(
    webhookId: 'webhook_id_here',
    url: 'https://your-domain.com/webhook-url',  // URL to receive webhook notifications
    isTest: true,  // Test mode
);

$webhook = $tabbyService->deleteWebhook('webhook_id_here');

try {
    // your code
} catch (Exception $e) {
    // handle the error
}