PHP code example of blaaiz / blaaiz-laravel-sdk
1. Go to this page and download the library: Download blaaiz/blaaiz-laravel-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/ */
blaaiz / blaaiz-laravel-sdk example snippets
// Add to your .env file
BLAAIZ_API_KEY=your-api-key-here
BLAAIZ_BASE_URL=https://api-dev.blaaiz.com // For development
// BLAAIZ_BASE_URL=https://api.blaaiz.com // For production
// Publish configuration (optional)
php artisan vendor:publish --tag=blaaiz-config
// Test the connection
use Blaaiz\LaravelSdk\Facades\Blaaiz;
$isConnected = Blaaiz::testConnection();
echo $isConnected ? 'API Connected' : 'Connection Failed';
use Blaaiz\LaravelSdk\Facades\Blaaiz;
$customer = Blaaiz::customers()->create([
'first_name' => 'John',
'last_name' => 'Doe',
'type' => 'individual', // or 'business'
'email' => '[email protected] ',
'country' => 'NG',
'id_type' => 'passport', // drivers_license, passport, id_card, resident_permit
'id_number' => 'A12345678',
// 'business_name' => 'Company Name' // Required if type is 'business'
]);
echo 'Customer ID: ' . $customer['data']['data']['id'];
$customer = Blaaiz::customers()->get('customer-id');
echo 'Customer: ' . json_encode($customer['data']);
$customers = Blaaiz::customers()->list();
echo 'Customers: ' . json_encode($customers['data']);
$updatedCustomer = Blaaiz::customers()->update('customer-id', [
'first_name' => 'Jane',
'email' => '[email protected] '
]);
// Option A: Upload from file path
$result = Blaaiz::customers()->uploadFileComplete('customer-id', [
'file' => file_get_contents('/path/to/passport.jpg'),
'file_category' => 'identity', // identity, proof_of_address, liveness_check
'filename' => 'passport.jpg', // Optional
'content_type' => 'image/jpeg' // Optional
]);
// Option B: Upload from Base64 string
$result = Blaaiz::customers()->uploadFileComplete('customer-id', [
'file' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
'file_category' => 'identity'
]);
// Option C: Upload from Data URL (with automatic content type detection)
$result = Blaaiz::customers()->uploadFileComplete('customer-id', [
'file' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
'file_category' => 'identity'
]);
// Option D: Upload from Public URL (automatically downloads and uploads)
$result = Blaaiz::customers()->uploadFileComplete('customer-id', [
'file' => 'https://example.com/documents/passport.jpg',
'file_category' => 'identity'
]);
echo 'Upload complete: ' . json_encode($result['data']);
echo 'File ID: ' . $result['file_id'];
// Step 1: Get pre-signed URL
$presignedUrl = Blaaiz::files()->getPresignedUrl([
'customer_id' => 'customer-id',
'file_category' => 'identity' // identity, proof_of_address, liveness_check
]);
// Step 2: Upload file to the pre-signed URL (implement your file upload logic)
// Use Guzzle or any HTTP client to upload the file
// Step 3: Associate file with customer
$fileAssociation = Blaaiz::customers()->uploadFiles('customer-id', [
'id_file' => $presignedUrl['data']['file_id'] // Use the file_id from step 1
]);
$collection = Blaaiz::collections()->initiate([
'method' => 'open_banking',
'amount' => 100.00,
'customer_id' => 'customer-id',
'wallet_id' => 'wallet-id',
'phone' => '+1234567890' // Optional
]);
echo 'Payment URL: ' . $collection['data']['url'];
echo 'Transaction ID: ' . $collection['data']['transaction_id'];
$collection = Blaaiz::collections()->initiate([
'method' => 'card',
'amount' => 5000,
'customer_id' => 'customer-id',
'wallet_id' => 'wallet-id'
]);
echo 'Payment URL: ' . $collection['data']['url'];
// Get available networks
$networks = Blaaiz::collections()->getCryptoNetworks();
echo 'Available networks: ' . json_encode($networks['data']);
// Initiate crypto collection
$cryptoCollection = Blaaiz::collections()->initiateCrypto([
'amount' => 100,
'network' => 'ethereum',
'token' => 'USDT',
'wallet_id' => 'wallet-id'
]);
$attachment = Blaaiz::collections()->attachCustomer([
'customer_id' => 'customer-id',
'transaction_id' => 'transaction-id'
]);
$payout = Blaaiz::payouts()->initiate([
'wallet_id' => 'wallet-id',
'customer_id' => 'customer-id',
'method' => 'bank_transfer',
'from_amount' => 1000,
'from_currency_id' => '1', // NGN
'to_currency_id' => '1', // NGN
'account_number' => '0123456789',
'bank_id' => '1', // Required for NGN
'phone_number' => '+2348012345678'
]);
echo 'Payout Status: ' . $payout['data']['transaction']['status'];
$interacPayout = Blaaiz::payouts()->initiate([
'wallet_id' => 'wallet-id',
'customer_id' => 'customer-id',
'method' => 'interac',
'from_amount' => 100,
'from_currency_id' => '2', // CAD
'to_currency_id' => '2', // CAD
'email' => '[email protected] ',
'interac_first_name' => 'John',
'interac_last_name' => 'Doe'
]);
$vba = Blaaiz::virtualBankAccounts()->create([
'wallet_id' => 'wallet-id',
'account_name' => 'John Doe'
]);
echo 'Account Number: ' . $vba['data']['account_number'];
echo 'Bank Name: ' . $vba['data']['bank_name'];
$vbas = Blaaiz::virtualBankAccounts()->list('wallet-id');
echo 'Virtual Accounts: ' . json_encode($vbas['data']);
$wallets = Blaaiz::wallets()->list();
echo 'Wallets: ' . json_encode($wallets['data']);
$wallet = Blaaiz::wallets()->get('wallet-id');
echo 'Wallet Balance: ' . $wallet['data']['balance'];
$transactions = Blaaiz::transactions()->list([
'page' => 1,
'limit' => 10,
'status' => 'SUCCESSFUL' // Optional filter
]);
echo 'Transactions: ' . json_encode($transactions['data']);
$transaction = Blaaiz::transactions()->get('transaction-id');
echo 'Transaction: ' . json_encode($transaction['data']);
$banks = Blaaiz::banks()->list();
echo 'Available Banks: ' . json_encode($banks['data']);
$accountInfo = Blaaiz::banks()->lookupAccount([
'account_number' => '0123456789',
'bank_id' => '1'
]);
echo 'Account Name: ' . $accountInfo['data']['account_name'];
$currencies = Blaaiz::currencies()->list();
echo 'Supported Currencies: ' . json_encode($currencies['data']);
$feeBreakdown = Blaaiz::fees()->getBreakdown([
'from_currency_id' => '1', // NGN
'to_currency_id' => '2', // CAD
'from_amount' => 100000
]);
echo 'You send: ' . $feeBreakdown['data']['you_send'];
echo 'Recipient gets: ' . $feeBreakdown['data']['recipient_gets'];
echo 'Total fees: ' . $feeBreakdown['data']['total_fees'];
$webhook = Blaaiz::webhooks()->register([
'collection_url' => 'https://your-domain.com/webhooks/collection',
'payout_url' => 'https://your-domain.com/webhooks/payout'
]);
$webhookConfig = Blaaiz::webhooks()->get();
echo 'Webhook URLs: ' . json_encode($webhookConfig['data']);
$replay = Blaaiz::webhooks()->replay([
'transaction_id' => 'transaction-id'
]);
$completePayoutResult = Blaaiz::createCompletePayout([
'customer_data' => [
'first_name' => 'John',
'last_name' => 'Doe',
'type' => 'individual',
'email' => '[email protected] ',
'country' => 'NG',
'id_type' => 'passport',
'id_number' => 'A12345678'
],
'payout_data' => [
'wallet_id' => 'wallet-id',
'method' => 'bank_transfer',
'from_amount' => 1000,
'from_currency_id' => '1',
'to_currency_id' => '1',
'account_number' => '0123456789',
'bank_id' => '1',
'phone_number' => '+2348012345678'
]
]);
echo 'Customer ID: ' . $completePayoutResult['customer_id'];
echo 'Payout: ' . json_encode($completePayoutResult['payout']);
echo 'Fees: ' . json_encode($completePayoutResult['fees']);
$completeCollectionResult = Blaaiz::createCompleteCollection([
'customer_data' => [
'first_name' => 'Jane',
'last_name' => 'Smith',
'type' => 'individual',
'email' => '[email protected] ',
'country' => 'NG',
'id_type' => 'drivers_license',
'id_number' => 'ABC123456'
],
'collection_data' => [
'method' => 'card',
'amount' => 5000,
'wallet_id' => 'wallet-id'
],
'create_vba' => true // Optionally create a virtual bank account
]);
echo 'Customer ID: ' . $completeCollectionResult['customer_id'];
echo 'Collection: ' . json_encode($completeCollectionResult['collection']);
echo 'Virtual Account: ' . json_encode($completeCollectionResult['virtual_account']);
namespace App\Http\Controllers;
use Blaaiz\LaravelSdk\Blaaiz;
use Blaaiz\LaravelSdk\Exceptions\BlaaizException;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
public function __construct(private Blaaiz $blaaiz)
{
}
public function createPayout(Request $request)
{
try {
$payout = $this->blaaiz->payouts()->initiate([
'wallet_id' => $request->input('wallet_id'),
'method' => 'bank_transfer',
'from_amount' => $request->input('amount'),
'from_currency_id' => $request->input('from_currency_id'),
'to_currency_id' => $request->input('to_currency_id'),
'account_number' => $request->input('account_number'),
'bank_id' => $request->input('bank_id')
]);
return response()->json($payout);
} catch (BlaaizException $e) {
return response()->json([
'error' => $e->getMessage(),
'status' => $e->getStatus(),
'error_code' => $e->getErrorCode()
], $e->getStatus() ?: 500);
}
}
}
use Blaaiz\LaravelSdk\Exceptions\BlaaizException;
try {
$customer = Blaaiz::customers()->create($invalidData);
} catch (BlaaizException $e) {
echo 'Blaaiz API Error: ' . $e->getMessage();
echo 'Status Code: ' . $e->getStatus();
echo 'Error Code: ' . $e->getErrorCode();
// Check error type
if ($e->isClientError()) {
// 4xx errors - client/validation issues
echo 'Client error detected';
} elseif ($e->isServerError()) {
// 5xx errors - server issues
echo 'Server error detected';
}
// Get array representation
$errorArray = $e->toArray();
Log::error('Blaaiz API Error', $errorArray);
}
use Blaaiz\LaravelSdk\Facades\Blaaiz;
// Method 1: Verify signature manually
$isValid = Blaaiz::webhooks()->verifySignature(
$payload, // Raw webhook payload (string or array)
$signature, // Signature from webhook headers
$webhookSecret // Your webhook secret key
);
if ($isValid) {
echo 'Webhook signature is valid';
} else {
echo 'Invalid webhook signature';
}
// Method 2: Construct verified event (recommended)
try {
$event = Blaaiz::webhooks()->constructEvent(
$payload, // Raw webhook payload
$signature, // Signature from webhook headers
$webhookSecret // Your webhook secret key
);
echo 'Verified event: ' . json_encode($event);
// $event['verified'] will be true
// $event['timestamp'] will contain verification timestamp
} catch (BlaaizException $e) {
echo 'Webhook verification failed: ' . $e->getMessage();
}
namespace App\Http\Controllers;
use Blaaiz\LaravelSdk\Facades\Blaaiz;
use Blaaiz\LaravelSdk\Exceptions\BlaaizException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
class WebhookController extends Controller
{
private string $webhookSecret;
public function __construct()
{
// Get webhook secret from config or environment
$this->webhookSecret = config('blaaiz.webhook_secret') ?: env('BLAAIZ_WEBHOOK_SECRET');
}
public function collection(Request $request): Response
{
$signature = $request->header('x-blaaiz-signature');
$payload = $request->getContent();
try {
// Verify webhook signature and construct event
$event = Blaaiz::webhooks()->constructEvent($payload, $signature, $this->webhookSecret);
Log::info('Verified collection event', [
'transaction_id' => $event['transaction_id'],
'status' => $event['status'],
'amount' => $event['amount'],
'currency' => $event['currency'],
'verified' => $event['verified']
]);
// Process the collection
// Update your database, send notifications, etc.
return response()->json(['received' => true]);
} catch (BlaaizException $e) {
Log::error('Webhook verification failed', ['error' => $e->getMessage()]);
return response()->json(['error' => 'Invalid signature'], 400);
}
}
public function payout(Request $request): Response
{
$signature = $request->header('x-blaaiz-signature');
$payload = $request->getContent();
try {
// Verify webhook signature and construct event
$event = Blaaiz::webhooks()->constructEvent($payload, $signature, $this->webhookSecret);
Log::info('Verified payout event', [
'transaction_id' => $event['transaction_id'],
'status' => $event['status'],
'recipient' => $event['recipient'],
'verified' => $event['verified']
]);
// Process the payout completion
// Update your database, send notifications, etc.
return response()->json(['received' => true]);
} catch (BlaaizException $e) {
Log::error('Webhook verification failed', ['error' => $e->getMessage()]);
return response()->json(['error' => 'Invalid signature'], 400);
}
}
}
use App\Http\Controllers\WebhookController;
// Webhook routes (disable CSRF protection for these routes)
Route::post('/webhooks/collection', [WebhookController::class, 'collection'])->name('webhooks.collection');
Route::post('/webhooks/payout', [WebhookController::class, 'payout'])->name('webhooks.payout');
protected $except = [
'webhooks/*',
];
return [
'api_key' => env('BLAAIZ_API_KEY'),
'base_url' => env('BLAAIZ_BASE_URL', 'https://api-dev.blaaiz.com'),
'timeout' => env('BLAAIZ_TIMEOUT', 30),
'webhook_secret' => env('BLAAIZ_WEBHOOK_SECRET'),
];