How to build an E-commerce website with Laravel PayHere
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.
- craete a laravel project
$ composer create-project laravel/laravel sri-commerce
- Install larave tailwindcss preset
$ composer require laravel-frontend-presets/tailwindcss --dev
- Generate UI scaffolding login & registration
$ php artisan ui tailwindcss --auth
- Add Nunito font to tailwind config
module.exports = { // ... theme: { // ... fontFamily: { serif: ['Nunito', 'sans-serif'], } }, // ... }
- Link Nunito font in the heder section of
resources/views/layouts/app.blade.php
```
<br/>
## OKii. We are ready to get started.
<br/>
#### Step 1. Installation
```bash
$ 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:
PAY_HERE_MERCHANT_ID=
PAY_HERE_MERCHANT_SECRET=
PAY_HERE_BUSINESS_APP_ID=
PAY_HERE_BUSINESS_APP_SECRET=
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
- Payment Retrieval API
- Subscription Management API (only if you use subscriptions)
Step 5. Implement checkout and redirect route
Route::get('/checkout/success', 'CheckoutController@success')
->name('checkout.success');
Route::get('/checkout/cancelled', 'CheckoutController@cancelled')
->name('checkout.cancelled');
Route::get('/checkout/{product}', 'CheckoutController@show')
->name('checkout');
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
Step 8. Query purchased items.
You can query the purchased items from the Payment model using paidBy and success scopes.
$products = Payment::query()
->with('payable')
->paidBy(Auth::user())
->success()
->get()
->map->payable;
You can find the source code of this example in this repository.