Skip to content

Initialize Transaction

For customers to pay for a product or a service on you application, they need a payment page. Paystack allows you to generate such page (called authorization url) thought the api. An authorization url allows customers to securely enter their payment details and complete a transaction.

Making a Request

To generate the URL in you Laravel code, create an instance of InitializeRequestData and pass to Transaction::initialize,

php
use NjoguAmos\Paystack\Facades\Transaction;
use NjoguAmos\Paystack\Data\Transactions\InitializeRequestData;

$data = new InitializeRequestData(
    amount: 10000,
    email: '[email protected]'
);

$response = Transaction::initialize(data: $data);

$authorizationUrl = $response->authorization_url;
# e.g. https://checkout.paystack.com/3ni8kdavz62431k

Handling the Response

After payment completion, Paystack will redirect to your callback URL or the default URL set in your dashboard.

php
// Redirect the user to the payment page or render on an iframe
return redirect()->away($response->authorization_url);

// Or if you're using an API
return response()->json([
    'authorization_url' => $response->authorization_url,
    'access_code' => $response->access_code,
    'reference' => $response->reference,
]);

The authorization_url should render a payment page similar to this:

Paystack Payment page

Request Parameters

The request parameter must be an instance of \NjoguAmos\Paystack\Data\Transactions\InitializeRequestData and accepts the following parameters.

ParameterTypeRequiredDescription
amountintYesAmount in the smallest currency unit
emailstringYesCustomer's email address
referenceintYesUnique transaction reference. e.g Your transactions table primary key.
currencyCurrencyNoTransaction currency (defaults to your integration currency). Instance of NjoguAmos\Paystack\Enums\Currency
callback_urlstringNoURL to redirect to after payment. Overrides the callback URL set in your dashboard.
planstringNoIf transaction is for a subscription, provide the plan code here (invalidates the amount parameter)
invoice_limitintNoNumber of times to charge customer during subscription to plan
metadatastringNoStringified JSON object of custom data
channelsarrayNoPayment channels to make available. An array of NjoguAmos\Paystack\Enums\Currency
split_codestringNoThe split code for transaction split (e.g., SPL_98WF13Eb3w)
subaccountstringNoThe code for the subaccount that owns the payment (e.g., ACCT_8f4s1eq7ml6rlzj)
transaction_chargeintNoAmount to override the split configuration for a single split payment
bearerBearerNoWho bears the transaction charges. An instance of \NjoguAmos\Paystack\Enums\Bearer

IMPORTANT

The reference parameter can be used to verify a transaction later.

Response Parameters

The a successful response is an instance of \NjoguAmos\Paystack\Data\Transactions\InitializeResponseData and contains the following properties:

PropertyTypeDescription
authorization_urlstringURL to redirect the customer to for payment
access_codestringAccess code for the transaction
referencestringTransaction reference

Advanced Example

You can pass more supported field as defined in InitializeRequestData in the request.

php
use \NjoguAmos\Paystack\Enums\Bearer;
use NjoguAmos\Paystack\Enums\Currency;
use \NjoguAmos\Paystack\Enums\Channel;
use NjoguAmos\Paystack\Endpoints\Transaction
use NjoguAmos\Paystack\Data\Transactions\InitializeRequestData;

// Create request data with additional parameters
$data = new InitializeRequestData(
    amount: 50000,
    email: '[email protected]',
    reference: 5764789325239,
    currency: Currency::KES,
    callback_url: 'https://example.com/callback',
    channels: [Channel::MOBILE_MONEY, Channel::BANK],
    subaccount: 'acc_122',
    bearer: Bearer::SUB_ACCOUNT 
);

$transaction = Transaction::initialize(data: $data);

Released under the MIT License.