PHP code example of starfolksoftware / paystack-php

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

    

starfolksoftware / paystack-php example snippets





StarfolkSoftware\Paystack\Client as PaystackClient;

// Initialize the client
$paystack = new PaystackClient([
    'secretKey' => 'sk_test_your_secret_key_here',
]);

// List all transactions
$transactions = $paystack->transactions->all([
    'perPage' => 50,
    'page' => 1
]);

echo "Total transactions: " . $transactions['meta']['total'] . "\n";

// Create a customer
$customer = $paystack->customers->create([
    'email' => '[email protected]',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'phone' => '+2348123456789'
]);

echo "Customer created: " . $customer['data']['customer_code'] . "\n";

// Create a payment request
$paymentRequest = $paystack->paymentRequests->create([
    'description' => 'Website Development Invoice',
    'line_items' => [
        ['name' => 'Frontend Development', 'amount' => 50000, 'quantity' => 1],
        ['name' => 'Backend Development', 'amount' => 75000, 'quantity' => 1],
        ['name' => 'UI/UX Design', 'amount' => 25000, 'quantity' => 1]
    ],
    'tax' => [
        ['name' => 'VAT', 'amount' => 11250] // 7.5% of total
    ],
    'customer' => '[email protected]',
    'due_date' => date('Y-m-d', strtotime('+30 days')),
    'send_notification' => true
]);

// List payment requests
$paymentRequests = $paystack->paymentRequests->all([
    'perPage' => 20,
    'page' => 1,
    'status' => 'pending'
]);

// Fetch specific payment request
$paymentRequest = $paystack->paymentRequests->fetch('PRQ_1weqqsn2wwzgft8');

// Update payment request
$updated = $paystack->paymentRequests->update('PRQ_1weqqsn2wwzgft8', [
    'description' => 'Updated Website Development Invoice',
    'due_date' => date('Y-m-d', strtotime('+45 days'))
]);

// Send reminder notification
$notification = $paystack->paymentRequests->sendNotification('PRQ_1weqqsn2wwzgft8');

// Create a plan
$plan = $paystack->plans->create([
    'name' => 'Premium Monthly',
    'interval' => 'monthly',
    'amount' => 5000, // ₦50.00 per month
    'currency' => 'NGN',
    'description' => 'Premium subscription with all features'
]);

// Create subscription
$subscription = $paystack->subscriptions->create([
    'customer' => 'CUS_xwaj0txjryg393b',
    'plan' => $plan['data']['plan_code'],
    'authorization' => 'AUTH_authorization_code'
]);

// List subscriptions
$subscriptions = $paystack->subscriptions->all([
    'perPage' => 50,
    'plan' => $plan['data']['plan_code']
]);

// Disable subscription
$disabled = $paystack->subscriptions->disable('SUB_subscription_code', [
    'code' => 'SUB_subscription_code',
    'token' => 'subscription_email_token'
]);

use StarfolkSoftware\Paystack\Client as PaystackClient;

try {
    $paystack = new PaystackClient([
        'secretKey' => 'sk_test_your_secret_key_here',
    ]);
    
    $transaction = $paystack->transactions->initialize([
        'email' => 'invalid-email', // This will cause an error
        'amount' => 20000
    ]);
    
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    // Network or HTTP-related errors
    echo "HTTP Error: " . $e->getMessage() . "\n";
    
} catch (\Exception $e) {
    // General errors
    echo "Error: " . $e->getMessage() . "\n";
}

// Handle API response errors
$response = $paystack->transactions->verify('invalid_reference');
if (!$response['status']) {
    echo "API Error: " . $response['message'] . "\n";
    // Handle the error appropriately
}

$paystack = new PaystackClient([
    'secretKey' => 'sk_test_your_secret_key_here',
    'apiVersion' => 'v1', // API version (default: v1)
    'baseUri' => 'https://api.paystack.co', // Custom base URI
]);

// Access the underlying HTTP client if needed
$httpClient = $paystack->getHttpClient();

// In your webhook endpoint
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] ?? '';

// Verify webhook signature
$secretKey = 'sk_test_your_secret_key_here';
$computedSignature = hash_hmac('sha512', $payload, $secretKey);

if (hash_equals($signature, $computedSignature)) {
    $event = json_decode($payload, true);
    
    switch ($event['event']) {
        case 'charge.success':
            // Handle successful payment
            $reference = $event['data']['reference'];
            echo "Payment successful: {$reference}\n";
            break;
            
        case 'subscription.create':
            // Handle new subscription
            $subscriptionCode = $event['data']['subscription_code'];
            echo "New subscription: {$subscriptionCode}\n";
            break;
            
        default:
            echo "Unhandled event: {$event['event']}\n";
    }
} else {
    echo "Invalid signature\n";
    http_response_code(400);
}

// Use test secret key
$paystack = new PaystackClient([
    'secretKey' => 'sk_test_your_test_secret_key_here',
]);

// Test card numbers for different scenarios
$testCards = [
    'success' => '4084084084084081',
    'insufficient_funds' => '4084084084084107',
    'invalid_pin' => '4084084084084099'
];
bash
composer 
bash
# Clone the repository
git clone https://github.com/starfolksoftware/paystack-php.git
cd paystack-php

# Install dependencies
composer install

# Run tests
composer test