CellPay Payment Gateway Integration in Laravel

CellPay Payment Gateway Integration in Laravel

For those with an e-commerce site, integration of payment gateway is very important - CellPay payment gateway plays an important role. This integration provides a swift and efficient method for the customers to purchase their desired as well as provides security while transferring money during the transaction.

Laravel is a well-known PHP framework that can be used to make robust web applications. It has various built-in features which make it easier for you to develop and design various applications.

CellPay is like a digital debit card that cannot be stolen. You can link multiple bank accounts to your CellPay account and use it for various utility payments, fund transfers, etc. It also supports merchant payments due to which you can transfer funds to different merchants, pay your bills, perform mobile top-up, and many more.

 

Why is CellPay better than others?

CellPay is a digital debit card that helps its customers to pay directly from his/her bank account with NO hassle of loading funds or carrying a debit card.

a. No wallet is required. Your money remains in your bank account 100% Safe with earning interest.

b. The gateway is 100% free - no extra fee will be charged.

c. Support is available 24/7

d. The system is 100% secured implementing all the possible security measures

 

Steps for Integrating CellPay Payment Gateway in Laravel

In this article, we will cover an overview of the steps you can take to integrate CellPay Payment Gateway in Laravel. There can be different methods to integrate the two systems, but we will look at the one which is most common and easiest to install.

 

Here are the steps involved in the integration process. Each of the steps is discussed below.

1. Create a CellPay merchant account.

2. Install Laravel app

3. Enter your Merchant details from your Merchant Account

4. Create routes for payments

5. Create CellPay Controller

6. Create an Order model

7. Create CellPay model

8. Create a blade file

 

1. Create a CellPay merchant account

For live online transactions, you need to create a merchant account from CellPay. Then you get access to live API and merchant id that you will need in the process of integrating CellPay Gateway in Laravel. You can contact the CellPay support team directly, do some paperwork and get the important details.

 

2. Install Laravel app

Create and install a new Laravel app according to your requirements. This is an important step. (If you have already created a new Laravel app you can skip this step and move on to step 3.)

If your computer already has PHP and Composer installed, you may create a new Laravel project using the Composer command directly.

 

Use the following code to install the app.

composer create composer create-project laravel/laravel Laravel-cellpay-app

 

3. Enter your Merchant details from your Merchant Account

As I mentioned earlier, Merchant details are provided by cellpay after you create your merchant account. You can also get test credentials from CellPay Documentation and use it to test the payment system.

 

4. Create routes for payments

In the case of payments, there are four routes you will need to clarify and input in your routes/web.php file. There is a payment process, Payment success, Payment failed and Payment cancellation.



use Illuminate\Support\Facades\Route;

Route::get('/checkout/payment/cellpay/process', [
'name' => 'CellPay Checkout Process',
'as' => 'checkout.payment.cellpay.process',
'uses' => 'Frontend\[email protected]',
]);

Route::get('/checkout/payment/cellpay/completed/{random_order_id}', [
'name' => 'CellPay Payment Completed',
'as' => 'checkout.payment.cellpay.completed',
'uses' => 'Frontend\[email protected]',
]);


Route::get('/checkout/payment/cellpay/failed/{random_order_id}', [
'name' => 'CellPay Payment Failed',
'as' => 'checkout.payment.cellpay.failed',
'uses' => 'Frontend\[email protected]',
]);

Route::get('/checkout/payment/cellpay/cancelled/{random_order_id}', [
'name' => 'CellPay Payment Cancelled',
'as' => 'checkout.payment.cellpay.cancelled',
'uses' => 'Frontend\[email protected]',
]);



 

5. Create CellPay Controller

First, run php artisan make:controller CellPayController to create a controller. This is where all the payment logic will be processed. Now, enter the following code in your app/Http/Controllers/CellPayController.php file:


C?php

namespace App\Http\Controllers;

use App\CellPay;
use App\Http\Controllers\Controller;
use App\Order;
use App\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;

