Paystack Split Payments in Laravel

Alemoh Rapheal B. Enike
4 min readOct 1, 2021

In this article, we’ll be learning how to handle the splitting of payments of an item between the owner of the item and the admin of an application (this is just an example there are countless places where split payment occurs sometimes across multiple accounts).

laravel new split_and_subscription_payments

or

composer create-project laravel/laravel split_and_subscription_payments

Change Directory

cd split_and_subscription_payments

We’ll be using Laravel Jetstream and tailwindcss for the user interface

Let’s setup jetstream

composer require laravel/jetstream

Database configuration

We’ll be using four tables for the purpose of this tutorial

  1. Users (owners of the packages)
  2. Accounts (Details of split code and percentage)
  3. Sales (history of purchased packages)
  4. Packages ( let’s say laravel packages for sale)
  5. Banks (List of banks with their names and unique code)

The users table already exist with a model in Laravel

Seed the users table with dummy data

php artisan make:seeder UserTableSeeder

To seed the database with a user account for easy testing

Add the following code to the UserTableSeeder.php file

Next let’s create accounts table with the model using the command below

The command above will create a model and migrations table.

Account.php under the models folder should look like this

Accounts migration table file

Next let’s create banks table, seeder and model

Next let’s create sales table and model

Assume this platform sells packages to users!!!

Next let’s create a packages table, model and seeder which is basically what the publishers are selling

The code for the PackageTableSeeder.php file

Add the following lines to DatabaseSeeder.php file

Next seed the data into the table

Next we’ll be using the Unicodeveloper paystack package for this tutorial

github.com/unicodeveloper/laravel-paystack awesome package by Prosper Otemuyiwa

Pretty straight forward to integrate!!!

Next let’s implement the following views and create their controllers respectively

Tailwindcss templates can be found here

The source code can be found in the repository

The subaccount controller has a method called subaccount that handles the form submission.

After the submission using the createSubaccount method from the paystack package the response contains the unique split code for the account to receive split payment.

$this->validate($request, [
'settlement_bank' => ['required', 'numeric'],
'account_number' => ['required', 'numeric'],
]);
try {//unique code for the subaccount .. kindly note you can use any unique code.
$reference = Paystack::genTranxRef();
Account::create([
'user_id' => auth()->user()->id,
'reference' => $reference,
]);
//merge the percentage, business name and metadata to the request
$request->merge([
'percentage_charge' => 10.5,
'business_name' => ucwords(Auth::user()->name),
'settlement_bank' => $request->settlement_bank,
'metadata' => ['reference' => $reference],
]);
//call the createsubaccount method
$create_subaccount = Paystack::createSubAccount();
//retrieve the response and update the account table
if ($create_subaccount['status']) {
if ($create_subaccount['data']['subaccount_code'] != null) {
Account::where(['reference' => $create_subaccount['data']['metadata']['reference']])
->update([
'subaccount_code' => $create_subaccount['data']['subaccount_code'],
'is_verified' => $create_subaccount['data']['is_verified'] ? 0 : 1,
]);
return redirect()->back()->with('notice', 'Subaccount created successfully!!!');
}
}

Next store the split code in the account table by updating the record

Next, retrieve the subaccount code of the package owner in the view under the split form file.

After successful payment record of the transaction when the user clicks on the buy now button

The user can now access the item while paystack sorts the splitting according to the percentage specified in the create subaccount section.

User can now access the package after purchase

This is the result of the split payment after successful package purchase by the user

This approach can be used in any language or framework for such use case of split payments.

Tutorial Repository

Thank you for reading this article.

Please kindly share with your network and feel free to use the comment section for questions, answers, and contributions.

Twitter alemsbaja

Originally published at https://alemsbaja.hashnode.dev.

--

--

Alemoh Rapheal B. Enike
Alemoh Rapheal B. Enike

Written by Alemoh Rapheal B. Enike

Software developer |writer | librarian | (Tech is easy to Learn) https://amazon.com/dp/B0B8T838K

No responses yet