Thawani Pay Ecommerce API Integration In Laravel

  • 08-02-2023
  • 1723
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In this post we will see, how to integrate the thawani pay payment gateway integration in the laravel application, I will tell you to create step-by-step guidelines for integration Thawani payment gateway in laravel.

Below in this post, I have provided a very simple thawani payment integration tutorial in the laravel app with example, we will use a simple CRUL method for the execute payment API and refund API. The CURL is very simple to use for API integration in the app.

Thawani payment e-commerce project API integration in laravel app also we will see how to refund payment in thawani payment API using so let's start the model implementing in our project.

Befor we integrating thawani pay ecommerce api in laravel you must need to generate api keys form the thawani payment dashaboard. Click here to generate Thawani pay

Preview

image

Step 1. Define Route

In this step we will first define the route for making execute the controller method, see below route file code and paste it into your application. First, import the "ThawanipayController" at the top of the route file.

routes/web.php
<?php

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

/*
|--------------------------------------------------------------------------
| 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('payment',[ThawanipayController::class,'payment'])->name('payment');
Route::get('success',[ThawanipayController::class,'success'])->name('success');
Route::get('fail',[ThawanipayController::class,'fail'])->name('fail');

Step 2. Create Controller

After the route defines, we will create a controller, use the below-suggested command in your laravel app command prompt and create a controller.

php artisan make:controller ThawanipayController

After successfully creating the controller, we will move next to the payment code in the ThawanipayController, In this controller, you can see I defined four methods. The payment() method was made for the create transaction in the thawing pay, after that, I have create a fail and success method for the channel response from the payment gateway. and also I have provided a refund() method for the making refund of your payment made.

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

namespace App\Http\Controllers;

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

class ThawanipayController extends Controller
{
    public function payment()
    {
        $data['client_reference_id']        = time();
        $data['mode']                       = 'payment';
        $data['products'][0]['name']        = "Name";
        $data['products'][0]['quantity']    = 1;
        $data['products'][0]['unit_amount'] = round(100 * 1000,2);
        $data['success_url']                = route('success');
        $data['cancel_url']                 = route('fail');

        $response = Curl::to('https://uatcheckout.thawani.om/api/v1/checkout/session')
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->withData($data)
                ->asJson()
                ->post();

        \Session::put('pay_session_id',$response->data->session_id);

        $to = 'https://uatcheckout.thawani.om/pay/'.$response->data->session_id.'?key=HGvTMLDssJghr9tlN9gr4DVYt0qyBy';
       
        return redirect()->to($to);
    }

    public function success()
    {
        $sessionId = \Session::get('pay_session_id');

        $urls = 'https://uatcheckout.thawani.om/api/v1/checkout/session/'.$sessionId;

        $response = Curl::to($urls)
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->asJson()
                ->get();

        dd($response);
    }

    public function fail()
    {
        $sessionId = \Session::get('pay_session_id');

        $urls = 'https://uatcheckout.thawani.om/api/v1/checkout/session/'.$sessionId;

        $response = Curl::to($urls)
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->asJson()
                ->get();

        dd($response);   
    }

    public function refund()
    {
        $sessionId = \Session::get('pay_session_id');

        $urls = 'https://uatcheckout.thawani.om/api/v1/checkout/session/'.$sessionId;

        $response = Curl::to($urls)
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->asJson()
                ->get();


        $paymentObject = Curl::to('https://uatcheckout.thawani.om/api/v1/payments?checkout_invoice='.$response->data->invoice)
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->asJson()
                ->get();

        $card = current($paymentObject->data);

        $refund = Curl::to('https://uatcheckout.thawani.om/api/v1/refunds')
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->withData(['payment_id' => $card->payment_id,'reason' => 'refund'])
                ->asJson()
                ->post();
                
        $refundStatus = Curl::to('https://uatcheckout.thawani.om/api/v1/refunds/'.$refund->data->refund_id)
                ->withHeader('Content-Type: application/json')
                ->withHeader('thawani-api-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et')
                ->asJson()
                ->get();

        dd($refundStatus);
    }
}

Step 3. Make Payment Link

In this step, we will create a payment link, by clicking on the like it will redirect to the thawani payment page for the make transaction, it will open a payment page and ask for the enter card details or payment authorization.

<a href="{{ route('payment') }}">Make Payment</a>

Step 4. Start Server

Start the development server using the below provided command in your app command prompt.

php artisan serve

You can directly open the payment link after the star development server of your application. just copy the URL and paste it into your browser, note: this is just used for the local machine server only.

http://127.0.0.1:8000/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 :