PHP code example of paytabs / php-sdk

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




use Paytabs\Sdk\Enums\TranClass;
use Paytabs\Sdk\Enums\TranType;
use Paytabs\Sdk\Exceptions\PaytabsExceptionInterface;
use Paytabs\Sdk\Paytabs;
use Paytabs\Sdk\PaytabsLogger;
use Paytabs\Sdk\Profile\ProfilesFactory;
use Paytabs\Sdk\Request\Payload\PayloadsFactory;
use Paytabs\Sdk\Request\RequestsFactory;

$profile = ProfilesFactory::createUaeProfile(
	(int) getenv('PAYTABS_PROFILE_ID'),
	(string) getenv('PAYTABS_SERVER_KEY')
);

// OR create any Logger implements LoggerInterface
$logger = PaytabsLogger::getInstance()->logger;

$payload = PayloadsFactory::createHostedPage();
$payload
    ->buildTransaction(TranType::Sale, TranClass::Ecom)
    ->buildCart('order-1001', 'AED', 100.00, 'Order 1001')
    // Where the shopper returns to, and where PayTabs posts the Callback.
    ->buildURLs('https://example.com/return', 'https://example.com/ipn')
    ->buildHideShipping(true);

$request = RequestsFactory::createPaymentRequest($payload);

$paytabs = Paytabs::getInstance($profile, null, $logger);
$paytabs->setRequest($request);

try {
    $response = $paytabs->submit();
} catch (PaytabsExceptionInterface $e) {
    // Every SDK exception implements this interface.
    // Never retry a payment request blindly: there is no idempotency key, so a
    // retry after a timeout can charge the shopper twice. Query first instead.
    error_log('PayTabs request failed: '.$e->getMessage());
    exit('Payment could not be started.');
}

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

if ($response->isRedirect()) {
    // Hosted payment page: send the shopper to PayTabs to pay.
    $redirect = $response->getRedirect();
    header('Location: '.$redirect->redirect_url);
    exit;
}

// Completed without a redirect (for example a tokenised repeat payment).
$completed = $response->getPayloadMapped();

// These return ?bool, so compare against true: null means "no status reported",
// and a pending transaction is neither successful nor failed.
if (true === $completed->isTransactionSuccessful()) {
    // Paid. Fulfil the order.
} elseif (true === $completed->isTransactionPending()) {
    // Not final yet — wait for the IPN, do not fulfil and do not cancel.
} elseif (true === $completed->isTransactionFailed()) {
    // Declined or failed.
} else {
    // Unknown: query the transaction before acting on it.
}

use Paytabs\Sdk\Exceptions\PaytabsExceptionInterface;

try {
    $response = $paytabs->submit();
} catch (PaytabsExceptionInterface $e) {
    // Anything raised by the SDK.
}

use Paytabs\Sdk\PaytabsLogger;

$fileLogger = PaytabsLogger::getInstance()->logger;
$browserLogger = PaytabsLogger::getInstance(null, true)->logger;

$logger = new Paytabs\Sdk\Logger\FileLog('PayTabs', PaytabsLogger::getLogFile('/var/log/my-app/'));

use Paytabs\Sdk\Response\Payload\Payloads\Paytabs as ResponsePayload;

ResponsePayload::setLogger($myPsrLogger);

use Paytabs\Sdk\Exceptions\UnknownResponseValueException;
use Paytabs\Sdk\Response\Payload\Payloads\Paytabs as ResponsePayload;

ResponsePayload::setStrictMode(true);

try {
	$mapped = $response->getPayload()->getMapped();
} catch (UnknownResponseValueException $e) {
	// Unknown transaction type/class/status encountered.
}

// Optional: restore default tolerant mode.
ResponsePayload::setStrictMode(false);
bash
composer