Step-by-Step Guide to Integrating eGHL with Laravel
- Setup Laravel Project:
Ensure you have a Laravel project set up. If not, you can create one using:
composer create-project --prefer-dist laravel/laravel your_project_name
- Install Required Packages:
Laravel comes with Guzzle, an HTTP client that can be used to make API requests. Ensure it is installed:
composer require guzzlehttp/guzzle
- Configure eGHL Settings:
Add your eGHL merchant details to the.env
file:
EGHL_MERCHANT_NAME=your_merchant_name
EGHL_MERCHANT_ID=your_merchant_id
EGHL_PASSWORD=your_password
EGHL_PAYMENT_GATEWAY_URL=https://yourpaymentgatewayurl.com
EGHL_RETURN_URL=https://yourdomain.com/payment/return
EGHL_CALLBACK_URL=https://yourdomain.com/payment/callback
- Create Routes:
Define routes for handling payment and response from eGHL in yourroutes/web.php
:
use App\Http\Controllers\PaymentController;
Route::get('/payment', [PaymentController::class, 'index']);
Route::post('/payment/process', [PaymentController::class, 'process']);
Route::get('/payment/return', [PaymentController::class, 'return']);
Route::post('/payment/callback', [PaymentController::class, 'callback']);
- Create PaymentController:
Create a controller to handle the payment process:
php artisan make:controller PaymentController
In PaymentController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class PaymentController extends Controller
{
public function index()
{
return view('payment');
}
public function process(Request $request)
{
$merchantId = env('EGHL_MERCHANT_ID');
$merchantName = env('EGHL_MERCHANT_NAME');
$password = env('EGHL_PASSWORD');
$amount = number_format($request->amount, 2);
$orderId = 'INV' . time();
$paymentDesc = $request->description;
$currencyCode = 'MYR';
$hash = sha1($merchantId.$password.$orderId.$amount.$currencyCode);
$paymentData = [
'MerchantID' => $merchantId,
'ServiceID' => $merchantName,
'PaymentID' => $orderId,
'OrderNumber' => $orderId,
'PaymentDesc' => $paymentDesc,
'MerchantReturnURL' => env('EGHL_RETURN_URL'),
'MerchantCallbackURL' => env('EGHL_CALLBACK_URL'),
'Amount' => $amount,
'CurrencyCode' => $currencyCode,
'CustName' => $request->name,
'CustEmail' => $request->email,
'CustPhone' => $request->phone,
'HashValue' => $hash,
];
return view('process_payment', compact('paymentData'));
}
public function return(Request $request)
{
// Handle the return from eGHL
$status = $request->TxnStatus;
$orderId = $request->OrderNumber;
$message = $request->TxnMessage;
if ($status == '0') {
// Payment successful
} else {
// Payment failed
}
return view('payment_return', compact('status', 'orderId', 'message'));
}
public function callback(Request $request)
{
// Handle the callback from eGHL
// This will typically be used to update the payment status in your database
}
}
- Create Payment View:
Create a view (resources/views/payment.blade.php
) to handle the payment form:
<form action="/payment/process" method="POST">
@csrf
<input type="text" name="amount" placeholder="Amount">
<input type="text" name="description" placeholder="Description">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<input type="text" name="phone" placeholder="Phone">
<button type="submit">Pay Now</button>
</form>
- Create Process Payment View:
Create a view (resources/views/process_payment.blade.php
) to handle the redirection to eGHL:
<form id="eghl_form" method="POST" action="{{ env('EGHL_PAYMENT_GATEWAY_URL') }}">
@foreach($paymentData as $key => $value)
<input type="hidden" name="{{ $key }}" value="{{ $value }}">
@endforeach
<button type="submit">Proceed to Payment</button>
</form>
<script type="text/javascript">
document.getElementById('eghl_form').submit();
</script>
- Handle Return and Callback:
Create views to handle the return and callback from eGHL. For example,resources/views/payment_return.blade.php
:
@if($status == '0')
<p>Payment successful! Order ID: {{ $orderId }}</p>
@else
<p>Payment failed! Message: {{ $message }}</p>
@endif
By following these steps, you should be able to integrate eGHL with your Laravel application. Make sure to test thoroughly in a sandbox environment provided by eGHL before going live.