Puspa Subedi 4 years ago

Esewa Payment Integration in Laravel 8

Blog image

In this article, I will show you how to integrate eSewa API with Laravel 8. Majority of eCommerce platform in Nepal use this API.

I have made a normal eCommerce like page and implement the eSewa API.

Code

The sample code is available on github example repository.

Firstly, I have created the fresh laravel project with command 

composer create-project --pref-dist laravel\laravel PaymentIntegration

Then we will created a database from xampp phpmyadmin and Connect the database file with laravel project.

After that we will create the model and controller for product and order as 

After that we will create the table products and order and edit the column and refresh the migration as


fig: Edit Column for products.


fig: Refresh Migration 

Same process is done for the order table.

EsewaController to Handle Esewa response and Verification This Handling process is fully given in the eSewa official API Documentation.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Order;

class EsewaController extends Controller
{
    public function sucess(Request $request)
    {
        if($request->oid && $request->amt &&$request->refId)
        {
            $order = Order::where('invoice_no',$request->oid)->first();
            if($order){
            $url = "https://uat.esewa.com.np/epay/transrec";
            $data =[
            'amt'=> $order->total,
            'rid'=> $request->refId,
            'pid'=> $order->invoice_no,
            'scd'=> 'epay_payment'
                 ];
            }

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);
        $response_code = $this->get_response('response_code',$response);
        if(trim($response_code) =='Success')
        {
            $order->status1;
            $order->save();
            return redirect()->route('payment.response')->with('success_message''Trasaction completed.');
        }
        }
    }
    public function failure()
    {
        return redirect()->route('payment.response')->with('failure_message''Failed.');
    }
    //extract value from response code of verification of payment
    public function get_response($node, $xml)
    {
        if($xml==false){
        return false;
        }
        $found = preg_match('#<'.$node.'[?:\s+>]+)?>(.*?)'.'</'.$node.'>#s',$xml$matches);
        if($found!= false){
            return $matches[1];
        }
        return false;
    }
    public function response()
    {
        return view('response');
    }
}


ProductsController.php

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $productsProduct::all();
        return view('products'compact('products'));
    }

 
}



OrderController.php

<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\Product;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (isset($request->product_id))
        {
            $product_id = $request->product_id;
            $product = Product::where('id',$product_id)->first();
            $order = New Order();
            $order->product_id = $product->id;
            $order->invoice_no = $product->id.time();
            $order->total = $product->amount;
            $order->save();
            return view ('checkout',compact('product','order'));
        }
    }
}