class CellPayController extends Controller
{

public function process(Request $request)
{
$totalAmount = 1000;

// Generates random order id for order and cellpay
$random_order_id = uniqid();

$merchantID = "Your Merchand ID";

$description = "CellPay_payment";

//Setting up urls for live or test
$url = 'https://app.cellpay.com.np/test_merchant_api?';

$order = new Order();
$order->user_id = $userID;
$order->user_type = $userType;
$order->random_id = $random_order_id;
$order->payment_id = $paymentMethodID;
$order->total_quantity = 10;
$order->total_amount = $totalAmount;
$order->status = Order::ORDER_COMPLETE;
$order->payment_status = Order::PAYMENT_PENDING;
$order->save();


$cellPay = with(new CellPay);

try {

$cartData = [
'merchant_id' => $merchantID,
'amount' => $totalAmount,
'invoice_number' => $random_order_id,
'description' => $description,
'success_callback' => $cellPay->getSuccessCallback($random_order_id),
'failure_callback' => $cellPay->getFailureCallback($random_order_id),
'cancel_callback' => $cellPay->getCancelCallback($random_order_id),
];
$stringData = http_build_query($cartData);

return redirect()->away($url . $stringData);

} catch (Exception $e) {
// $order->update(['payment_status' => Order::PAYMENT_PENDING]);
$order = Order::where('random_id', $random_order_id)->firstOrFail();
$order->update(['status' => Order::ORDER_CANCELLED]);

return redirect()
->route('cart.index', $random_order_id)
->with('alert_msg',
sprintf("Your payment failed with error: %s", $e->getMessage()));
}

return redirect()->route('cart.index')->with([
'alert_msg' =>
"We're unable to process your payment at the moment, please try again !",
]);
}


public function paymentCompleted($random_order_id)
{
if (Auth::user()) {
$user = User::findOrFail(Auth::user()->id);
$userID = $user->id;
}

$order = Order::where('random_id', $random_order_id)
->where('user_id', $userID)->firstOrFail();
$order->update(['status' => Order::PAYMENT_COMPLETED]);

return redirect()->route('cart.index')->with([
'success_msg' =>
'Thank you for your shopping. Your payment has been successful.',
]);
}


public function paymentFailed($random_order_id)
{
// dd('Failed');
if (Auth::user()) {
$user = User::findOrFail(Auth::user()->id);
$userID = $user->id;
}

// dd('Payment Failed');
$order = Order::where('random_id', $random_order_id)->where('user_id', $userID)
->firstOrFail();
$order->update(['status' => Order::ORDER_CANCELLED]);

return redirect()->route('cart.index')->with([
'alert_msg' =>
'The payment for your order has been declined, so your order has been cancelled.',
]);
}

public function paymentCancelled($random_order_id)
{
// dd('Cancelled');
if (Auth::user()) {
$user = User::findOrFail(Auth::user()->id);
$userID = $user->id;
}

// dd('Payment Cancelled');
$order = Order::where('random_id', $random_order_id)->where('user_id', $userID)
->firstOrFail();
$order->update(['status' => Order::ORDER_CANCELLED]);
// dd($random_order_id);
return redirect()->route('cart.index')->with([
'alert_msg' =>
'You just cancelled the payment, so your order has been cancelled',
]);
}
}



6. Create an Order Model

First, run php artisan make:model Order to create a model. This is where all the functions are defined for making the code clean. Now, enter the following code in your app/Order.php file:


namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

class Order extends Model
{
const PAYMENT_COMPLETED = 1;
const PAYMENT_PENDING = 0;

const ORDER_COMPLETE = 1;
const ORDER_CANCELLED = 0;

protected $fillable = ['transaction_id', 'amount', 'payment_status', 'status'];




7. Create a CellPay Model

First, run php artisan make:model CellPay to create a model. This is where all the redirect is defined. Now, enter the following code in your app/CellPay.php file:


namespace App;

use Illuminate\Database\Eloquent\Model;

class CellPay extends Model
{

public function getSuccessCallback($random_order_id)
{
return route('checkout.payment.cellpay.completed', $random_order_id);

}

public function getFailureCallback($random_order_id)
{
return route('checkout.payment.cellpay.failed', $random_order_id);
}


public function getCancelCallback($random_order_id)
{
return route('checkout.payment.cellpay.cancelled', $random_order_id);
}
}


9. Create a blade file

First, create a blade file cellpay.blade, and add the following code in your resources/views/cellpay.blade:

div id="cellpayPaymentModal" class="modal fade">
div class="modal-dialog">
div class="modal-content">
div class="modal-header">
h5 class="modal-title">CellPayh5>
button type="button" class="close" data-dismiss="modal">×button>
div>
div class="modal-body">
button type="button" class="btn btn-primary"
onclick="window.location='{{ route('checkout.payment.cellpay.process') }}'">
Rs. 1000 Pay with CellPay
button>
div>
div>
div>
div>


Final Words:

Integrating CellPay Gateway in Laravel can take your online business to next level. Your customers will be able to pay directly on the website from his/her bank account without having to worry about any security breaches.


Follow these above steps and copy the code when required! You will be able to integrate the  CellPay payment gateway in no time using this process.   

Recent comments(0)

Please, Sign In to leave a reply