kra-connect/php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kra-connect/php-sdk

Composer 安装命令:

composer require kra-connect/php-sdk

包简介

Official PHP SDK for KRA GavaConnect API - Tax compliance and verification for Kenya

README 文档

README

Official PHP SDK for Kenya Revenue Authority's GavaConnect API

Latest Version Total Downloads License: MIT PHP Version

Repository

Features

  • PIN Verification - Verify KRA PIN numbers
  • TCC Verification - Check Tax Compliance Certificates
  • e-Slip Validation - Validate electronic payment slips
  • NIL Returns - File NIL returns programmatically
  • Taxpayer Details - Retrieve taxpayer information
  • Type Safety - Full PHP 8.1+ type declarations
  • PSR Standards - PSR-4 autoloading, PSR-3 logging, PSR-6 caching
  • Laravel Support - Service provider and facades
  • Symfony Support - Bundle integration
  • Retry Logic - Automatic retry with exponential backoff
  • Caching - Response caching for improved performance
  • Rate Limiting - Built-in rate limiting

Requirements

  • PHP 8.1 or higher
  • Composer
  • ext-json

Installation

Via Composer

composer require kra-connect/php-sdk

Laravel

The package will automatically register the service provider. Publish the configuration:

php artisan vendor:publish --provider="KraConnect\Laravel\KraConnectServiceProvider"

Symfony

Register the bundle in config/bundles.php:

return [
    // ...
    KraConnect\Symfony\KraConnectBundle::class => ['all' => true],
];

Quick Start

Basic Usage

<?php

use KraConnect\KraClient;

// Initialize the client
$client = new KraClient('your-api-key');

// Verify a PIN
$result = $client->verifyPin('P051234567A');

if ($result->isValid) {
    echo "Taxpayer: " . $result->taxpayerName . "\n";
    echo "Status: " . $result->status . "\n";
} else {
    echo "Invalid PIN: " . $result->errorMessage . "\n";
}

Using Configuration

<?php

use KraConnect\KraClient;
use KraConnect\Config\KraConfig;

$config = new KraConfig(
    apiKey: 'your-api-key',
    baseUrl: 'https://api.kra.go.ke/gavaconnect/v1',
    timeout: 30,
    retries: 3
);

$client = new KraClient($config);

Laravel Usage

<?php

namespace App\Http\Controllers;

use KraConnect\Facades\KraConnect;

class TaxController extends Controller
{
    public function verifySupplier($pin)
    {
        $result = KraConnect::verifyPin($pin);

        return response()->json($result);
    }
}

Or inject the client:

<?php

use KraConnect\KraClient;

class TaxService
{
    public function __construct(
        private KraClient $kraClient
    ) {}

    public function verify($pin)
    {
        return $this->kraClient->verifyPin($pin);
    }
}

API Reference

KraClient

verifyPin(string $pinNumber): PinVerificationResult

Verify a KRA PIN number.

Parameters:

  • $pinNumber - The PIN to verify (format: P + 9 digits + letter)

Returns:

  • PinVerificationResult - Verification result with taxpayer details

Throws:

  • InvalidPinFormatException - If PIN format is invalid
  • ApiAuthenticationException - If API key is invalid
  • ApiTimeoutException - If request times out
  • RateLimitExceededException - If rate limit is exceeded

Example:

$result = $client->verifyPin('P051234567A');
echo $result->taxpayerName;

verifyTcc(string $tccNumber): TccVerificationResult

Verify a Tax Compliance Certificate.

Parameters:

  • $tccNumber - The TCC number to verify

Returns:

  • TccVerificationResult - TCC verification result

Example:

$result = $client->verifyTcc('TCC123456');
echo "Valid until: " . $result->expiryDate->format('Y-m-d');

validateEslip(string $slipNumber): EslipValidationResult

Validate an electronic payment slip.

fileNilReturn(string $pinNumber, string $period, string $obligationId): NilReturnResult

File a NIL return for a taxpayer.

Example:

$result = $client->fileNilReturn(
    pinNumber: 'P051234567A',
    period: '202401',
    obligationId: 'OBL123456'
);

getTaxpayerDetails(string $pinNumber): TaxpayerDetails

Retrieve detailed taxpayer information.

Configuration

$config = new KraConfig(
    apiKey: 'your-api-key',
    baseUrl: 'https://api.kra.go.ke/gavaconnect/v1',
    timeout: 30,
    retries: 3,
    cacheEnabled: true,
    cacheTtl: 3600,
    rateLimitMaxRequests: 100,
    rateLimitWindowSeconds: 60
);

