Skip to content

Usage

The package provides a convenient global helper function to interact with the Paystack API.

Helper Function

You can use the global paystack() helper function to access the service. This is the recommended way to use the package.

php
// Get the service instance
$paystack = paystack();

// Access resources directly
$transactions = paystack()->transaction()->list();

Facade

Alternatively, if you prefer Facades, you can use the Paystack facade.

php
use Turndale\Paystack\Facades\Paystack;

// Access resources
$transactions = Paystack::transaction()->list();

Available Resources

The following resources are available via both the helper and the Facade:

  • transaction()
  • plan()
  • subscription()
  • customer()
  • page()
  • product()
  • paymentRequest()
  • settlement()
  • transferRecipient()
  • transfer()
  • transferControl()
  • charge()
  • dispute()
  • refund()
  • verification()
  • miscellaneous()

Exception Handling

The package throws a Turndale\Paystack\Exceptions\PaystackException when an API error occurs. This exception contains useful information to help you debug or handle the error gracefully.

php
use Turndale\Paystack\Exceptions\PaystackException;
use Turndale\Paystack\Facades\Paystack;

try {
    $response = Paystack::transaction()->initialize([
        'email' => '[email protected]',
        'amount' => 5000
    ]);
} catch (PaystackException $e) {
    // The error message from Paystack
    $message = $e->getMessage();

    // The HTTP status code (e.g., 400, 401, 404)
    $code = $e->getCode();

    // The type of error (e.g., 'api_error', 'validation_error')
    $type = $e->getType();

    // The specific Paystack error code, if available
    $paystackCode = $e->getPaystackCode();

    // Additional metadata about the error
    $meta = $e->getMeta();

    // Suggested next step, if provided
    $nextStep = $e->getNextStep();

    // Log or handle the error
    logger()->error("Paystack Error: {$message}", ['meta' => $meta]);

    return back()->withErrors(['payment' => $message]);
} catch (\Exception $e) {
    // Handle other generic exceptions
}

Released under the MIT License.