PHP SDK
Official PHP SDK for Snippe — fluent payment builder and webhook handler
The official Snippe SDK for PHP. Provides a fluent builder for creating payments and a one-line webhook capture helper.
- Source: github.com/Neurotech-HQ/snippe-php-sdk
- Packagist:
snippe/snippe-php
Install
composer require snippe/snippe-phpRequirements: PHP 8.1+, ext-curl, ext-json.
Initialize the client
use Snippe\Snippe;
$snippe = new Snippe('snp_your_api_key');Load the API key from an environment variable (e.g. getenv('SNIPPE_API_KEY'))
rather than committing it to source.
Create a payment
The SDK exposes a fluent builder for each payment type. Method chaining returns a builder; ->send() performs the HTTP call.
$payment = $snippe->mobileMoney(5000, '0754123456')
->customer('John Doe', 'john@email.com')
->send();The customer's phone receives a USSD push to authorize. The SDK normalises phone numbers automatically (0754..., 255754..., and +255754... all work).
$payment = $snippe->card(10000)
->phone('0754123456')
->customer('John Doe', 'john@email.com')
->billing('123 Main Street', 'Dar es Salaam', 'DSM', '14101', 'TZ')
->redirectTo(
'https://yoursite.com/success',
'https://yoursite.com/cancel'
)
->send();
// Redirect the customer
header('Location: ' . $payment->paymentUrl());
exit;See the Payments API for every supported field.
Verify a webhook
Webhook::capture() reads the raw request body, verifies the HMAC-SHA256 signature, rejects stale timestamps, and returns a parsed event.
use Snippe\Webhook;
$event = Webhook::capture();
if ($event->isPaymentCompleted()) {
$reference = $event->reference();
// Mark order as paid
}
http_response_code(200);The signing key is read from the SNIPPE_WEBHOOK_SECRET environment variable by default. To pass it explicitly:
$event = Webhook::capture(['signing_key' => $config['snippe_secret']]);For the full event catalogue, see the Webhooks guide.
Don't read php://input yourself and re-serialize the JSON — that breaks the
signature. Webhook::capture() handles the raw body for you.
Errors
The SDK throws Snippe\SnippeException on any non-2xx response.
use Snippe\SnippeException;
try {
$payment = $snippe->mobileMoney(5000, '0754123456')
->customer('John Doe', 'john@email.com')
->send();
} catch (SnippeException $e) {
error_log($e->getMessage());
// Inspect $e->errorCode() for the API error code
}See Error handling for the full code list.
Idempotency
The PHP SDK caches idempotency keys for 24 hours and automatically reuses them on retry. To pass an explicit key:
$payment = $snippe->mobileMoney(5000, '0754123456')
->customer('John Doe', 'john@email.com')
->idempotencyKey('order-12345-attempt-1')
->send();Keys must be 30 characters or fewer — longer keys return a PAY_001 error.
Public API surface
| Class | Namespace |
|---|---|
Snippe | Snippe\Snippe |
PaymentBuilder | Snippe\PaymentBuilder |
Payment | Snippe\Payment |
Webhook | Snippe\Webhook |
SnippeException | Snippe\SnippeException |