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.
<?php return [ // The current mode is live / production or test 'test_mode' => env('TELR_TEST_MODE', true), // The currency of store 'currency' => env('TELR_CURRENCY', 'AED'), // The sale endpoint that receive the params // @see https://telr.com/support/knowledge-base/hosted-payment-page-integration-guide 'sale' => [ 'endpoint' => 'https://secure.telr.com/gateway/order.json', ], // The hosted payment page use the following params as it explained in the integration guide // @see https://telr.com/support/knowledge-base/hosted-payment-page-integration-guide/#request-method-and-format 'create' => [ 'ivp_method' => "create", 'ivp_store' => env('TELR_STORE_ID', null), 'ivp_authkey' => env('TELR_STORE_AUTH_KEY', null), 'return_auth' => '/handle-payment/success', 'return_can' => '/handle-payment/cancel', 'return_decl' => '/handle-payment/declined', ] ]; ?>
- 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
public function payment(Request $request) { $telrManager = new \TelrGateway\TelrManager(); $order_id = rand(11111,99999); $total = 1; $billingParams = [ 'first_name' => 'Abc', 'sur_name' => 'Xyz', 'address_1' => 'Test Address', 'address_2' => 'Test Address 2', 'city' => 'Dubai', 'zip' => 123456, 'country' => 'UAE', // 'country' => 'United Arab Emirates', 'email' => 'testTelr@gmail.com', ]; $url_link = $telrManager->pay($order_id, $total, 'Telr Test Payment Details', $billingParams)->redirect(); $url = $url_link->getTargetUrl(); // Display Result Of Response // dd($url); // return redirect($url); return view('payment',compact('url')); }
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
public function paymentSuccess(Request $request) { // Store Transaction Details $telrManager = new \TelrGateway\TelrManager(); $transaction = $telrManager->handleTransactionResponse($request); //Card Details $card_last_4 = $transaction->response['order']['card']['last4']; $card_holder_name = $transaction->response['order']['customer']['name']['forenames']." ".$transaction->response['order']['customer']['name']['surname']; //Queries $paymentDetails = Transaction::where('cart_id',$request->cart_id)->firstOrFail(); // Display Result Of Response dump('paymentSuccess :: ',$transaction); dump('transaction Response :: ',$transaction->response); dd('payment Details :: ',$paymentDetails); // Handle Order & Email Related Other Things return redirect(route('home')); }
After Successfully making payment Telr redirects to the success URL & we will handle orders, emails & others in this function.
3. Payment Cancel
public function paymentCancel(Request $request) { $telrManager = new \TelrGateway\TelrManager(); $transaction = $telrManager->handleTransactionResponse($request); // Display Result Of Response dd('paymentCancel :: ',$transaction);</code> return redirect(route('home')); }
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
ublic function paymentDeclined(Request $request) { $telrManager = new \TelrGateway\TelrManager(); $transaction = $telrManager->handleTransactionResponse($request); // Display Result Of Response dd('paymentDeclined :: ',$transaction); return redirect(route('home')); }
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.
public function paymentWithCurl(Request $request) { $unique_cart_id = rand(11111,55555); $params = array( 'ivp_method' => 'create', 'ivp_framed' => 2, 'ivp_store' => env('TELR_STORE_ID', null), 'ivp_authkey' => env('TELR_STORE_AUTH_KEY', null), 'ivp_cart' => $unique_cart_id, 'ivp_test' => env('TELR_TEST_MODE', true), 'ivp_amount' => '100.00', 'ivp_currency' => env('TELR_CURRENCY', 'AED'), 'ivp_desc' => 'Product Description', 'return_auth' => 'http://127.0.0.1:8000/handle-payment/success?id='.$unique_cart_id, 'return_can' => 'http://127.0.0.1:8000/handle-payment/cancel?id='.$unique_cart_id, 'return_decl' => 'http://127.0.0.1:8000/handle-payment/declined?id='.$unique_cart_id, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://secure.telr.com/gateway/order.json"); curl_setopt($ch, CURLOPT_POST, count($params)); curl_setopt($ch, CURLOPT_POSTFIELDS,$params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); $results = curl_exec($ch); curl_close($ch); $results = json_decode($results,true); $ref= trim($results['order']['ref']); $url= trim($results['order']['url']); if (empty($ref) || empty($url)) { # Failed to create order dd("something went wrong"); } // return redirect($url); return view('payment',compact('url')); }
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
<p align="center" style="overflow-x:auto;"> <iframe id="telrPaymentFrame" name="telrPaymentFrame" src="{{ $url }}" style="width: 50%;min-width: 600px;height: 600px;frameborder: 0;" /></iframe> </p>
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
public function toArray() { return [ 'ivp_method' => $this->getMethod(), 'ivp_framed' => 2, 'ivp_store' => $this->getStoreId(), 'ivp_authkey' => $this->getAuthkey(), 'ivp_cart' => $this->getCartId(), 'ivp_test' => $this->isTestMode(), 'ivp_amount' => $this->getAmount(), 'ivp_currency' => $this->getCurrency(), 'ivp_desc' => $this->getDesc(), 'ivp_lang' => $this->getLangCode(), 'return_auth' => $this->getSuccessURL(), 'return_can' => $this->getCancelURL(), 'return_decl' => $this->getDeclinedURL(), 'bill_fname' => $this->getBillingFirstName(), 'bill_sname' => $this->getBillingSurName(), 'bill_addr1' => $this->getBillingAddress1(), 'bill_addr2' => $this->getBillingAddress2(), 'bill_city' => $this->getBillingCity(), 'bill_region' => $this->getBillingRegion(), 'bill_zip' => $this->getBillingZip(), 'bill_country' => $this->getBillingCountry(), 'bill_email' => $this->getBillingEmail(), ]; }
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.
composer.json file. "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }, "files": [ "app/Overrides/laravel_payment/telr/CreateTelrRequest.php" ] },
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.