You can find all paystack's endpoints at https://developers.paystack.co/reference
composer require sdkcodes/lara-paystack to install the latest version of LaraPaystackIn order to collect payments with paystack from your customers, this is how the flow generally looks like:
$paystack = new PaystackService(), and immediately start to use all methods offered by the SDK.public function talkToPaystack(Request $request, PaystackService $paystack){
// Laravel automatically does the instantiation for you through its Service Container
// and you can start to use the paystack object in your method
}
$paystack->initializeTransaction($data)->redirectNow();
Below is a sample route and controller that uses the LaraPaystack package to make and verify payment
<?php
//web.php
Route::post("/submit-payment-request", "PaymentController@initializeTransaction");
Route::get("/payment-callback", "PaymentController@respondToCallback");
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sdkcodes\LaraPaystack\PaystackService;
class PaymentController{
public function initializeTransaction(Request $request, PaystackService $paystack){
$data = [
'amount' => 10000 // should be in kobo: this can come from a form submitted by the user
'email' => $request->email,
'callback_url' => url('route-to-handle-paystack-callback')
];
// You can check this link for all fields that you can send to paystack https://developers.paystack.co/v1.0/reference#initialize-a-transaction
// this line will initialize transaction with paystack and automatically help you redirect to the payment url returned by paystack
$paystack->initializeTransaction($data)->redirectNow();
}
/**
* Controller method to handle response from paystack after payment
* @param trxref
*/
public function respondToCallback(Request $request, PaystackService $paystack){
//sent to us by paystack after successful payment
$transRef = $request->trxref;
// this will verify if the transaction is valid and someone is not just trying to play funny with our payment
$paystack->verifyTransaction($transRef);
//you can now do anything you want with the paymentData
$paymentData = $paystack->getPaymentData();
}
}
As it stands, here are some of the things you can do with this package:
I plan to keep updating the package and as time permits, cover all of Paystack's endpoints.
You're welcome to contribute as well.
LaraPaystack requires some environment variables be set, and it uses the appropriate paystack key based on your enviroment
PAYSTACK_TEST_SECRET_KEY will be automatically used if your APP_ENV matches any of the following values: local, test, stagingThe PAYSTACK_LIVE_SECRET_KEY will be automatically used if your APP_ENV matches any of the following values: production
However, you can easily override the environment values by setting a USE_ENV_AS variable in your .env
USE_ENV_AS can have either values of production to simulate production environment or testing to simulate test environment
To use the package for Paystack's bulk transfer, you need to first disable OTP from your paystack dashboard.
You will use the doBulkTransfer(array $recipients, string $currency="NGN", string $source="balance") method to carry out bulk transfer.
Note: Only the first argument to the method is required
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sdkcodes\LaraPaystack\PaystackService;
class PaymentController{
/**
* Initiate bulk transfer and send money to multiple bank accounts at the same time
*/
public function doMultipleTransfers(Request $request, PaystackService $paystack){
// You can check this link for all fields that you can send to paystack https://developers.paystack.co/reference#initiate-bulk-transfer
$people = array(
[
'amount' => 50000,
'recipient' => 'RCP_m9yzgv4tbi6f20b'
],
[
'amount' => 20000,
'recipient' => 'RCP_z6b9zeky5z379dn'
]
);
$response = $paystack->initiateBulkTransfer($people);
}
}
Complete documentation
This package was highly inspired by @Unicodeveloper's Laravel Paystack package
The package is as free as a bird.
A star of the repo would highly be appreciated
How can I help you explore Laravel packages today?