Skip to the content.

Laravel PayHere

← back


PayHere is the leading payment service provider for Sri Lanka.



Installation

$ composer require apichef/laravel-pay-here

Publish migrations and config

$ php artisan vendor:publish --provider="ApiChef\PayHere\PayHereServiceProvider"

Run migrations

$ php artisan migrate



Configurations

return [
    /*
    |--------------------------------------------------------------------------
    | Base Url
    |--------------------------------------------------------------------------
    |
    | This is where you can configure PayHere base url, based on the
    | application environment. On production, the value should set to
    | https://www.payhere.lk/
    |
    */

    'base_url' => env('PAY_HERE_BASE_URL', 'https://sandbox.payhere.lk/'),

    /*
    |--------------------------------------------------------------------------
    | Merchant Credentials
    |--------------------------------------------------------------------------
    |
    | Merchant ID is issued per account by PayHere, You can copy it from your
    | PayHere settings page in the Domains & Credentials tab.
    |
    | To obtain a merchant secret, you need to add a domain to allowed
    | domains/apps.
    |
    */

    'merchant_credentials' => [
        'id' => env('PAY_HERE_MERCHANT_ID'),
        'secret' => env('PAY_HERE_MERCHANT_SECRET'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Business App Credentials
    |--------------------------------------------------------------------------
    |
    | You must create a business application to call other PayHere services. Go 
    | to the Business Apps tab and create an app.
    |
    | NOTE: Tick the following permission
    | - Payment Retrieval API
    | - Subscription Management API (only if you use subscriptions)
    |
    */

    'business_app_credentials' => [
        'id' => env('PAY_HERE_BUSINESS_APP_ID'),
        'secret' => env('PAY_HERE_BUSINESS_APP_SECRET'),
    ],

    /*
    |--------------------------------------------------------------------------
    | PayHere Database Connection
    |--------------------------------------------------------------------------
    |
    | This is the database connection you want PayHere to use while storing &
    | reading your payment data. By default PayHere assumes you use your
    | default connection. However, you can change that to anything you want.
    |
    */

    'database_connection' => env('DB_CONNECTION'),

    /*
    |--------------------------------------------------------------------------
    | PayHere Middleware
    |--------------------------------------------------------------------------
    |
    | This is the middleware group that PayHere payment notification webhook
    | and redirect on success/canceled routes uses.
    |
    */

    'middleware' => env('PAY_HERE_MIDDLEWARE', []),
]



Create a payable instance

When you are implementing the checkout form you need to pass a payable (payment or a subscription) instance. To do this package provides make static method in the Payment and Subscription classes.


payment example

namespace App\Http\Controllers;

use ApiChef\PayHere\Payment;
use App\Product;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function show(Product $product, Request $request)
    {
        $payment = Payment::make($product, $request->user(), $product->price);

        return view('checkout')
            ->with('payable', $payment);
    }
}


subscription example

namespace App\Http\Controllers;

use ApiChef\PayHere\Subscription;
use App\Package;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function show(Package $package, Request $request)
    {
        $subscription = Subscription::make(
            $package, 
            $request->user(), 
            '1 Month', 
            'Forever', 
            $package->price
        );

        return view('checkout')
            ->with('payable', $subscription);
    }
}



Checkout form

Package provides x-pay-here-checkout-form blade component to make it easy to implemnt checkout form.


It accepts 4 parametrs

Example blade code



Querying payable (Payment or Subscription) models by order_id

PayHere redirects the user back to the application with order_id GET parameter on successful or cancllled payment.


In this controlle you nee to fetch payable instance by order_id. To do this package provides findByOrderId static methon in both Payment and Subscription models.


namespace App\Http\Controllers;

use ApiChef\PayHere\Payment;
use App\Product;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function success(Request $request)
    {
        $payment = Payment::findByOrderId($request->get('order_id'));
        
        // ...
    }
}



Quering Payment Model


Payment model has payable relationship to get the item paid for.

$item = Payment::find(10)->payable;


payer relationship allows you to get the buyer of the item.

$buyer = Payment::find(10)->payer;


To fetch all the payments of a buyer, you can call paidBy cope.

$myPayments = Payment::paidBy(Auth::user())->get();


If you need to fetch all the payments made for an item, you can query paidFor scope.

$allPaymentsForTheBook = Payment::paidFor(Book::find(10))->get();


If you need to fetch all successful payments, you can query success scope.

$allSuccessfulPayments = Payment::success()->get();


PayHere redirects user to your website on success or cancled checkout with order_id GET parameter. You can use findByOrderId method to find the payment by order_id.

$payments = Payment::findByOrderId($request->get('order_id'));


You can combine the above scopes and query:

$mySuccessfulPayments = Payment::paidBy(Auth::user())->success()->get();



Quering Subscription Model


Subscription model has subscribable relationship to get the item paid for.

$plan = Subscription::find(10)->subscribable;


payer relationship allows you to get the subscriber of the item.

$subscriber = Subscription::find(10)->payer;


To fetch all the subscriptions of a user, you can call paidBy cope.

$mySubscriptions = Subscription::paidBy(Auth::user())->get();


If you need to fetch all the subscriptions for an item, you can query subscribedTo scope.

$allSubscriptionsForGoldPlan = Subscription::subscribedTo(Plan::find(1))->get();


If you need to fetch all active subscriptions, you can query active scope.

$allActiveSubscriptions = Subscription::active()->get();


PayHere redirects user to your website on recurring payment sumition success or cancled with order_id GET parameter. You can use findByOrderId method to find the subscription by order_id.

$subscription = Subscription::findByOrderId($request->get('order_id'));


You can combine the above scopes and query:

$myActiveSubscriptions = Subscription::paidBy(Auth::user())->active()->get();



Querying Payment Details

documentation will be updated soon


Example form implementation


← back