Payment gateways are a crucial element of any e-commerce store, enabling smooth and secure online transactions. This tutorial will guide you through the process of integrating Moneris into your Laravel 11 application, ensuring a seamless payment experience for your users.
Table Of Content
1 Prerequisites
1.) PHP version of 8.2
2.) MySql
3.) Moneris Payment Gateway
2 Introduction
In this article, I am going to show you how to Integrate Moneris Payment Gateway to your Laravel application.
3 Create / Install a Laravel Project
3.1 Install Laravel Project
First, make sure your computer has a composer.
Use the following command to install new Laravel Project.
Upon Successful Transaction, the payment trasactions data will be stored in the database. This process involves accessing the .env file to input and define the database credentials.
Inside the Moneris Gateway merchant account, you need to obtain an API Token. Go to My Account and click on the ADMIN. Then click on Store Settings and click on the button to generate an API Token.
6 Configure Moneris Credentials
6.1 Add the Moneris API Credentials in .env
Insert the Store ID and API Token, Which we obtained from previous step MONERIS_STORE_ID and MONERIS_API_TOKEN.
MONERIS_STORE_ID =Your Store ID
MONERIS_API_TOKEN=Store API Token
7 Create Migration and Model
Now, you need to create migration for new table payments to store the response of Moneris API. Also, create a model Payment for the same.
Run this below command.
php artisan make:model Payment —migration
In the generated new migration file, update the up and down methods as described below:
database/migrations/2024_05_22_163057_payment.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payments',function(Blueprint $table) {
$table->increments('id');
$table->string('txn_number');
$table->string('receipt_id');
$table->string('referenc_num');
$table->string('user_email');
$table->string('amount');
$table->longText('message');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Use the following command to run the migration to update your database.
php artisan migrate
app/Models/Payment.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
use HasFactory;
protected $table = 'payments';
protected $guarded = ['id'];
}
8 Create New Controller - MonerisController
Now create a controller "MonerisController" and add index() and store() methods
Use the following artisan command to Create Controller.
php artisan make:controller MonerisController
app/Http/Controllers/MonerisController.php
<?php
namespace App\Http\Controllers;
use App\Libraries\Moneris;
use App\Libraries\Transaction;
use App\Libraries\CofInfo;
use App\Libraries\MpgRequest;
use App\Libraries\MpgHttpsPost;
use App\Libraries\HttpsPost;
use App\Libraries\MpgResponse;
use App\Libraries\MpgAvsInfo ;
use App\Libraries\MpgCvdInfo ;
use App\Models\Payment;
use App\Http\Requests;
use Illuminate\Http\Request;
use Redirect;
class MonerisController extends Controller
{
public function index()
{
return view('payment-moneris');
}
public function store(Request $request)
{
// dd($request);
/************************ Request Variables ***************************/
$store_id=env('MONERIS_STORE_ID');
$api_token=env('MONERIS_API_TOKEN');
//exit;
/********************* Transactional Variables ************************/
$type='purchase';
$order_id='ord-'.date("dmy-G:i:s");
$cust_id='CUST'.rand(0,1000);
$amount=number_format($request->amount,2);
$pan=$request->cardit_card_number;
$expiry_date=$request->expiry_month.$request->expiry_year; //December 2008
$crypt='7';
/************************** AVS Variables *****************************/
$avs_street_number = $request->street_number;
$avs_street_name = $request->street_name;
$avs_zipcode = $request->zipcode;
$avs_email = $request->email;
$avs_hostname = 'www.getsamplecode.com';
$avs_browser = 'Mozilla';
$avs_shiptocountry = 'Canada';
$avs_merchprodsku = '123456';
$avs_custip = $_SERVER['REMOTE_ADDR'];
$avs_custphone = $request->custphone;;
/************************** CVD Variables *****************************/
$cvd_indicator = '1';
$cvd_value = $request->card_cvv;;
/********************** AVS Associative Array *************************/
$avsTemplate = array(
'avs_street_number'=>$avs_street_number,
'avs_street_name' =>$avs_street_name,
'avs_zipcode' => $avs_zipcode,
'avs_hostname'=>$avs_hostname,
'avs_browser' =>$avs_browser,
'avs_shiptocountry' => $avs_shiptocountry,
'avs_merchprodsku' => $avs_merchprodsku,
'avs_custip'=>$avs_custip,
'avs_custphone' => $avs_custphone
);
/********************** CVD Associative Array *************************/
$cvdTemplate = array(
'cvd_indicator' => $cvd_indicator,
'cvd_value' => $cvd_value
);
/************************** AVS Object ********************************/
$mpgAvsInfo = new mpgAvsInfo ($avsTemplate);
/************************** CVD Object ********************************/
$mpgCvdInfo = new mpgCvdInfo ($cvdTemplate);
/***************** Transactional Associative Array ********************/
$txnArray=array(
'type'=>$type,
'order_id'=>$order_id,
'cust_id'=>$cust_id,
'amount'=>$amount,
'pan'=>$pan,
'expdate'=>$expiry_date,
'crypt_type'=>$crypt
);
/********************** Transaction Object ****************************/
$mpgTxn = new Transaction($txnArray);
/************************ Set AVS and CVD *****************************/
$mpgTxn->setAvsInfo($mpgAvsInfo);
$mpgTxn->setCvdInfo($mpgCvdInfo);
/************************ Request Object ******************************/
$mpgRequest = new mpgRequest($mpgTxn);
$mpgRequest->setProcCountryCode("CA"); //"US" for sending transaction to US environment
$mpgRequest->setTestMode(true); //false or comment out this line for production transactions
/*********************** HTTPS Post Object ****************************/
$mpgHttpPost =new mpgHttpsPost($store_id,$api_token,$mpgRequest);
/*************************** Response *********************************/
$mpgResponse=$mpgHttpPost->getMpgResponse();
if($mpgResponse->getResponseCode()>50)
return redirect()->back()->with('error',$mpgResponse->getMessage());
else
{
$payment = Payment::create([
'txn_number' => $mpgResponse->getTxnNumber(),
'receipt_id' => $mpgResponse->getReceiptId(),
'referenc_num' => $mpgResponse->getReferenceNum(),
'user_email' => $avs_email,
'amount' => $mpgResponse->getTransAmount(),
'message' => $mpgResponse->getMessage()
]);
return redirect()->back()->with('message',"Payment Successful");
}
}
}
?>
9 Define a Route
Define routes for the MonerisController in the web.php file routes/web.php