7th Jan 2022

Laravel: Telr Payment

Web

Written By, Vishal Prajapati

Introduction

This is the most useful & trending payment getaway for online payments. In this blog, we are looking at how we can integrate Telr Payment in Laravel with & without a package.

This is a simple payment gateway & easy to integrate. It has supported many currencies Like AED, INR, QAR, SAR, USD & many more…

So, What are you waiting for let’s Starts:-

1. Install Laravel Project

To Install Fresh Laravel Project Write Below Command In Your CMD/Terminal :
Composer create-project laravel/laravel TelrPayment

2. Laravel Telr Package For Payment

  • composer require laravel_payment/telr
    Enter this line in your terminal & after that, the telr payment package is successfully installed in your project.
  • php artisan vendor:publish –provider=”TelrGateway\TelrServiceProvider”
    If you are using a lower version of Laravel Version then you need to fire this command otherwise not needed.
  • php artisan migrate
    To migrate the table in your database, fire this command. It will create one ‘transaction’ table in your DB. This will store the payment details of every transaction.

3. Telr Configuration

After successfully installation, Go to config -> Telr.php file & here are the configuration details to set.

  • Test_mode: if true then this will react as test mode, if false it’s live.
  • Currency: default is SAR but you can set any of that can be provided by Telr.
  • Endpoint: Endpoint is Telr payment URL that helps to make payments.
  • ivp_method: This by-default need to be ‘create’ when paying amount
  • ivp_store: This is your store id of Telr account you have created on telr.com
  • ivp_authkey: This is your authentication key of Telr account you have created on telr.com
  • return_auth: If payment is a success then this Url return the result.
  • return_can: If payment is canceled then this Url returns the result.
  • return_decl: If payment is declined then this Url returns the result.

4. Set Routes In Web.php

You need to set some of the routes that we are using Here for Telr Payment.

Route::get(‘/payment’, [PaymentController::class,’payment’])->name(‘payment’);
Route::get(‘/payment-curl’, [PaymentController::class,’paymentWithCurl’])->name(‘payment-curl’);
Route::get(‘/handle-payment/success’,[PaymentController::class,’paymentSuccess’])->name(‘payment-success’);
Route::get(‘/handle-payment/cancel’,[PaymentController::class,’paymentCancel’])->name(‘payment-cancel’);
Route::get(‘/handle-payment/declined’,[PaymentController::class,’paymentDeclined’])->name(‘payment-declined’);

Here We have set five URLs in the routes -> web.php file.
The first one is for making payment, the second one is for without package payment & the rest of the three are for Telr success, cancel & declined responses.

5. Create Controller

After setting up the route we need to create one controller to handle all of this payment stuff.
php artisan make:controller PaymentController

This will create a new controller in this path.
App -> Http -> Controllers -> PaymentController.php

After creating PaymentController we are using below some functions to handle payment things::

1. Make Payment

For using Telr Package we have created one payment function that creates Telr objects. We are using the payment method of this object and making payment as in the above example.
It will return the URL to make payment. If you want to make the payment if Iframe then passes the Url in Iframe src otherwise direct redirect to this URL. It will display the payment page of the Telr.

2. Payment Success

After Successfully making payment Telr redirects to the success URL & we will handle orders, emails & others in this function.

3. Payment Cancel

If payment is canceled this year return the canceled URL & this will handle into this paymentCancel function. Using this we return failure Emails & Messages to the buyer.

4. Payment Declined

If payment is declined by the bank for any other reasons then Telr redirects back to declined & we will handle this thing in the above function. Same way as the canceled URL you can return the proper message of the buyer that payment is declined.

5. Payment With Curl (Without Package)

If you don’t want to use a package for this. We have created one function to use curl to make payments.

In the above function, we are passing some required things that Telr needs to make payment. After passing details Telr gives the same response as we got in the package. But if you use core things then you need to handle transaction details of payment by yourself.

6. Using Iframe

If You want to make payment on your website instead of the Telr payment page. Then just follow the below things.
Instead of direct redirection to the URL given by Telr just pass into Iframe tag src & you will get payment details on your site.

// return redirect($url);
return view(‘payment’,compact(‘url’));

In the above code, we are creating one payment.blade.php file & just passing the URL into it.
Path : resources -> views -> payment.blade.php

In the payment blade file, we are just passing a URL in src & it will display the payment things into it.

7. Override Package Files

If you want to make payment in Iframe then you need to pass ivp_framed = 2 parameters when you create a Telr object. Unfortunately, this is not available in this package so I make one thing just override one file that makes the object of payment & passes this thing & yes that works.
If you just use code payment then you can pass or remove it depending upon you.

To override I have created one file in the path below.
App -> Overrides -> laravel_payment -> telr -> CreateTelrRequest.php

In the above file I have passed ‘ivp_framed’ => 2, key & value in toArray() function .
One more thing, this overriding thing works when we ask Laravel to override this file & use this code for payment.
For this, we need to add one line in the below file.

We need to add the path of the file that we are overriding & put it into the ‘files’ array.
After that just fire the below command it will reflect the changes we made.

composer dump-autoload

This will not download any things but this will check the composer.json file & just add this file in its autoload file to load this file when we make payment.

Conclusion

I hope this blog will help anyone who can use Telr payment in Laravel.

Written By,

Web Developer at Yudiz Solutions Pvt. Ltd