Step-by-Step Guide to Integrating Gkash 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 Gkash Settings:
Add your Gkash API credentials and other settings to the.env
file:
GKASH_MERCHANT_ID=your_merchant_id
GKASH_API_KEY=your_api_key
GKASH_PAYMENT_GATEWAY_URL=https://securepay.gkash.my/Transaction/Pay
GKASH_RETURN_URL=https://yourdomain.com/payment/return
GKASH_CALLBACK_URL=https://yourdomain.com/payment/callback
- Create Routes:
Define routes for handling payment and response from Gkash 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('GKASH_MERCHANT_ID');
$apiKey = env('GKASH_API_KEY');
$amount = number_format($request->amount, 2, '.', '');
$orderId = 'INV' . time();
$name = $request->name;
$email = $request->email;
$phone = $request->phone;
$desc = $request->description;
$currency = 'MYR';
$returnUrl = env('GKASH_RETURN_URL');
$callbackUrl = env('GKASH_CALLBACK_URL');
$signature = hash('sha256', $merchantId.$orderId.$amount.$currency.$apiKey);
$paymentData = [
'merchant_id' => $merchantId,
'order_id' => $orderId,
'amount' => $amount,
'currency' => $currency,
'customer_name' => $name,
'customer_email' => $email,
'customer_phone' => $phone,
'description' => $desc,
'return_url' => $returnUrl,
'callback_url' => $callbackUrl,
'signature' => $signature,
];
return view('process_payment', compact('paymentData'));
}
public function return(Request $request)
{
$status = $request->status;
$orderId = $request->order_id;
$amount = $request->amount;
$message = $request->message;
if ($status == 'success') {
// Payment successful
} else {
// Payment failed
}
return view('payment_return', compact('status', 'orderId', 'amount', 'message'));
}
public function callback(Request $request)
{
// Handle the callback from Gkash
// 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 Gkash:
<form id="gkash_form" method="POST" action="{{ env('GKASH_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('gkash_form').submit();
</script>
- Handle Return and Callback:
Create views to handle the return and callback from Gkash. For example,resources/views/payment_return.blade.php
:
@if($status == 'success')
<p>Payment successful! Order ID: {{ $orderId }}, Amount: {{ $amount }}</p>
@else
<p>Payment failed! Message: {{ $message }}</p>
@endif
By following these steps, you should be able to integrate Gkash with your Laravel application. Make sure to test thoroughly in a sandbox environment provided by Gkash before going live.