Outsource Laravel Development Partner - $3200/Month | Bacancy

PayHere for Laravel

Published on by

PayHere for Laravel image

PayHere is a widely used payment gateway service in Sri Lanka. The Laravel PayHere plugin provides seamless integration of PayHere into Laravel applications, allowing developers to access all PayHere APIs easily. This package supports various payment types, making it an essential solution for anyone looking to enhance their application's payment processing capabilities.

Main Features

  • Onsite and Offsite Checkout Flows
  • Dispatches events for received webhooks, managing payments, and managing subscriptions.
  • Implement the PayHereCustomer interface with your User model to facilitate payments from authenticated users in your Laravel application.
  • Provides a Billable trait that you can also use in your User model with methods to perform common billing tasks.
  • An Intermediate RESTful API that allows for communication with the PayHere RESTful API. This is especially useful for mobile apps that need to integrate with PayHere.
  • An HTML form-based API builder to help you generate the necessary HTML inputs based on your configuration and redirect your users to the payment gateway to complete the transaction.
  • It also includes a built-in Filament panel, making it easy to manage payments and subscriptions right in your Laravel application.

You can install PayHere using Composer:

composer require laravel-payhere/laravel-payhere

Finally, you may run the following command to publish assets and migrations:

php artisan payhere:install

If you plan to use the PayHere panel, you should run the following command to publish the Filament assets:

php artisan filament:assets

You will need to ensure you have an account with PayHere and that you get the relevant API Keys and Merchant ID's from the PayHere portal and add them to your .env file:

PAYHERE_MERCHANT_ID=your-payhere-merchant-id
PAYHERE_MERCHANT_SECRET=-your-payhere-merchant-secret
PAYHERE_APP_ID=your-payhere-app-id
PAYHERE_APP_SECRET=your-payhere-app-secret

There are also additional environment variables for the default currency, notify url and return url allowing further customisation of the integration with the payment gateway.

Basic Example

Here, we define a basic route for a user to checkout:

use App\Http\Controllers\Checkout;
 
Route::get('/checkout', Checkout::class);

The corresponding controller could look similar to the following:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Routing\Controller;
use PayHere\PayHere;
 
class Checkout extends Controller
{
public function __invoke()
{
return PayHere::builder()
->guest()
->title("Bold 'LN' Logo Retro Trucker Hat")
->amount(25)
->checkout();
}
}

When this route is visited, it will redirect the user to PayHere to complete the payment. Once completed, they will be redirected back to your site or to a return URL you define using the PAYHERE_RETURN_URL environment variable in your .env file.

Suppose you prefer to have an Onsite checkout experience for your users. In that case, Laravel PayHere allows you to load the PayHere JavaScript SDK to your Blade template using the @payhereJS Blade directive right before your application layout's closing </head> tag. There is also a Blade component available to add Payment Buttons. An example would be:

<x-payhere::button
:order="PayHere\PayHere::builder()->guest()->amount(100)->getOrder()"
class="px-8 py-4"
>
Buy Now
</x-payhere::button>

Discover more about this package and its features in the official documentation, and take a look at the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
A new beta of Laravel Wayfinder just dropped image

A new beta of Laravel Wayfinder just dropped

Read article
Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026 image

Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026

Read article
Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development image

Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development

Read article
JSON:API Resource in Laravel 12.45 image

JSON:API Resource in Laravel 12.45

Read article
Caching With MongoDB for Faster Laravel Apps image

Caching With MongoDB for Faster Laravel Apps

Read article
Laravel 12.44 Adds HTTP Client afterResponse() Callbacks image

Laravel 12.44 Adds HTTP Client afterResponse() Callbacks

Read article