Laravel Zaakpay Payment Gateway Integration Tutorial Example

  • 05-02-2023
  • 1374
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Here In this post, I will tell you a general overview of the steps for integrating the Zaakpay payment gateway into a Laravel application, as well as I will tell you how to handle the zaakpay payment response after the payment made successfully, and also included refund payment zaakpay payment.

In this post, I have given all about the zaakpay payment integration tutorial in the laravel application from scratch also make a youtube video on it for more understanding you can refer this video too.

Here is some step for integration zaakpay payment in laravel:

  • Step 1. Make Payment Button
  • Step 2. Make Controller & Route
  • Step 3. Make Checksum Model
  • Step 4. Create PaymentController
  • Step 5. Make Blade File
  • Step 6. Start Development Server & Test

Step 1. Make Payment Button

In the video tutorial I gave a full HTML code description, but here UI just put a make payment link, by clicking this payment link payment gateway will open.

<div>
    <a href="{{ route('make.payment') }}">Make payment</a>
</div>

Step 2. Make Controller & Route

In this step we will create a PaymentController, in this PaymentController, we will define the make payment, the response from the payment gateway, refund payment method we will define.

Enter the below-provided command in your application terminal, this command will create a PaymentController in the controller directory.

php artisan make:controller PaymentController

After creating a controller, we will call that controller in the route file. we will create three routes from the make payment, response payment Handel and refund payment route. just copy the given route in your web.php file.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PaymentController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('make/payment',[PaymentController::class,'index'])->name('make.payment');
Route::any('success/payment',[PaymentController::class,'response'])->name('success.payment');
Route::get('refund',[PaymentController::class,'refund'])->name('refund.payment');

Step 3. Make Checksum Model

After the route is successfully defined, we will add a model for the make payment checksum, this Checksum model will calculate the payment.

Run the below-provided command in the terminal and create a Checksum model.

php artisan make:model Checksum

After successfully creating a checksum model using the above-provided command, use the below example code in your model, just copy the whole model as the below-provided code and paste into your model.

App\Models\Checksum.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Checksum extends Model
{
    static function calculateChecksum($secret_key, $all) {
        $hash = hash_hmac('sha256', $all , $secret_key);
        $checksum = $hash;
        return $checksum;
    }
    
    static function getAllParams($data) {
        $all = '';
        
        //Compatible till version 8
        $checksumsequence= array("amount","bankid","buyerAddress",
                "buyerCity","buyerCountry","buyerEmail","buyerFirstName","buyerLastName","buyerPhoneNumber","buyerPincode",
                "buyerState","currency","debitorcredit","merchantIdentifier","merchantIpAddress","mode","orderId",
                "product1Description","product2Description","product3Description","product4Description",
                "productDescription","productInfo","purpose","returnUrl","shipToAddress","shipToCity","shipToCountry",
                "shipToFirstname","shipToLastname","shipToPhoneNumber","shipToPincode","shipToState","showMobile","txnDate",
                "txnType","zpPayOption");
        
        foreach($checksumsequence as $seqvalue) {
            if(array_key_exists($seqvalue, $data)) {
                if(!$data[$seqvalue]=="")
                {
                    if($seqvalue != 'checksum') 
                    {
                        $all .= $seqvalue;
                        $all .="=";
                        $all .= $data[$seqvalue];
                        $all .= "&";
                    }
                }
            }
        }
        
        return $all;
    }
    
    static function outputForm($checksum,$data) {
        foreach($data as $key => $value) {
            echo ''."\n";
        }
        echo ''."\n";
    }
    
    static function verifyChecksum($checksum, $all, $secret) {
        $cal_checksum = Checksum::calculateChecksum($secret, $all);
        $bool = 0;
        if($checksum == $cal_checksum)  {
            $bool = 1;
        }
        
        return $bool;
    }
}

Step 4. Create PaymentController

Zaakpay Payment gateway Integration Tutorial PHP

Open PaymentController in your application and paste the below-given code into your application controller, there are three methods defined in this controller. The first method for the make zaakpay payment. the second method will handle the response from the zaakpay gateway. and the third method for the refund is your payment method.

