Skip to the content.

How to build an E-commerce website with Laravel PayHere

← back

ApiChef Laravel PayHere package provides more features than what is explain in the guide, such as easy subscriptions API, payment details retrieval. You can find the full package documentation here

I have done few things befe I start this guide.


## OKii. We are ready to get started.


#### Step 1. Installation

$ composer require apichef/laravel-pay-here

Step 2. Publish migrations and config

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

Step 3. Run migrations

$ php artisan migrate

Step 4. Configur credentials

You need to set the following envirnoment variables:



Merchant ID is issued per account by PayHere, You can copy it from your PayHere settings page.

To obtain a merchant secret, you need to add a domain to allowed domains/apps under the Domains & Credentials tab

You must create a business application to consume other PayHere services. Go to the Business Apps tab and create app.

NOTE: Tick the following permission

Step 5. Implement checkout and redirect route

Route::get('/checkout/success', 'CheckoutController@success')

Route::get('/checkout/cancelled', 'CheckoutController@cancelled')

Route::get('/checkout/{product}', 'CheckoutController@show')

Step 6. Implement checkout controller

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('payment', $payment);

    public function success(Request $request)
        $payment = Payment::findByOrderId($request->get('order_id'));
        /** @var Product $product */
        $product = $payment->payable;
        // perform the side effects of successful payment
        // redirect to payment success page

    public function cancelled(Request $request)
        $payment = Payment::findByOrderId($request->get('order_id'));
        /** @var Product $product */
        $product = $payment->payable;

        // perform the side effects of cancelled payment

        // redirect to payment cancelled page

Step 7. Implement checkout form

Example form tag

Step 8. Query purchased items.

You can query the purchased items from the Payment model using paidBy and success scopes.

$products = Payment::query()

You can find the source code of this example in this repository.

← back