PHP code example of paytabs / laravel-sdk

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

    

paytabs / laravel-sdk example snippets


use Paytabs\Laravel\Facades\Paytabs;
use Paytabs\Sdk\Enums\TranClass;
use Paytabs\Sdk\Enums\TranType;
use Paytabs\Sdk\Request\Payload\PayloadsFactory;
use Paytabs\Sdk\Request\Payload\Parts\CustomerDetails;
use Paytabs\Sdk\Request\RequestsFactory;

// Create the payload
$payload = PayloadsFactory::createHostedPage();

// Add customer details
$payload
    ->buildTransaction(TranType::Sale, TranClass::Ecom)
    ->buildCart('order-001', 'AED', 99.5, 'Order Payment')
    ->buildCustomerDetails(
        CustomerDetails::init('Fname Lname', '+971500000000', '[email protected]')
            ->setAddress('UAE', 'State', 'City', 'Street', '12345')
    );

// Create the request
$request = RequestsFactory::createPaymentRequest($payload);

// Submit and get response
$response = Paytabs::submitRequest($request);

if ($response->isFailure()) {
    echo $response->getFailure()->code . ' - ' . $response->getFailure()->message;
    exit;
}

if ($response->isRedirect()) {
    $paymentUrl = $response->getRedirect()->redirect_url;
}

'ipn_handler' => \App\Services\PaytabsIpnHandler::class,



namespace App\Services;

use Paytabs\Laravel\Contracts\IpnHandlerInterface;
use Paytabs\Sdk\Response\Responses\Webhook\AbstractTransactionResult;
use Paytabs\Sdk\Response\Payload\Payloads\Callbacks\Ipn;

class PaytabsIpnHandler implements IpnHandlerInterface
{
    public function handleIpn(
        AbstractTransactionResult $transactionResult,
        Ipn $mappedPayload
    ): void {
        // Update your order status
        if ($mappedPayload->isPaymentSuccessful()) {
            // Payment successful

            // Fetch order by Tran Ref
            $order = Order::where('tran_ref', $mappedPayload->tran_ref)->first();
            // Fetch order by Order id
            $order = Order::find($mappedPayload->cart_id);

            $order->update(['status' => 'paid']);
        }
    }
}

use Paytabs\Laravel\Facades\Paytabs;
use Paytabs\Sdk\Profile\Endpoints\Jordan;
use Paytabs\Sdk\Profile\ProfilesFactory;
use Paytabs\Sdk\Profile\EndpointsFactory;

// Create a custom profile
$profile = ProfilesFactory::createProfile(
    EndpointsFactory::getKsaEndpoint(),
    12345,
    'your_server_key'
);

// Use the custom profile
Paytabs::usingProfile($profile);

// Or use credentials directly
Paytabs::usingCredentials(12345, 'your_server_key', Jordan::CODE);

// Reset to default configuration
Paytabs::usingDefaults();
bash
php artisan vendor:publish --tag=paytabs-config