Error Handling

<?php

use KraConnect\KraClient;
use KraConnect\Exceptions\{
    InvalidPinFormatException,
    ApiAuthenticationException,
    ApiTimeoutException,
    RateLimitExceededException,
    ApiException
};

try {
    $result = $client->verifyPin('P051234567A');
} catch (InvalidPinFormatException $e) {
    echo "Invalid PIN format: " . $e->getMessage();
} catch (ApiAuthenticationException $e) {
    echo "Authentication failed: " . $e->getMessage();
} catch (ApiTimeoutException $e) {
    echo "Request timed out: " . $e->getMessage();
} catch (RateLimitExceededException $e) {
    echo "Rate limit exceeded. Retry after: " . $e->getRetryAfter() . " seconds";
    sleep($e->getRetryAfter());
    // Retry
} catch (ApiException $e) {
    echo "API error: " . $e->getMessage();
    echo "Status code: " . $e->getStatusCode();
}

Laravel Configuration

After publishing, edit config/kra-connect.php:

return [
    'api_key' => env('KRA_API_KEY'),
    'base_url' => env('KRA_API_BASE_URL', 'https://api.kra.go.ke/gavaconnect/v1'),
    'timeout' => env('KRA_TIMEOUT', 30),
    'retries' => env('KRA_MAX_RETRIES', 3),
    'cache' => [
        'enabled' => env('KRA_CACHE_ENABLED', true),
        'ttl' => env('KRA_CACHE_TTL', 3600),
        'store' => env('KRA_CACHE_STORE', 'redis'),
    ],
    'rate_limit' => [
        'enabled' => env('KRA_RATE_LIMIT_ENABLED', true),
        'max_requests' => env('KRA_RATE_LIMIT_MAX_REQUESTS', 100),
        'window_seconds' => env('KRA_RATE_LIMIT_WINDOW_SECONDS', 60),
    ],
];

Add to .env:

KRA_API_KEY=your_api_key_here
KRA_API_BASE_URL=https://api.kra.go.ke/gavaconnect/v1
KRA_TIMEOUT=30
KRA_MAX_RETRIES=3
KRA_CACHE_ENABLED=true
KRA_CACHE_TTL=3600

Advanced Usage

Batch Verification

$pins = ['P051234567A', 'P051234567B', 'P051234567C'];
$results = $client->verifyPinsBatch($pins);

foreach ($results as $result) {
    echo "{$result->pinNumber}: " . ($result->isValid ? '' : '') . "\n";
}

Custom HTTP Client

use GuzzleHttp\Client;

$httpClient = new Client([
    'timeout' => 60,
    'verify' => true,
]);

$client = new KraClient('your-api-key', $httpClient);

Custom Cache

use Symfony\Component\Cache\Adapter\RedisAdapter;

$cache = new RedisAdapter(
    RedisAdapter::createConnection('redis://localhost')
);

$config = new KraConfig(
    apiKey: 'your-api-key',
    cache: $cache
);

$client = new KraClient($config);

Testing

# Run tests
composer test

# Run tests with coverage
composer test-coverage

# Run PHPStan
composer phpstan

# Fix code style
composer cs-fix

# Check code style
composer cs-check

Examples

See the examples directory for more usage examples:

Development

Setup

# Clone the repository
git clone https://github.com/your-org/kra-connect.git
cd kra-connect/packages/php-sdk

# Install dependencies
composer install

# Run tests
composer test

Code Style

This package follows PSR-12 coding standards. Use PHP CS Fixer:

composer cs-fix

Static Analysis

Run PHPStan for static analysis:

composer phpstan

Publishing

Publishing to Packagist

# Update version in composer.json
# Update CHANGELOG.md

# Tag the release
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0

# Packagist will automatically detect the new tag
# Or manually update on https://packagist.org/packages/kra-connect/php-sdk

GitHub Release

# Create GitHub release
gh release create v1.0.0 --title "v1.0.0" --notes "Release notes here"

Contributing

Contributions are welcome! Please:

  1. Fork the repository: BerjisTech/kra-connect-php-sdk
  2. Create a feature branch
  3. Make your changes with tests
  4. Submit a pull request

License

MIT License - see LICENSE for details.

Support

Changelog

See CHANGELOG.md for version history.

Disclaimer

This is an independent project and is not officially affiliated with or endorsed by the Kenya Revenue Authority.

kra-connect/php-sdk 适用场景与选型建议

kra-connect/php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「php」 「laravel」 「API-Client」 「kenya」 「kra」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kra-connect/php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kra-connect/php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 kra-connect/php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-01