1. Go to this page and download the library: Download nexuspay/payment-made-easy 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/ */
nexuspay / payment-made-easy example snippets
use NexusPay\PaymentMadeEasy\GatewayCapabilities;
use NexusPay\PaymentMadeEasy\Contracts\SubscriptionDriverInterface;
if (GatewayCapabilities::driverImplements('paystack', SubscriptionDriverInterface::class)) {
// safe to call subscription APIs
}
$slugs = GatewayCapabilities::gatewaysImplementing(SubscriptionDriverInterface::class);
use NexusPay\PaymentMadeEasy\Events\PaymentSuccessful;
use NexusPay\PaymentMadeEasy\Events\PaymentFailed;
use NexusPay\PaymentMadeEasy\Events\PaymentPending;
use NexusPay\PaymentMadeEasy\Events\RefundProcessed;
use NexusPay\PaymentMadeEasy\Events\SubscriptionCreated;
use NexusPay\PaymentMadeEasy\Events\SubscriptionCancelled;
use NexusPay\PaymentMadeEasy\Events\SubscriptionRenewed;
use NexusPay\PaymentMadeEasy\Events\TransferSuccessful;
use NexusPay\PaymentMadeEasy\Events\TransferFailed;
use NexusPay\PaymentMadeEasy\Events\DisputeCreated;
use NexusPay\PaymentMadeEasy\Events\ChargebackCreated;
protected $listen = [
PaymentSuccessful::class => [App\Listeners\HandleSuccessfulPayment::class],
PaymentFailed::class => [App\Listeners\HandleFailedPayment::class],
PaymentPending::class => [App\Listeners\HandlePendingPayment::class],
RefundProcessed::class => [App\Listeners\HandleRefundProcessed::class],
SubscriptionCreated::class => [App\Listeners\HandleSubscriptionCreated::class],
SubscriptionCancelled::class => [App\Listeners\HandleSubscriptionCancelled::class],
SubscriptionRenewed::class => [App\Listeners\HandleSubscriptionRenewed::class],
TransferSuccessful::class => [App\Listeners\HandleTransferSuccessful::class],
TransferFailed::class => [App\Listeners\HandleTransferFailed::class],
DisputeCreated::class => [App\Listeners\HandleDisputeCreated::class],
ChargebackCreated::class => [App\Listeners\HandleChargebackCreated::class],
];
// app/Listeners/HandleSuccessfulPayment.php
class HandleSuccessfulPayment
{
public function handle(PaymentSuccessful $event): void
{
$gateway = $event->webhookEvent->getGateway(); // e.g. 'paystack'
$eventType = $event->webhookEvent->getEventType(); // e.g. 'charge.success'
$data = $event->paymentData;
// Normalized keys commonly
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use NexusPay\PaymentMadeEasy\WebhookManager;
use NexusPay\PaymentMadeEasy\Exceptions\WebhookException;
class CustomWebhookController extends Controller
{
public function handle(Request $request, string $gateway, WebhookManager $webhookManager)
{
try {
$webhookManager->handle($gateway, $request);
return response()->json(['status' => 'success']);
} catch (WebhookException $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
}
}
use NexusPay\PaymentMadeEasy\Contracts\SubscriptionDriverInterface;
use NexusPay\PaymentMadeEasy\Contracts\DisbursementDriverInterface;
use NexusPay\PaymentMadeEasy\Contracts\VirtualAccountDriverInterface;
use NexusPay\PaymentMadeEasy\Contracts\PaymentLinkDriverInterface;
$driver = Payment::driver('paystack');
if ($driver instanceof SubscriptionDriverInterface) {
$driver->createPlan([...]);
}
if ($driver instanceof DisbursementDriverInterface) {
$driver->transfer([...]);
}
if ($driver instanceof VirtualAccountDriverInterface) {
$driver->createVirtualAccount([...]);
}
if ($driver instanceof PaymentLinkDriverInterface) {
$driver->createPaymentLink([...]);
}
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use NexusPay\PaymentMadeEasy\PaymentManager;
use NexusPay\PaymentMadeEasy\Contracts\SubscriptionDriverInterface;
class PaymentController extends Controller
{
public function __construct(private PaymentManager $paymentManager) {}
public function charge(Request $request)
{
$driver = $this->paymentManager->driver($request->gateway ?? 'paystack');
return $driver->initializePayment([
'email' => $request->email,
'amount' => $request->amount,
'reference' => $request->reference,
]);
}
public function createPlan(Request $request)
{
$driver = $this->paymentManager->driver($request->gateway ?? 'paystack');
if (!$driver instanceof SubscriptionDriverInterface) {
abort(422, 'Selected gateway does not support subscriptions.');
}
return $driver->createPlan($request->validated());
}
}
$order = Payment::driver('paypal')->initializePayment([
'amount' => 50.00,
'currency' => 'USD',
'email' => '[email protected]',
]);
// Redirect to $order['links'][n]['href'] where rel == 'approve'
// After approval, capture using the order ID:
Payment::driver('paypal')->verifyPayment($order['id']);
$response = Payment::driver('mpesa')->initializePayment([
'phone' => '254712345678', // international format, no +
'amount' => 500, // KES whole number
]);
$checkoutRequestId = $response['data']['checkout_request_id'];
// Poll or wait for callback, then verify:
Payment::driver('mpesa')->verifyPayment($checkoutRequestId);
// In your test
Payment::shouldReceive('driver->initializePayment')
->once()
->andReturn(['status' => true, 'data' => ['authorization_url' => 'https://...']]);
use NexusPay\PaymentMadeEasy\Services\PaymentRecorder;
$recorder = app(PaymentRecorder::class);
// After initializing a payment
$transaction = $recorder->recordTransaction('paystack', $requestData, $gatewayResponse);
// After verifying / receiving a webhook
$recorder->updateTransactionStatus('ORDER_001', 'successful', 'PS_GATEWAY_REF');
// Record a transfer
$transfer = $recorder->recordTransfer('paystack', $transferData, $transferResponse);
// Log a webhook event for auditing
$recorder->logWebhookEvent('paystack', $webhookEvent);
// Refunds (e.g. after a refund webhook) — appends `payment_refunds` rows and updates parent status
// (partially_refunded vs refunded from summed amounts). Optional 6th arg: provider refund id for dedupe.
$recorder->handleRefundWebhook('paystack', 'ORDER_001', 50.0, 'NGN', $rawPayload, 're_abc123');
// Attach a transaction to a user
$transaction->payable()->associate($user)->save();
// Scope helpers
PaymentTransaction::successful()->gateway('paystack')->get();
PaymentSubscription::active()->forEmail('[email protected]')->get();
PaymentTransfer::successful()->gateway('mtnmomo')->get();