PHP code example of intelfric-technology / laravel-snippe
1. Go to this page and download the library: Download intelfric-technology/laravel-snippe 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/ */
intelfric-technology / laravel-snippe example snippets
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(except: [
'snippe/*',
]);
})
Route::post('/webhooks/snippe', [SnippeWebhookController::class, 'handle'])
->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class);
use ShadrackJm\Snippe\Facades\Snippe;
// Mobile Money — USSD push sent to customer's phone
$payment = Snippe::mobileMoney(5000, '0754123456')
->customer('John Doe', '[email protected] ')
->send();
echo $payment->reference(); // "9015c155-9e29-..."
echo $payment->status(); // "pending"
use ShadrackJm\Snippe\Facades\Snippe;
$session = Snippe::session(50000)
->customer('John Doe', '[email protected] ')
->allowedMethods(['mobile_money', 'bank_transfer']) // restrict payment methods
->redirectTo(
route('subscription.success'),
route('subscription.cancel')
)
->webhook(route('snippe.webhook'))
->metadata(['subscription_id' => 42, 'plan_id' => 3, 'company_id' => 12])
->expiresIn(1800) // 30-minute checkout window
->send();
// Redirect the customer to Snippe's hosted checkout page
return redirect($session->checkoutUrl());
$session = Snippe::session()
->allowCustomAmount()
->customer('John Doe', '[email protected] ')
->redirectTo(route('topup.success'))
->send();
return redirect($session->checkoutUrl());
$payment = Snippe::mobileMoney(5000, '0754123456')
->customer('John Doe', '[email protected] ')
->webhook('https://yourapp.com/snippe/webhook')
->metadata(['order_id' => 'ORD-123'])
->description('Order #123 from My Shop')
->idempotencyKey('order-123-attempt-1')
->send();
// The customer receives a USSD push. You get a webhook when done.
$session = Snippe::session(100000)
->customer('John Doe', '[email protected] ')
->allowedMethods(['bank_transfer'])
->redirectTo(route('payment.success'), route('payment.cancel'))
->metadata(['invoice_id' => 'INV-001'])
->send();
return redirect($session->checkoutUrl());
$payment = Snippe::card(10000)
->phone('0754123456')
->customer('Jane Doe', '[email protected] ')
->billing('123 Main Street', 'Dar es Salaam', 'DSM', '14101', 'TZ')
->redirectTo('https://yourapp.com/success', 'https://yourapp.com/cancel')
->send();
// Redirect the customer to the secure checkout page
return redirect($payment->paymentUrl());
$payment = Snippe::qr(5000)
->customer('John Doe', '[email protected] ')
->redirectTo('https://yourapp.com/success', 'https://yourapp.com/cancel')
->send();
$qrData = $payment->qrCode(); // Render as a QR image
$paymentUrl = $payment->paymentUrl(); // Or redirect to hosted page
// Generic payment with a full array
$payment = Snippe::createPayment([
'payment_type' => 'mobile',
'details' => ['amount' => 5000, 'currency' => 'TZS'],
'phone_number' => '255754123456',
'customer' => ['firstname' => 'John', 'lastname' => 'Doe', 'email' => '[email protected] '],
]);
// Convenience wrapper for mobile payments
$payment = Snippe::initiateMobilePayment([
'amount' => 5000,
'phone_number' => '0754123456',
'customer' => ['firstname' => 'John', 'lastname' => 'Doe', 'email' => '[email protected] '],
]);
$payment = Snippe::verifyTransaction('9015c155-9e29-4e8e-8fe6-d5d81553c8e6');
// Or alias:
$payment = Snippe::find('9015c155-...');
echo $payment->status(); // "completed"
echo $payment->amount(); // 5000
echo $payment->completedAt(); // "2026-01-25T00:50:44Z"
$result = Snippe::payments(limit: 20, offset: 0);
$balance = Snippe::balance();
$available = $balance['data']['available']['value']; // e.g. 6943
$currency = $balance['data']['available']['currency']; // "TZS"
Snippe::push('payment-reference-uuid');
Snippe::push('payment-reference-uuid', '+255787654321'); // Different number
// In a service provider boot() method
Event::listen('snippe.payment.completed', function (array $data) {
// $data: reference, amount, currency, customer, metadata, payload
$subscription = Subscription::find($data['metadata']['subscription_id']);
$subscription->activate();
});
Event::listen('snippe.payment.failed', function (array $data) {
// Handle failure — notify user, cancel pending record, etc.
});
// routes/web.php
Route::post('/webhooks/snippe', [SnippeWebhookController::class, 'handle'])
->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class);
// app/Http/Controllers/SnippeWebhookController.php
namespace App\Http\Controllers;
use App\Models\PaymentTransaction;
use App\Models\Subscription;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use ShadrackJm\Snippe\Exceptions\SnippeException;
use ShadrackJm\Snippe\Webhook;
class SnippeWebhookController extends Controller
{
public function handle(Request $request): JsonResponse
{
try {
$event = Webhook::fromRaw($request->getContent(), $request->headers->all());
} catch (SnippeException $e) {
return response()->json(['error' => 'Invalid payload'], 400);
}
// Verify signature
$secret = config('snippe.webhook_secret');
if ($secret) {
try {
$event->verifyOrFail($secret);
} catch (SnippeException $e) {
return response()->json(['error' => 'Invalid signature'], 401);
}
}
$payload = $event->toArray();
if ($event->isPaymentCompleted()) {
$subscriptionId = $payload['data']['metadata']['subscription_id'] ?? null;
$subscription = Subscription::find($subscriptionId);
if ($subscription && $subscription->status === 'pending') {
// Record the transaction
PaymentTransaction::create([
'reference' => $payload['data']['reference'] ?? null,
'amount' => $payload['data']['amount']['value'] ?? 0,
'currency' => $payload['data']['amount']['currency'] ?? 'TZS',
'status' => 'completed',
'channel' => $payload['data']['channel']['type'] ?? null,
'provider' => $payload['data']['channel']['provider'] ?? null,
'customer_name' => $payload['data']['customer']['name'] ?? null,
'customer_phone' => $payload['data']['customer']['phone'] ?? null,
'metadata' => $payload,
'completed_at' => now(),
]);
// Auto-activate the subscription
$subscription->activate();
}
}
if ($event->isPaymentFailed()) {
$subscriptionId = $payload['data']['metadata']['subscription_id'] ?? null;
Subscription::where('id', $subscriptionId)
->where('status', 'pending')
->update(['status' => 'cancelled', 'cancelled_at' => now()]);
}
// Always return 200 so Snippe stops retrying
return response()->json(['status' => 'received']);
}
}
use ShadrackJm\Snippe\Webhook;
$event = Webhook::fromRaw($request->getContent(), $request->headers->all());
// Option A — throws SnippeException if invalid
$event->verifyOrFail(config('snippe.webhook_secret'));
// Option B — returns bool
if (! $event->verify(config('snippe.webhook_secret'))) {
return response()->json(['error' => 'Invalid signature'], 401);
}
use ShadrackJm\Snippe\Exceptions\SnippeException;
try {
$session = Snippe::session(50000)
->customer('John Doe', '[email protected] ')
->send();
} catch (SnippeException $e) {
$e->getMessage(); // "amount must be at least 500"
$e->getCode(); // 422 (HTTP status)
$e->getErrorCode(); // "validation_error"
$e->getResponse(); // full API error array
}
use Illuminate\Support\Facades\Http;
use ShadrackJm\Snippe\Facades\Snippe;
it('creates a payment session', function () {
Http::fake([
'*/sessions' => Http::response([
'data' => [
'reference' => 'sess_test123',
'status' => 'pending',
'checkout_url' => 'https://checkout.snippe.sh/sess_test123',
],
], 201),
]);
$session = Snippe::session(50000)
->customer('John Doe', '[email protected] ')
->allowedMethods(['mobile_money', 'bank_transfer'])
->send();
expect($session->isPending())->toBeTrue()
->and($session->checkoutUrl())->toContain('sess_test123');
});
// config/snippe.php
return [
// Your Snippe API key from the dashboard
'api_key' => env('SNIPPE_API_KEY', ''),
// Snippe API base URL — do not change unless testing against a sandbox
'base_url' => env('SNIPPE_BASE_URL', 'https://api.snippe.sh/v1'),
// The URL Snippe will POST webhook events to (must be publicly accessible)
'webhook_url' => env('SNIPPE_WEBHOOK_URL', null),
// Webhook signing secret for verifying event authenticity
'webhook_secret' => env('SNIPPE_WEBHOOK_SECRET', null),
// Route path for the built-in webhook endpoint
// Set to false to disable the built-in route and register your own
'webhook_path' => env('SNIPPE_WEBHOOK_PATH', 'snippe/webhook'),
// HTTP request timeout in seconds
'timeout' => env('SNIPPE_TIMEOUT', 30),
// Currency for all transactions (Tanzania: TZS)
'currency' => env('SNIPPE_CURRENCY', 'TZS'),
];
bash
php artisan vendor:publish --tag=snippe-config