PHP code example of paynecta / paynecta-laravel-sdk
1. Go to this page and download the library: Download paynecta/paynecta-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/ */
use Paynecta\LaravelSdk\Facades\Paynecta;
// Get all payment links
$links = Paynecta::paymentLinks()->getAll();
// Get specific link
$link = Paynecta::paymentLinks()->get('ABC123');
// Get only invoices
$invoices = Paynecta::paymentLinks()->getInvoices();
// Search links
$results = Paynecta::paymentLinks()->search('subscription');
// Count total links
$count = Paynecta::paymentLinks()->count();
use Paynecta\LaravelSdk\Facades\Paynecta;
// Initialize payment
$payment = Paynecta::payments()->initialize(
'ABC123', // Payment link code
'254700000000', // Mobile number
100 // Amount in KES (1-250,000)
);
// With validation
$payment = Paynecta::payments()->initializeWithValidation(
'ABC123',
'0700000000', // Accepts 07XX format
500
);
// Get transaction reference
$reference = Paynecta::payments()->getTransactionReference($payment);
echo "Transaction Reference: {$reference}";
use Paynecta\LaravelSdk\Facades\Paynecta;
// Query status
$status = Paynecta::payments()->queryStatus('ABCP20240803123456ABCD');
// Check status
if (Paynecta::payments()->isCompleted($status)) {
$receipt = Paynecta::payments()->getMpesaReceiptNumber($status);
echo "Payment completed! Receipt: {$receipt}";
}
if (Paynecta::payments()->isFailed($status)) {
$reason = Paynecta::payments()->getFailureReason($status);
echo "Payment failed: {$reason}";
}
// Poll until complete (checks every 2 seconds, max 30 attempts)
$finalStatus = Paynecta::payments()->pollStatus($reference, 30, 2);
use Paynecta\LaravelSdk\Facades\Paynecta;
// Get all currency rates (160+ currencies)
$rates = Paynecta::currencyRates()->getAll();
// Get specific currency rate
$usdRate = Paynecta::currencyRates()->get('USD');
// Convert currency
$converted = Paynecta::currencyRates()->convert(100, 'USD', 'KES');
// Convert with specific date
$converted = Paynecta::currencyRates()->convert(100, 'USD', 'KES', '2025-10-01');
// Get historical rates
$history = Paynecta::currencyRates()->getHistory('KES', 'USD', '2025-10-01', '2025-10-13');
// Helper methods
$rate = Paynecta::currencyRates()->getRate($usdRate);
$amount = Paynecta::currencyRates()->getConvertedAmount($converted);
$allRates = Paynecta::currencyRates()->getRates($rates);
use Paynecta\LaravelSdk\Facades\Paynecta;
// Get all banks
$banks = Paynecta::banks()->getAll();
// Get specific bank
$bank = Paynecta::banks()->get('jR3kL9');
// Search banks
$results = Paynecta::banks()->search('equity');
// Find by name
$kcb = Paynecta::banks()->findByName('KCB Bank');
// Find by paybill
$bank = Paynecta::banks()->findByPaybill('247247');
// For dropdowns (returns bank_id => bank_name)
$options = Paynecta::banks()->getForDropdown();
// Simple list (returns bank_name => paybill_number)
$list = Paynecta::banks()->getAllAsList();
// Grouped by first letter
$grouped = Paynecta::banks()->getGroupedByLetter();
protected $except = [
'paynecta/webhook',
];
namespace App\Listeners;
use Paynecta\LaravelSdk\Events\PaymentCompletedEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
class HandlePaymentCompleted implements ShouldQueue
{
public function handle(PaymentCompletedEvent $event)
{
// Access payment data
$reference = $event->transactionReference;
$amount = $event->amount;
$receipt = $event->mpesaReceiptNumber;
$mobile = $event->mobileNumber;
// Update database
\DB::table('payments')
->where('transaction_reference', $reference)
->update([
'status' => 'completed',
'mpesa_receipt' => $receipt,
'paid_at' => now()
]);
// Send confirmation, fulfill order, etc.
}
}
use Paynecta\LaravelSdk\Events\PaymentCompletedEvent;
use Paynecta\LaravelSdk\Events\PaymentFailedEvent;
use Paynecta\LaravelSdk\Events\PaymentCancelledEvent;
protected $listen = [
PaymentCompletedEvent::class => [
HandlePaymentCompleted::class,
],
PaymentFailedEvent::class => [
HandlePaymentFailed::class,
],
PaymentCancelledEvent::class => [
HandlePaymentCancelled::class,
],
];
use Paynecta\LaravelSdk\PaynectaClient;
class PaymentController extends Controller
{
public function __construct(
protected PaynectaClient $paynecta
) {}
public function processPayment()
{
$user = $this->paynecta->verifyAuth();
$links = $this->paynecta->paymentLinks()->getAll();
}
}
$user = Paynecta::setTimeout(60)->verifyAuth();
// In .env
PAYNECTA_LOGGING=true
// Or programmatically
Paynecta::setLogging(true)->verifyAuth();