PHP code example of ahmedibrahimyassindev / tamara-php-sdk

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

    

ahmedibrahimyassindev / tamara-php-sdk example snippets


use TamaraPhpSdk\Contracts\TamaraOrderableInterface;
use TamaraPhpSdk\Traits\TamaraOrderableTrait;

class Order extends \Illuminate\Database\Eloquent\Model implements TamaraOrderableInterface
{
    use TamaraOrderableTrait;

    public function getTamaraAmount(): float
    {
        return (float) $this->total_amount;
    }
}

use TamaraPhpSdk\TamaraService;

// Create service from environment variables
$service = TamaraService::fromEnv();

// Or instantiate manually with credentials
// $client = new \TamaraPhpSdk\TamaraClient($merchantToken, $notificationToken, $sandbox = true);
// $service = new TamaraService($client);

$checkoutUrl = $service->initiateCheckout(
    $order,
    'https://yourdomain.com/payment/success',
    'https://yourdomain.com/payment/failure',
    'https://yourdomain.com/payment/cancel'
);

// Redirect user to Tamara
header("Location: {$checkoutUrl}");
exit;

use TamaraPhpSdk\TamaraService;

$service   = TamaraService::fromEnv();
$processor = $service->createWebhookProcessor();


// Register resolvers to find matching orders by reference ID
$processor->addResolver(function (string $orderReferenceId) {
    return Order::where('order_reference_id', $orderReferenceId)->first();
});

$rawPayload = file_get_contents('php://input');
$data       = json_decode($rawPayload, true) ?: [];
$token      = $_GET['tamaraToken'] ?? null;
$authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? null;

$result = $processor->process($data, $rawPayload, $token, $authHeader);

header('Content-Type: application/json');
echo json_encode($result);

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\Request;
use TamaraPhpSdk\TamaraService;
use TamaraPhpSdk\TamaraWebhookProcessor;

class TamaraPaymentController extends Controller
{
    public function checkout(Order $order, TamaraService $tamara)
    {
        $redirectUrl = $tamara->initiateCheckout(
            $order,
            route('tamara.success'),
            route('tamara.failure'),
            route('tamara.cancel')
        );

        return redirect()->away($redirectUrl);
    }

    public function webhook(Request $request, TamaraWebhookProcessor $processor)
    {

        $processor->addResolver(fn($ref) => Order::where('order_reference_id', $ref)->first());

        $result = $processor->process(
            $request->all(),
            $request->getContent(),
            $request->bearerToken(),
            $request->header('Authorization'),
            $request->query()
        );
        return response()->json($result, $result['http_status'] ?? 200);
    }
}

$processor = $tamara->createWebhookProcessor();
$processor->autoAuthorise(true);

// Authorise order
$service->authoriseOrder($tamaraOrderId);

// Get order details
$orderData = $service->getOrder($tamaraOrderId);

// Capture payment (provide shipping information service->cancelOrder($tamaraOrderId);
bash
composer