PHP code example of paynexus / laravel-paynexus
1. Go to this page and download the library: Download paynexus/laravel-paynexus 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/ */
paynexus / laravel-paynexus example snippets
use PayNexus\Facades\PayNexus;
$result = PayNexus::initiatePayment([
'amount' => 1000,
'phone' => '254712345678',
'description' => 'Order #123',
]);
if ($result['success']) {
return redirect()->route('payment.status', $result['data']['checkout_request_id']);
}
use PayNexus\Facades\PayNexus;
use PayNexus\Models\PaynexusPayment;
// Create order
$order = Order::create([...]);
// Initiate payment
$result = PayNexus::initiatePayment([
'amount' => $order->total,
'phone' => $request->phone,
'description' => "Order {$order->order_number}",
]);
// Link payment to order
if ($result['success']) {
$payment = PaynexusPayment::where('checkout_request_id', $result['data']['checkout_request_id'])->first();
$payment->update([
'payable_type' => Order::class,
'payable_id' => $order->id,
]);
}
$result = PayNexus::initiatePayment([
'amount' => 1000,
'phone' => $user->phone,
'description' => 'Monthly subscription',
]);
if ($result['success']) {
$payment = PaynexusPayment::where('checkout_request_id', $result['data']['checkout_request_id'])->first();
$payment->update([
'payable_type' => Subscription::class,
'payable_id' => $subscription->id,
]);
}
// Create invoice
$invoice = PayNexus::createInvoice([
'customer_name' => 'John Doe',
'customer_email' => '[email protected] ',
'amount' => 5000,
'line_items' => [
['description' => 'Consulting', 'amount' => 5000],
],
]);
// Send invoice
if ($invoice['success']) {
PayNexus::sendInvoice($invoice['data']['id']);
}
use PayNexus\Events\PaymentCompleted;
use PayNexus\Events\PaymentFailed;
protected $listen = [
PaymentCompleted::class => [
\App\Listeners\HandlePaymentSuccess::class,
],
PaymentFailed::class => [
\App\Listeners\HandlePaymentFailure::class,
],
];
use PayNexus\Models\PaynexusPayment;
// Find payment
$payment = PaynexusPayment::where('reference', 'PNX123')->first();
// Check status
$payment->isPending();
$payment->isCompleted();
$payment->isFailed();
// Mark as verified (admin review)
$payment->markVerified(1500.00, '254712345678', 'bank_statement');
// Initiate payment
PayNexus::initiatePayment(['amount' => 1000, 'phone' => '254712345678', 'description' => '...']);
PayNexus::initiateMpesaPayment(['amount' => 1000, 'phone' => '254712345678', 'description' => '...']);
// Check status
PayNexus::getPaymentByReference('PNX123');
PayNexus::getPaymentById(42);
PayNexus::getPaymentByCheckoutId('ws_CO_...');
PayNexus::checkMpesaStatus('ws_CO_...');
PayNexus::pollStatus('ws_CO_...');
// List payments
PayNexus::listPayments(['status' => 'completed', 'from_date' => '2026-01-01']);
PayNexus::createInvoice(['customer_name' => 'John', 'amount' => 5000, ...]);
PayNexus::getInvoice(123);
PayNexus::listInvoices(['status' => 'pending']);
PayNexus::updateInvoice(123, ['status' => 'sent']);
PayNexus::deleteInvoice(123);
PayNexus::sendInvoice(123);
PayNexus::getReceipt(123);
PayNexus::listReceipts(['payment_id' => 456]);
PayNexus::resendReceipt(123);
PayNexus::createCheckoutSession([
'amount' => 1000,
'customer_email' => '[email protected] ',
'success_url' => 'https://yourapp.com/success',
'cancel_url' => 'https://yourapp.com/cancel',
]);
PayNexus::getMerchant();
PayNexus::getBusinesses();
PayNexus::getPaymentAccounts();
PayNexus::registerWebhook('My App', 'https://yourapp.com/paynexus/webhook', ['payment.completed']);
PayNexus::listWebhooks();
PayNexus::updateWebhook(1, ['active' => false]);
PayNexus::deleteWebhook(1);
PayNexus::validatePhone('0712345678');
// Returns: ['valid' => true, 'normalized' => '254712345678']
use Illuminate\Support\Facades\Http;
Http::fake([
'paynexus.co.ke/*' => Http::response([
'success' => true,
'data' => [
'payment_id' => 123,
'reference' => 'PNXTEST',
'checkout_request_id' => 'ws_CO_test',
],
]),
]);
$result = PayNexus::initiatePayment([
'amount' => 1000,
'phone' => '254712345678',
]);
use PayNexus\Exceptions\PayNexusAuthException;
use PayNexus\Exceptions\PayNexusConnectionException;
use PayNexus\Exceptions\PayNexusApiException;
try {
$result = PayNexus::initiatePayment([...]);
if (!$result['success']) {
return back()->with('error', $result['message']);
}
} catch (PayNexusAuthException $e) {
// Invalid API key
} catch (PayNexusConnectionException $e) {
// Network error
} catch (PayNexusApiException $e) {
// API error
}
bash
php artisan vendor:publish --tag=paynexus-config
php artisan vendor:publish --tag=paynexus-migrations
php artisan migrate