Laravel Tap Payment Gateway Integration Tutorial Example

  • 06-08-2022
  • 2404
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Laravel Tap Payment Gateway Integration Tutorial Example; In this post, I will integrate the Tap payment gateway into the laravel 9 application.

Using cURL I will integrate the tap payment gateway in the laravel 9 application. For Generate your tap payment credentials click here Tap Company and signup with your personal details nad login. Then go to the API section and generate your secret key from tap payment.

In this laravel tap payment gateway integration tutorial, for example, you can easy to integrate it into your application. Just follow each step and integrate it into your application. Having ended this tutorial you will simply integrate the tap payment gateway in your laravel application.

PREVIEW :

Payment form for general order to tap payment with the customer details.

image

Made payment by entering card details and expire details with the CVV number on the tap payment screen.

image

After completing the payment redirecting to our application tap the payment screen.

image

Tap payment completed after success response based on payment capture status.

image

Tap payment integration in laravel is very to integrate using cURL. So let's start tap payment integration.

Step 1: Create Project

We will start to integrate Tap payment with cloning the new fresh laravel project. Copy below the suggested command and fire it in your terminal. Little takes time to create and install composer in the laravel application. By default latest version of laravel will take through this command.

composer create-project laravel/laravel Laravel-Tap-Payment

Goto project directory by entering as below in your terminal.

cd Laravel-Tap-Payment

Step 2: Route & Controller

Will create a Tap payment controller using the below command. This command will create a payment Controller in your app/Http/Controllers directory.

This Tap payment controller will help us to write the server-side script in this controller. More about this controller will see in the next step.

php artisan make:controller TapController

Goto your routes folder inside the web.php file. This is for whole project routes. Define The three routes as below example in your web.php file.

The first route is "tap-payment" with the GET method for the view form and entering details. The second route for the form submits with a POST request. and short route for the call back from the Tap payment. Also called redirect route.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TapController;
/*
|--------------------------------------------------------------------------
| 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('tap-payment', [TapController::class,'form'])->name('tap.form');
Route::post('tap-payment', [TapController::class,'payment'])->name('tap.payment');
Route::any('tap-callback',[TapController::class,'callback'])->name('tap.callback');

This is our TapController which we earlier created by firing the command. This is all there method defined here which we create the route in the web.php file. just copy these three methods.

app/Http/Controllers/TapController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TapController extends Controller
{
    public function form()
    {
        return view('tap-payment');
    }

    public function payment(Request $request)
    {
        $request = $this->validate($request,[
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email',
            'currency' => 'required',
            'amount' => 'required|numeric',
            'order_id' => 'required',
            'phone' => 'required|numeric'
        ]);

        $payment = [
            "amount" => round($request['amount'],2),
            "description" =>  'Hello '. $request['first_name'].' '.$request['last_name'].' Your order_id is '.$request['order_id'].' please pay and confirm your order Thanks For made order.',
            "currency" => $request['currency'],
            "receipt" => [
                "email" => true,
                "sms" => true
            ],
            "customer"=> [
                "first_name"=> $request['first_name'],
                "last_name"=> $request['last_name'],
                "email"=> $request['email'],
                "phone"=> [
                    "country_code" => 'IN',
                    "number" => $request['phone']
                ]
            ],
            "source"=> [
                "id"=> "src_card"
            ],
            "redirect"=> [
                "url"=> route('tap.callback')
            ]
        ];
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.tap.company/v2/charges",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($payment),
        CURLOPT_HTTPHEADER => array(
            "authorization: Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ", // SECRET API KEY
            "content-type: application/json"
        ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        $response = json_decode($response);

        return redirect($response->transaction->url);
    }

    public function callback(Request $request)
    {
        $input = $request->all();

        $curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.tap.company/v2/charges/".$input['tap_id'],
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => "{}",
        CURLOPT_HTTPHEADER => array(
                "authorization: Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ" // SECRET API KEY
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        $responseTap = json_decode($response);

        if ($responseTap->status == 'CAPTURED') {
            
            return redirect()->route('tap.form')->with('success','Payment Successfully Made.');
        }

        return redirect()->route('tap.form')->with('error','Something Went Wrong.');
    }
}

Step 4: Payment Form

Payment Form, This form will collect user details and order IDs, and amounts. send users details to tap payment. Once the request sends to tap payment. The tap payment getaway will return the payment page URL.

<!doctype html>
<html lang="en">
    <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>Tap Payment Laravel - Webappfix</title>
    </head>
    <body>
    
    <div class="container mt-5">
        <form action="{{ route('tap.payment') }}" method="post">
            @csrf
            <div class="row">
                <div class="col-6 m-auto bg-light rounded border p-3">
                    @if (\Session::has('success'))
                        <div class="alert alert-success">
                            {{ Session::get('success') }}
                        </div>
                    @endif
                    @if (\Session::has('error'))
                        <div class="alert alert-danger">
                            {{ Session::get('error') }}
                        </div>
                    @endif
                    <h1 class="text-center">Tap Payment - Webappfix.com</h1>
                    <div class="form-group mt-3">
                        <label for="first_name" class="text-blue">First Name :</label>
                        <input id="first_name" class="form-control" type="text" name="first_name" placeholder="First Name">
                        @error('first_name')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="last_name" class="text-blue">Last Name :</label>
                        <input id="last_name" class="form-control" type="text" name="last_name" placeholder="Last Name">
                        @error('last_name')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="email" class="text-blue">Email :</label>
                        <input id="email" class="form-control" type="text" name="email" placeholder="Email Address">
                        @error('email')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="phone" class="text-blue">Phone :</label>
                        <input id="phone" class="form-control" type="number" name="phone" placeholder="Phone NUmber">
                        @error('phone')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="currency" class="text-blue">Select Currency :</label>
                        <select class="form-control" name="currency" id="currency" name="currency">
                            <option value="KWD">KWD</option>
                        </select>
                        @error('currency')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="order_id" class="text-blue">Order Id :</label>
                        <input id="order_id" class="form-control" type="text" name="order_id" placeholder="Order Id" value="{{ \Str::random(10) }}">
                        @error('order_id')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="form-group mt-3">
                        <label for="name" class="text-blue">Amount :</label>
                        <input id="name" class="form-control" type="number" name="amount" placeholder="Amount">
                        @error('amount')<font color="red">{{ $message }}</font>@endError
                    </div>
                    <div class="col-12 mt-3">
                        <button type="submit" class="btn btn-success">Pay Now</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    </body>
</html>

Step 5: Testing

Start the development server, and run the suggested command in your laravel application terminal. I will start your development server ay locally.

php artisan serve

Run the below URL in your browser. This URL will show you a user's fill-up form.

http://127.0.0.1:8000/tap-payment
CONCLUSION

In this post, I have integrated a tap payment gateway in the laravel 9 application using cURL. Even I also added a preview for better understanding. Just follow each step and integrate Tap Payment Gateway in your laravel application. First signup for your tap company account and log in and generate the product key and secret key from there and put these keys in the cURL header to prepare token.


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 :