Installation
composer require sdkcodes/lara-paystack
Publish the config file (optional, but recommended for customization):
php artisan vendor:publish --provider="Sdkcodes\LaraPaystack\LaraPaystackServiceProvider"
Configure .env
Add your Paystack API keys:
PAYSTACK_SECRET_KEY=your_secret_key
PAYSTACK_PUBLIC_KEY=your_public_key
First Use Case: Initialize a Transaction In a controller or service, initialize a transaction with minimal data:
use Sdkcodes\LaraPaystack\Facades\LaraPaystack;
$paymentData = [
'email' => 'customer@example.com',
'amount' => 10000, // Amount in kobo (10000 = ₦100)
'reference' => 'UNIQUE-' . time(),
'callback_url' => route('payment.callback'),
];
$response = LaraPaystack::initialize($paymentData);
return redirect()->away($response['data']['authorization_url']);
Verify Callback Handle the Paystack callback in a route:
Route::post('/payment/callback', [PaymentController::class, 'handleCallback']);
public function handleCallback(Request $request)
{
$data = $request->all();
$response = LaraPaystack::verifyTransaction($data['reference']);
return view('payment.success', ['response' => $response]);
}
$response = LaraPaystack::initialize([
'email' => 'user@example.com',
'amount' => 50000, // ₦500
'reference' => 'REF-' . Str::uuid(),
'callback_url' => route('callback'),
'metadata' => ['custom_field' => 'value'],
]);
return redirect()->away($response['data']['authorization_url']);
$response = LaraPaystack::verifyTransaction($reference);
if ($response['status']) {
// Payment successful
$transaction = $response['data'];
}
public function handleWebhook(Request $request)
{
$event = $request->event;
$data = $request->data;
if (LaraPaystack::verifyWebhook($event, $data)) {
// Process the event (e.g., charge.success, transfer.received)
}
}
$plan = LaraPaystack::createPlan([
'name' => 'Premium Subscription',
'amount' => 20000, // ₦200
'interval' => 'monthly',
'currency' => 'NGN',
]);
$subscription = LaraPaystack::subscribeCustomer([
'customer' => $customerId, // From Paystack
'plan' => $plan['data']['code'],
'start_date' => now()->addDay(),
]);
$customer = LaraPaystack::createCustomer([
'email' => 'user@example.com',
'first_name' => 'John',
'last_name' => 'Doe',
]);
$customers = LaraPaystack::listCustomers(['perPage' => 10]);
$transfer = LaraPaystack::transfer([
'source' => 'balance', // or 'account'
'amount' => 10000,
'reason' => 'Refund',
'recipient' => 'recipient_code_or_account_number',
'reference' => 'TRANS-' . time(),
]);
Bind the package to a service container for dependency injection:
// In a service provider
$this->app->bind(
\Sdkcodes\LaraPaystack\LaraPaystack::class,
function ($app) {
return new \Sdkcodes\LaraPaystack\LaraPaystack(
$app->make('config')->get('larapaystack')
);
}
);
Protect payment routes with middleware to ensure only authenticated users can initiate transactions:
Route::middleware(['auth'])->group(function () {
Route::post('/init-payment', [PaymentController::class, 'initiate']);
});
Log Paystack API responses for debugging:
$response = LaraPaystack::initialize($data);
\Log::info('Paystack Response', ['response' => $response]);
Use mocking to test Paystack interactions:
$this->mock(LaraPaystack::class)->shouldReceive('initialize')
->once()
->andReturn(['status' => true, 'data' => ['authorization_url' => 'https://test.com']]);
$amountInNaira = 100; // ₦100
$amountInKobo = $amountInNaira * 100; // 10000
$callbackUrl = url('/payment/callback');
verifyWebhook(), but ensure you use it:
if (!LaraPaystack::verifyWebhook($event, $data)) {
abort(403, 'Invalid webhook signature');
}
use Illuminate\Support\Facades\Http;
$response = Http::withOptions(['timeout' => 30])
->retry(3, 100)
->post('https://api.paystack.co/transaction/initialize', $data);
// Extend LaraPaystack class
namespace App\Services;
use Sdkcodes\LaraPaystack\LaraPaystack as BaseLaraPaystack;
class LaraPaystack extends BaseLaraPaystack
{
public function newEndpoint($data)
{
return $this->post('new-endpoint', $data);
}
}
Add this to your .env to log API requests/responses:
LARAPAYSTACK_DEBUG=true
Dump the raw response from Paystack to debug issues:
$response = LaraPaystack::initialize($data);
dd($response); // Check for errors in 'message' or 'status'
Use Paystack’s test mode with test cards (e.g., 5431748765432105) to avoid real charges during development.
invalid_reference: Duplicate or invalid reference.insufficient_balance: Insufficient funds in Paystack account.invalid_signature: Webhook signature verification failed.Override headers globally in the config (config/larapaystack.php):
'headers' => [
'Accept' => 'application/json
How can I help you explore Laravel packages today?