PHP code example of mitigoa / kra-php-sdk
1. Go to this page and download the library: Download mitigoa/kra-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/ */
mitigoa / kra-php-sdk example snippets
use KraPHP\KraClient;
$kra = new KraClient([
'client_id' => getenv('KRA_CLIENT_ID'),
'client_secret' => getenv('KRA_CLIENT_SECRET'),
'environment' => 'sandbox', // or 'production'
]);
// Validate a PIN
$pin = $kra->pin()->validate('A000000010');
echo $pin->taxpayerName; // "ACME KENYA LIMITED"
echo $pin->pinStatus; // "ACTIVE"
// Validate TCC
$tcc = $kra->tcc()->validate('A000000010', 'TCC-2024-XXXXX');
echo $tcc->isValid; // true
echo $tcc->expiryDate; // "2024-12-31"
// Get tax obligations
$obligations = $kra->taxpayer()->getObligations('A000000010');
// File NIL return
$result = $kra->returns()->fileNil([
'pin' => 'A000000010',
'obligation' => 'VAT',
'period' => '2024-10',
'reason' => 'No taxable supplies',
]);
// Submit eTIMS invoice
$invoice = new EtimsInvoice([
'invoiceNumber' => 'INV-2024-001',
'buyerPin' => 'A000000020',
'buyerName' => 'BUYER COMPANY LTD',
'invoiceDate' => '2024-11-01',
'currency' => 'KES',
'items' => [
[
'description' => 'Web Development Services',
'quantity' => 1,
'unitPrice' => 50000.00,
'vatRate' => 16,
'vatAmount' => 8000.00,
],
],
'totalExclVat' => 50000.00,
'totalVat' => 8000.00,
'totalInclVat' => 58000.00,
]);
$response = $kra->etims()->submitInvoice($invoice);
echo $response->invoiceId;
echo $response->controlUnit;
echo $response->qrCode;
$config = [
// Required
'client_id' => 'your-client-id',
'client_secret' => 'your-client-secret',
// Environment
'environment' => 'sandbox', // 'sandbox' | 'production'
// Base URLs (optional - defaults provided)
'sandbox_base_url' => 'https://api-sandbox.developer.go.ke',
'prod_base_url' => 'https://api.developer.go.ke',
'etims_sandbox_url' => 'https://etims-api-sbx.kra.go.ke',
'etims_prod_url' => 'https://etims-api.kra.go.ke/etims-api',
// Cache (optional)
'cache_driver' => 'file', // 'file' | 'redis' | 'memcached' | 'array'
'cache_ttl' => 3300, // seconds
// HTTP (optional)
'timeout' => 30, // seconds
'retry_attempts' => 3,
// eTIMS mTLS (optional)
'etims_cert_path' => '/path/to/client.crt',
'etims_key_path' => '/path/to/client.key',
];
$kra = new KraClient($config);
use KraPHP\Exceptions\AuthException;
use KraPHP\Exceptions\ApiException;
use KraPHP\Exceptions\RateLimitException;
try {
$pin = $kra->pin()->validate('A000000010');
} catch (AuthException $e) {
// Authentication failed
echo $e->getMessage();
echo $e->getKraErrorCode(); // e.g., 'INVALID_CREDENTIALS'
} catch (ApiException $e) {
// API error response
echo $e->getHttpStatusCode(); // e.g., 404
echo $e->getKraErrorCode(); // e.g., 'PIN_NOT_FOUND'
} catch (RateLimitException $e) {
// Rate limit exceeded
echo $e->getRetryAfter(); // seconds until retry
}
bash
composer