PHP code example of musheabdulhakim / paystack

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

    

musheabdulhakim / paystack example snippets




usheAbdulHakim\Paystack\Paystack;

// Replace with your actual Paystack secret key
$secretKey = 'sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Initialize the Paystack client
$client = Paystack::client($secretKey);

// Example: Initialize a new transaction
try {
    $response = $client->transaction()->initialize([
        'amount' => 500000, // Amount in kobo (e.g., NGN 5000.00)
        'email' => '[email protected]',
        'callback_url' => '[https://yourwebsite.com/verify-payment](https://yourwebsite.com/verify-payment)',
    ]);

    echo "Transaction initialized successfully! Authorization URL: " . $response['authorization_url'] . "\n";
} catch (\Exception $e) {
    echo "Error initializing transaction: " . $e->getMessage() . "\n";
}

// Example: List all customers
try {
    $customers = $client->customer()->list(['perPage' => 5]);
    echo "Customers:\n";
    print_r($customers);
} catch (\Exception $e) {
    echo "Error listing customers: " . $e->getMessage() . "\n";
}




usheAbdulHakim\Paystack\Paystack;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

// Replace with your actual Paystack secret key
$secretKey = 'sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Example of a dummy PSR-18 HTTP Client (replace with a real implementation like Guzzle)
class MyCustomHttpClient implements ClientInterface {
    public function sendRequest(\Psr\Http\Message\RequestInterface $request): \Psr\Http\Message\ResponseInterface {
        // Dummy response for demonstration
        return new \GuzzleHttp\Psr7\Response(200, [], '{"status": true, "message": "Custom client response"}');
    }
}

// Example of a dummy PSR-17 Request Factory
class MyCustomRequestFactory implements RequestFactoryInterface {
    public function createRequest(string $method, \Psr\Http\Message\UriInterface|string $uri): \Psr\Http\Message\RequestInterface {
        return new \GuzzleHttp\Psr7\Request($method, $uri);
    }
}

// Example of a dummy PSR-17 Stream Factory
class MyCustomStreamFactory implements StreamFactoryInterface {
    public function createStream(string $content = ''): \Psr\Http\Message\StreamInterface {
        return \GuzzleHttp\Psr7\Utils::streamFor($content);
    }
}

$client = Paystack::factory()
    ->withSecretKey($secretKey)
    ->withBaseUri('[https://api.paystack.com/](https://api.paystack.com/)') // Set a custom base URI
    ->withHeaders([ // Add default headers to all requests
        'X-Custom-Header' => 'My-App-Identifier',
        'User-Agent' => 'MyCustomPaystackClient/1.0',
    ])
    ->withQueryParameters(['perPage' => 50]) // Add default query parameters
    ->withHttpClient(new MyCustomHttpClient()) // Provide your custom HTTP client
    ->withRequestFactory(new MyCustomRequestFactory()) // Provide your custom request factory
    ->withStreamFactory(new MyCustomStreamFactory()) // Provide your custom stream factory
    ->make();

// Now, all requests made through $client will use your custom configurations.
try {
    $response = $client->miscellaneous()->banks(['country' => 'nigeria']);
    echo "Nigerian Banks (via custom client):\n";
    print_r($response);
} catch (\Exception $e) {
    echo "Error fetching banks: " . $e->getMessage() . "\n";
}