PHP code example of dawilly / laravel-clickpesa
1. Go to this page and download the library: Download dawilly/laravel-clickpesa 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/ */
dawilly / laravel-clickpesa example snippets
use Dawilly\Dawilly\Facades\Clickpesa;
// Preview available payment methods
$preview = Clickpesa::previewUssdPushRequest([
'amount' => '10000',
'currency' => 'TZS',
'orderReference' => 'ORDER-123',
'phoneNumber' => '255712345678',
'fetchSenderDetails' => true
]);
// Response:
// {
// "activeMethods": [
// {
// "name": "TIGO-PESA",
// "status": "AVAILABLE",
// "fee": 500,
// "message": "Service available"
// }
// ],
// "sender": {
// "accountName": "John Doe",
// "accountNumber": "255712345678",
// "accountProvider": "TIGO-PESA"
// }
// }
// Initiate the payment
$payment = Clickpesa::initiateUssdPushRequest([
'amount' => '10000',
'currency' => 'TZS',
'orderReference' => 'ORDER-123',
'phoneNumber' => '255712345678'
]);
// Response:
// {
// "id": "txn_abc123xyz",
// "status": "PROCESSING",
// "channel": "TIGO-PESA",
// "orderReference": "ORDER-123",
// "collectedAmount": "10000",
// "collectedCurrency": "TZS",
// "createdAt": "2023-11-07T05:31:56Z",
// "clientId": "your-client-id"
// }
// Preview card payment availability
$preview = Clickpesa::previewCardPayment([
'amount' => '100',
'currency' => 'USD',
'orderReference' => 'CARD-001'
]);
// Initiate card payment (returns payment link)
$cardPayment = Clickpesa::initiateCardPayment([
'amount' => '100',
'currency' => 'USD',
'orderReference' => 'CARD-001',
'customer' => [
'id' => 'existing-customer-id'
// OR for new customers:
// 'firstName' => 'John',
// 'lastName' => 'Doe',
// 'email' => '[email protected] ',
// 'phoneNumber' => '255712345678'
]
]);
// Response:
// {
// "cardPaymentLink": "https://pay.clickpesa.com/card/abc123xyz",
// "clientId": "your-client-id"
// }
// Redirect customer to the payment link
// Redirect::away($cardPayment['cardPaymentLink']);
// Check payment status by order reference
$status = Clickpesa::queryPaymentStatus('ORDER-123');
// Response:
// [
// {
// "id": "txn_abc123xyz",
// "status": "SUCCESS",
// "paymentReference": "PAY-XYZ-789",
// "orderReference": "ORDER-123",
// "collectedAmount": 10000,
// "collectedCurrency": "TZS",
// "message": "Payment completed successfully",
// "updatedAt": "2023-11-07T05:31:56Z",
// "createdAt": "2023-11-07T05:31:56Z",
// "customer": {
// "customerName": "John Doe",
// "customerPhoneNumber": "255712345678",
// "customerEmail": "[email protected] "
// },
// "clientId": "your-client-id"
// }
// ]
use Dawilly\Dawilly\Facades\Disbursement;
use Dawilly\Dawilly\Facades\Clickpesa;
// Ensure authentication token is generated
Clickpesa::generateToken();
// Preview mobile money payout
$preview = Disbursement::previewMobileMoneyPayout([
'amount' => 10000,
'phoneNumber' => '255712345678',
'currency' => 'TZS',
'orderReference' => 'PAYOUT-001'
]);
// Response:
// {
// "amount": 10500.00,
// "balance": 50000,
// "channelProvider": "TIGO PESA",
// "fee": 500,
// "exchanged": false,
// "order": {
// "amount": 10000,
// "currency": "TZS",
// "id": "PAYOUT-001"
// },
// "payoutFeeBearer": "merchant",
// "receiver": {
// "accountName": "Jane Doe",
// "accountNumber": "255712345678",
// "accountCurrency": "TZS",
// "amount": 10000
// }
// }
// Execute mobile money payout
$payout = Disbursement::createMobileMoneyPayout([
'amount' => 10000,
'phoneNumber' => '255712345678',
'currency' => 'TZS',
'orderReference' => 'PAYOUT-001'
]);
// Response:
// {
// "id": "payout_abc123xyz",
// "createdAt": "2023-11-07T05:31:56Z",
// "updatedAt": "2023-11-07T05:31:56Z",
// "orderReference": "PAYOUT-001",
// "amount": "10500.00",
// "currency": "TZS",
// "fee": "500.00",
// "exchanged": false,
// "status": "AUTHORIZED",
// "channel": "MOBILE MONEY",
// "channelProvider": "TIGO PESA",
// "beneficiary": {
// "accountNumber": "255712345678",
// "accountName": "Jane Doe",
// "amount": "10000.00"
// },
// "clientId": "your-client-id"
// }
// Preview bank payout
$preview = Disbursement::previewBankPayout([
'amount' => 20000,
'accountNumber' => '123456789',
'currency' => 'TZS',
'orderReference' => 'BANK-001',
'bic' => 'EQTZTZTZ',
'transferType' => 'ACH'
]);
// Response:
// {
// "amount": 22360.00,
// "balance": 50000,
// "channelProvider": "Equity Bank Tanzania Limited",
// "fee": 2360,
// "exchanged": false,
// "order": {
// "amount": 20000,
// "currency": "TZS",
// "id": "BANK-001"
// },
// "payoutFeeBearer": "merchant",
// "receiver": {
// "accountNumber": "123456789",
// "accountCurrency": "TZS",
// "amount": 20000
// },
// "transferType": "ACH"
// }
// Execute bank payout
$bankPayout = Disbursement::createBankPayout([
'amount' => 20000,
'accountNumber' => '123456789',
'accountName' => 'John Doe',
'currency' => 'TZS',
'orderReference' => 'BANK-001',
'bic' => 'EQTZTZTZ',
'transferType' => 'ACH' // or 'RTGS'
]);
// Response:
// {
// "id": "payout_xyz789abc",
// "createdAt": "2023-11-07T05:31:56Z",
// "updatedAt": "2023-11-07T05:31:56Z",
// "orderReference": "BANK-001",
// "amount": "22360.00",
// "currency": "TZS",
// "fee": "2360.00",
// "exchanged": false,
// "status": "AUTHORIZED",
// "channel": "BANK TRANSFER",
// "channelProvider": "Equity Bank Tanzania Limited",
// "transferType": "ACH",
// "beneficiary": {
// "accountNumber": "123456789",
// "accountName": "John Doe",
// "amount": "20000.00"
// },
// "clientId": "your-client-id"
// }
// Check payout status by order reference
$payoutStatus = Disbursement::queryPayoutStatus('PAYOUT-001');
// Response:
// [
// {
// "id": "payout_abc123xyz",
// "status": "SUCCESS",
// "orderReference": "PAYOUT-001",
// "amount": "10500.00",
// "currency": "TZS",
// "fee": "500.00",
// "channel": "MOBILE MONEY",
// "channelProvider": "TIGO PESA",
// "createdAt": "2023-11-07T05:31:56Z",
// "updatedAt": "2023-11-07T05:31:56Z",
// "beneficiary": {
// "accountNumber": "255712345678",
// "accountName": "Jane Doe"
// },
// "clientId": "your-client-id"
// }
// ]
use Dawilly\Dawilly\Events\PaymentReceived;
use Illuminate\Support\Facades\Event;
Event::listen(PaymentReceived::class, function (PaymentReceived $event) {
$paymentData = $event->paymentData;
// Update your order status
// Send confirmation email
// Log transaction
});
$response = Clickpesa::initiateUssdPushRequest([...]);
if (isset($response['success']) && $response['success'] === false) {
// Handle error
$message = $response['message'];
$details = $response['response'] ?? null;
// Log error
Log::error('Clickpesa Error', $response);
}
bash
php artisan vendor:publish --tag=clickpesa-config
bash
php artisan vendor:publish --tag=clickpesa-migrations
bash
php artisan vendor:publish --provider="Dawilly\\Dawilly\\ClickpesaServiceProvider"
bash
php artisan migrate
bash
php artisan test