PHP code example of chainbook / paystack

1. Go to this page and download the library: Download chainbook/paystack 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/ */

    

chainbook / paystack example snippets


use Chainbook\Paystack\Facades\Paystack;

// Initialize a transaction
$response = Paystack::initializeTransaction([
    'email'        => '[email protected]',
    'amount'       => 500000, // Amount in kobo/pesewas (= 5000 NGN/GHS)
    'reference'    => uniqid('ref_'),
    'callback_url' => 'https://yourapp.com/payment/callback',
]);

// $response['data']['authorization_url'] — redirect the user here

// Verify a transaction
$verification = Paystack::verifyTransaction('ref_abc123');

if ($verification['data']['status'] === 'success') {
    // Payment confirmed
}

// List transactions
$transactions = Paystack::listTransactions(['perPage' => 20, 'page' => 1]);

// Fetch a specific transaction
$transaction = Paystack::fetchTransaction(123456789);

// Charge a returning customer using saved authorization
$charge = Paystack::chargeAuthorization([
    'authorization_code' => 'AUTH_xxxxxxxx',
    'email'              => '[email protected]',
    'amount'             => 500000,
]);

// Create a customer
$customer = Paystack::createCustomer([
    'email'      => '[email protected]',
    'first_name' => 'Jane',
    'last_name'  => 'Doe',
    'phone'      => '+2348012345678',
]);

// Fetch a customer (by ID or customer code)
$customer = Paystack::fetchCustomer('CUS_xxxxxxxx');

// Update a customer
Paystack::updateCustomer('CUS_xxxxxxxx', ['first_name' => 'Janet']);

// List customers
$customers = Paystack::listCustomers(['perPage' => 50]);

// Create a plan
$plan = Paystack::createPlan([
    'name'     => 'Monthly Premium',
    'interval' => 'monthly',
    'amount'   => 500000,
]);

// List plans / fetch / subscribe
$plans         = Paystack::listPlans();
$plan          = Paystack::fetchPlan('PLN_xxxxxxxx');
$subscription  = Paystack::createSubscription([
    'customer'   => 'CUS_xxxxxxxx',
    'plan'       => 'PLN_xxxxxxxx',
    'start_date' => now()->addDay()->toIso8601String(),
]);
$subscriptions = Paystack::listSubscriptions();

// routes/web.php
Route::post('/webhooks/paystack', function (Illuminate\Http\Request $request) {
    $signature = $request->header('X-Paystack-Signature');
    $computed  = hash_hmac('sha512', $request->getContent(), config('paystack.secret_key'));

    if ($signature !== $computed) {
        abort(400, 'Invalid signature');
    }

    $event = $request->json()->all();

    match ($event['event'] ?? null) {
        'charge.success'   => /* handle payment */ null,
        'transfer.success' => /* handle transfer */ null,
        default            => null,
    };

    return response('OK', 200);
})->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);

$publicKey = Paystack::getPublicKey();

return [
    'public_key'     => env('PAYSTACK_PUBLIC_KEY'),
    'secret_key'     => env('PAYSTACK_SECRET_KEY'),
    'base_url'       => env('PAYSTACK_BASE_URL', 'https://api.paystack.co'),
    'webhook_secret' => env('PAYSTACK_WEBHOOK_SECRET'),
];
bash
php artisan vendor:publish --provider="Chainbook\Paystack\PaystackServiceProvider" --tag="paystack-config"