Paystack Split Payments in Laravel

Paystack Split Payments in Laravel

Integration of paystack package to handle split payments in laravel application

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 number of 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
npm install
npm run dev

Database configuration

image.png

php artisan migrate
php artisan serve

image.png

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

 php artisan make:model Accountm

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

php artisan make:model Bank -m

banks_table.jpg

image.png

Next let's create sales table and model

php artisan make:model Sale -m

image.png

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

php artisan make:model Package -m

image.png

php artisan make:seeder PackagesTableSeeder

The code for the PackageTableSeeder.php file

Add the following lines to DatabaseSeeder.php file

        $this->call(UserTableSeeder::class);
        $this->call(PackagesTableSeeder::class);
        $this->call(BanksTableSeeder::class);
php artisan migrate

Next seed the data into the table

php artisan db:seed

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

  1. Navbar modification

image.png

The source code can be found in the repository

  1. Display all packages (Free and Paid)

image.png

  1. Add create subaccount form (the account paystack will split the payment into)

image.png

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!!!');
                    }
                }

image.png

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

image.png

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

image.png

image.png

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

image.png

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

image.png

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

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!