PHP code example of danipa / sdk

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

    

danipa / sdk example snippets


use Danipa\Sdk\Danipa;
use Danipa\Sdk\RequestOptions;
use Danipa\Sdk\Model\CreateCollectionRequest;
use Danipa\Sdk\Model\Payer;

// API key prefix decides sandbox vs production:
//   dk_test_… → https://api.sandbox.danipa.com/ms
//   dk_live_… → https://api.danipa.com/ms
$danipa = Danipa::builder()
    ->apiKey(getenv('DANIPA_API_KEY'))
    ->build();

$collection = $danipa->collections()->create(
    new CreateCollectionRequest(
        amount: '125.00',
        currency: 'GHS',
        payer: Payer::phone('+233244112233', 'Ama K.'),
    ),
    RequestOptions::withIdempotencyKey('order-1234'),
);

echo $collection->status->value; // "PENDING"

use Danipa\Sdk\Model\CreateDisbursementRequest;
use Danipa\Sdk\Model\Recipient;

$danipa->disbursements()->create(
    new CreateDisbursementRequest(
        amount: '500.00',
        currency: 'GHS',
        recipient: Recipient::phone('+233244998877', 'Kojo'),
    ),
    RequestOptions::withIdempotencyKey('payout-' . $order->id),
);

$danipa = Danipa::builder()
    ->apiKey(getenv('DANIPA_API_KEY'))
    ->maxRetries(5)
    ->timeoutMs(60000)
    ->build();

use Danipa\Sdk\DanipaWebhook;

$payload   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_DANIPA_SIGNATURE'] ?? '';
$timestamp = $_SERVER['HTTP_X_DANIPA_TIMESTAMP'] ?? '';
$secret    = getenv('DANIPA_WEBHOOK_SECRET');

if (!DanipaWebhook::verify($payload, $signature, $timestamp, $secret)) {
    http_response_code(401);
    echo json_encode(['error' => 'Invalid signature']);
    exit;
}

$event = json_decode($payload, true);
// … dispatch event …
http_response_code(200);

use Danipa\Sdk\DanipaWebhook;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/webhooks/danipa', methods: ['POST'])]
public function __invoke(Request $request): Response
{
    $ok = DanipaWebhook::verify(
        $request->getContent(),
        $request->headers->get('X-Danipa-Signature', ''),
        $request->headers->get('X-Danipa-Timestamp', ''),
        $_ENV['DANIPA_WEBHOOK_SECRET'],
    );
    if (!$ok) {
        return new Response('', 401);
    }
    // … decode + dispatch …
    return new Response('', 200);
}

use Danipa\Sdk\DanipaWebhook;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::post('/webhooks/danipa', function (Request $request) {
    $ok = DanipaWebhook::verify(
        $request->getContent(),                            // raw body — NOT $request->all()
        $request->header('X-Danipa-Signature', ''),
        $request->header('X-Danipa-Timestamp', ''),
        env('DANIPA_WEBHOOK_SECRET'),
    );
    abort_if(!$ok, 401);
    // … dispatch …
    return response()->noContent();
});

use Danipa\Sdk\Errors\DanipaApiError;
use Danipa\Sdk\Errors\DanipaNetworkError;

try {
    $danipa->collections()->create($req);
} catch (DanipaApiError $e) {
    // Backend rejected the request — 4xx or exhausted-retry 5xx.
    fwrite(STDERR, sprintf(
        "[%d] %s: %s (correlationId: %s)\n",
        $e->status,
        $e->errorCode,
        $e->getMessage(),
        $e->correlationId ?? '-',
    ));
} catch (DanipaNetworkError $e) {
    // Never reached the backend (DNS, TLS, timeout, connection reset).
    fwrite(STDERR, 'Network failure: ' . ($e->getPrevious()?->getMessage() ?? $e->getMessage()) . "\n");
}

use Symfony\Component\HttpClient\Psr18Client;

$danipa = Danipa::builder()
    ->apiKey(getenv('DANIPA_API_KEY'))
    ->transport(new Psr18Client())
    ->build();