App\Http\Controllers\PaymentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Ixudra\Curl\Facades\Curl;

use App\Models\Checksum;
use Auth;

class PaymentController extends Controller
{
    public function index()
    {
        $secretKey   = "0678056d96914a8583fb518caf42828a" ; //Get your secret key on Zaakpay.com
        $transactApi = "/api/paymentTransact/V8" ;
        $environment = "https://zaakstaging.zaakpay.com" ; //For Live transaction use https://api.zaakpay.com
        $returnUrl = route('success.payment') ; //Change this with your response file

        $data['merchantIdentifier'] = "fb2016ffd3a64b2994a6289dc2b671a4";
        $data['orderId']            = time();
        $data['amount']             = (100*100);
        $data['buyerEmail']         = 'example@gmail.com';
        $data['currency']           = "INR";
        $data['returnUrl']          = $returnUrl;

        $all = Checksum::getAllParams($data);
        $checksum = Checksum::calculateChecksum($secretKey,$all);

        return view('payment',compact('data','environment','transactApi','checksum'));
    }

    public function response(Request $request)
    {
        $data = $request->all();

        $datas['merchantIdentifier'] = "fb2016ffd3a64b2994a6289dc2b671a4";
        $datas['mode']               = 0;
        $datas['orderDetail']['orderId']            = $data['orderId'];

        $secretKey   = "0678056d96914a8583fb518caf42828a";

        $checksum = Checksum::calculateChecksum($secretKey,json_encode($datas));

        $response = Curl::to('https://zaakstaging.zaakpay.com/checkTxn?v=5')
                ->withData(['data' => json_encode($datas),'checksum' => $checksum])
                ->post();

        dd(json_decode($response));
    }

    public function refund()
    {
        $datas['merchantIdentifier']        = "fb2016ffd3a64b2994a6289dc2b671a4";
        $datas['orderDetail']['orderId']    = '1675507346';
        $datas['orderDetail']['amount']     = "10000";

        $datas['mode']               = "0";
        $datas['updateDesired']      = "14";
        $datas['updateReason']       = "Refund";

        $secretKey   = "0678056d96914a8583fb518caf42828a";
        $checksum = Checksum::calculateChecksum($secretKey,json_encode($datas));

        $response = Curl::to('https://zaakstaging.zaakpay.com/updateTxn')
                ->withData(['data' => json_encode($datas),'checksum' => $checksum])
                ->post();

        // dd(json_decode($response));

        $datas1['merchantIdentifier'] = "fb2016ffd3a64b2994a6289dc2b671a4";
        $datas1['mode']               = 0;
        $datas1['orderDetail']['orderId']            = $datas['orderDetail']['orderId'];
        
        $checksums = Checksum::calculateChecksum($secretKey,json_encode($datas1));

        $responses = Curl::to('https://zaakstaging.zaakpay.com/checkTxn?v=5')
                ->withData(['data' => json_encode($datas1),'checksum' => $checksums])
                ->post();

        dd(json_decode($responses));
    }
}

Step 5. Make Blade File

Blade file for the make payment, this file will make checksum and then after it will submit form using javascript itself, once the form submits it will redirect to zaakpay payment gateway.

resources\views\payment.blade.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Zaakpay</title>
</head>
<body>
<center>
    <table width="500px;">
        <tr>
            <td align="center" valign="middle">Do Not Refresh or Press Back <br/> Redirecting to Zaakpay</td>
        </tr>
        <tr>
            <td align="center" valign="middle">
                <form action="<?php echo $environment.$transactApi ; ?>" method="post">
                    <?php
                        \App\Models\Checksum::outputForm($checksum,$data);
                    ?>
                </form>
            </td>
        </tr>
    </table>
    </center>
    <script type="text/javascript">
    var form = document.forms[0];
    form.submit();
    </script>
</body>
</html>

Step 6. Start Development Server & Test

Using the below command start your application development server.

php artisan serve

Run the below-provided localhost URL and make a payment that it, we already make a payment button that you can make a payment by pressing the payment button.

http://127.0.0.1:8000/make/payment

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :