PHP code example of taitech / azampay

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

    

taitech / azampay example snippets


// Using the Facade
use Taitech\Azampay\Facades\Azampay;
$response = Azampay::checkout($payload);

// Using the Service Container
$azampay = app('azampay');
$response = $azampay->checkout($payload);

// Using Dependency Injection
public function __construct(private AzampayService $azampay) {}
$response = $this->azampay->checkout($payload);

use Taitech\Azampay\AzampayService;

// Initialize the service
$azampay = new AzampayService(
    appName: 'YOUR_APP_NAME',
    clientId: 'YOUR_CLIENT_ID',
    secret: 'YOUR_CLIENT_SECRET',
    env: 'SANDBOX', // or 'PRODUCTION'
    service: 'MNO' // or 'BANK' or 'MERCHANT'
);

$azampay = new AzampayService(
    appName: 'YOUR_APP_NAME',
    clientId: 'YOUR_CLIENT_ID',
    secret: 'YOUR_CLIENT_SECRET',
    env: 'SANDBOX', // or 'PRODUCTION'
    service: 'MNO'
);

$azampay = new AzampayService(
    appName: 'YOUR_APP_NAME',
    clientId: 'YOUR_CLIENT_ID',
    secret: 'YOUR_CLIENT_SECRET',
    env: 'SANDBOX', // or 'PRODUCTION'
    service: 'BANK'
);

$azampay = new AzampayService(
    appName: 'YOUR_APP_NAME',
    clientId: 'YOUR_CLIENT_ID',
    secret: 'YOUR_CLIENT_SECRET',
    env: 'SANDBOX', // or 'PRODUCTION'
    service: 'MERCHANT'
);

// vendor credentials
$app = 'YOUR_APP_NAME';
$clientId = 'YOUR_CLIENT_ID';
$secret = 'YOUR_SECRET_KEY';


try {
     $azampay = new AzampayService(
        $app, // App Name
        $clientId, // Client ID
        $secret, // Client Secret
        'SANDBOX', // Environment (SANDBOX/PRODUCTION)
        'MNO', // Service (MNO, BANK, MERCHANT)
    );

    // Example of calling the checkout method for MNO [MOBILE PAYMENT]
    $payload = [
        'amount' => 5000,
        'accountNumber' => '0713 295 803',      // normalised to 255713295803
        'provider' => 'Mpesa',                  // Airtel, Tigo, Halopesa, Azampesa or Mpesa
        'externalId' => 'ORDER-1001-PAY-1',     // YOUR reference (≤ 128 chars) — returned as utilityref in the callback
        'additionalProperties' => [
                'order' => 'ORDER-1001',
            ],
    ];

    $response = $azampay->checkout($payload);
    // ['transactionId' => '...', 'message' => '...', 'success' => true, 'externalId' => 'ORDER-1001-PAY-1']

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

use Taitech\Azampay\Modules\CallbackModule;

// Laravel: app('azampay.callback') is pre-configured (fetches AzamPay's
// public key automatically, or uses AZAMPAY_PUBLIC_KEY).
$callback = app('azampay.callback');

// Plain PHP: give it the PEM key, or a closure that fetches it.
$callback = new CallbackModule(null, fn () => $azampay->getPublicKey());

$data = json_decode($request->getContent(), true);

if (! $callback->verify($data)) {
    return response()->json(['success' => false], 401);
}

$result = $callback->parse($data);
// $result['reference']  → your externalId
// $result['successful'] → true when transactionstatus is "success"
// $result['amount'], $result['operator'], $result['mno_reference'], ...

new AzampayService($app, $clientId, $secret, 'SANDBOX', 'MNO', [
    'timeout' => 30,        // seconds
    'force_ipv4' => true,   // resolve AzamPay hosts over IPv4 only
]);

$bank = $azampay->for('BANK');   // or new AzampayService(..., 'BANK')

$response = $bank->checkout([
    'provider' => 'CRDB',                  // CRDB or NMB
    'amount' => 25000,
    'merchantAccountNumber' => '0150123456789',
    'merchantMobileNumber' => '0712345678',
    'otp' => '123456',
    'referenceId' => 'ORDER-1001-PAY-2',   // yours; generated if omitted and returned
]);

$payouts = $azampay->for('DISBURSEMENT');

$payouts->nameLookup('tigo', '255712345678');   // confirm the recipient's name first

$response = $payouts->disburse([
    'source' => ['countryCode' => 'TZ', 'fullName' => 'My Shop Ltd', 'bankName' => 'azampesa', 'accountNumber' => '2557…', 'currency' => 'TZS'],
    'destination' => ['countryCode' => 'TZ', 'fullName' => 'Asha Juma', 'bankName' => 'tigo', 'accountNumber' => '255712345678', 'currency' => 'TZS'],
    'transferDetails' => ['amount' => 15000],    // type defaults to MNO, dateInEpoch to now
    'externalReferenceId' => 'REFUND-42',        // yours; generated if omitted and returned
    'remarks' => 'Refund for order 42',
    // 'checksum' => '...',                      // if AzamPay 

$merchant = $azampay->for('MERCHANT');

$partners = $merchant->paymentPartners();   // names, logos, provider ids

$page = $merchant->checkoutPage([
    'amount' => 15000,
    'vendorId' => 'your-vendor-id',
    'vendorName' => 'My Shop',
    'requestOrigin' => 'https://shop.example',
    'redirectSuccessURL' => 'https://shop.example/paid',
    'redirectFailURL' => 'https://shop.example/failed',
    'cart' => ['items' => [['name' => 'Router']]],
]);

return redirect($page['url']);   // $page['externalId'] is your reference

Http::fake([
    'authenticator-sandbox.azampay.co.tz/*' => Http::response(['data' => ['accessToken' => 'test']]),
    'sandbox.azampay.co.tz/azampay/mno/checkout' => Http::response(['transactionId' => 'T1', 'success' => true]),
]);

app('azampay')->checkout([...]);

Http::assertSent(fn ($request) => $request['externalId'] === 'ORDER-1001-PAY-1');