Skip to main content

PHP

There is no official PHP SDK yet. Use cURL to interact with the Cashless API directly.

For WordPress/WooCommerce, see the WooCommerce plugin guide.

Create a bill

<?php
$apiKey = 'YOUR_KEY';
$apiSecret = 'YOUR_SECRET';
$baseUrl = 'https://api.cashless.co.tz'; // or https://api-sandbox.cashless.co.tz

function createBill($amount, $reference, $mobile = null) {
global $apiKey, $apiSecret, $baseUrl;

$payload = [
'amount' => $amount,
'reference' => $reference,
];
if ($mobile) {
$payload['mobile'] = $mobile;
}

$ch = curl_init("$baseUrl/api/v3/bills/create");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: ApiKey $apiKey:$apiSecret",
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200) {
throw new Exception("API error: $response");
}

return json_decode($response, true);
}

$bill = createBill(5000, 'ORDER-001', '0712345678');
echo "Payment URL: " . $bill['webPayUrl'];

Check payment status

function getBill($billId) {
global $apiKey, $apiSecret, $baseUrl;

$ch = curl_init("$baseUrl/api/v3/bills/$billId");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: ApiKey $apiKey:$apiSecret",
],
]);

$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, true);
}

$bill = getBill('a1b2c3d4-e5f6-7890-abcd-ef1234567890');
echo "Status: " . $bill['status'];

Laravel example

// app/Services/CashlessService.php
namespace App\Services;

use Illuminate\Support\Facades\Http;

class CashlessService
{
private string $baseUrl;
private string $apiKey;
private string $apiSecret;

public function __construct()
{
$this->baseUrl = config('services.cashless.url', 'https://api.cashless.co.tz');
$this->apiKey = config('services.cashless.key');
$this->apiSecret = config('services.cashless.secret');
}

public function createBill(float $amount, string $reference, ?string $mobile = null): array
{
$payload = compact('amount', 'reference');
if ($mobile) $payload['mobile'] = $mobile;

$response = Http::withHeaders([
'Authorization' => "ApiKey {$this->apiKey}:{$this->apiSecret}",
])->post("{$this->baseUrl}/api/v3/bills/create", $payload);

return $response->json();
}

public function getBill(string $id): array
{
$response = Http::withHeaders([
'Authorization' => "ApiKey {$this->apiKey}:{$this->apiSecret}",
])->get("{$this->baseUrl}/api/v3/bills/{$id}");

return $response->json();
}
}