1. Go to this page and download the library: Download theihasan/laravel-bkash library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Ihasan\Bkash\Facades\Bkash;
public function initiatePayment(Request $request)
{
$paymentData = [
'amount' => '100', // Payment amount in BDT
'payer_reference' => 'customer123',
'callback_url' => route('bkash.callback'), //If you use this built in route then this package will handle your callback automatically otherwise you have to implement your own callback logic. So don't change this to use automatic callback handling
'merchant_invoice_number' => 'INV-123456',
];
try {
$response = Bkash::createPayment($paymentData);
// Redirect to the bKash payment page
return redirect()->away($response['bkashURL']);
} catch (\Exception $e) {
return back()->with('error', $e->getMessage());
}
}
use Ihasan\Bkash\Facades\Bkash;
public function initiatePayment(Request $request)
{
$paymentData = [
'amount' => '100', // Payment amount in BDT
'payer_reference' => 'customer123',
'callback_url' => route('bkash.callback'), //If you use this built in route then this package will handle your callback automatically otherwise you have to implement your own callback logic. So don't change this to use automatic callback handling
'merchant_invoice_number' => 'INV-123456',
];
try {
$response = Bkash::forTenant($tenantId)->createPayment($paymentData);
// Redirect to the bKash payment page
return redirect()->away($response['bkashURL']);
} catch (\Exception $e) {
return back()->with('error', $e->getMessage());
}
}
use Ihasan\Bkash\Facades\Bkash;
public function handleCallback(Request $request)
{
if ($request->input('status') === 'success') {
try {
$response = Bkash::executePayment($request->input('paymentID'));
return redirect()->route('payment.success', ['transaction_id' => $response['trxID']]);
} catch (\Exception $e) {
return redirect()->route('payment.failed')->with('error', $e->getMessage());
}
}
return redirect()->route('payment.failed')->with('error', 'Payment was not successful');
}
// Get a token
$token = Bkash::getToken();
// Refresh a token
$token = Bkash::refreshToken();
use Ihasan\Bkash\Facades\Bkash;
// Get a token for a tenant
$token = Bkash::forTenant($tenantId)->getToken();
// Refresh a token for a tenant
$token = Bkash::forTenant($tenantId)->refreshToken();
namespace App\Listeners;
use Ihasan\Bkash\Events\PaymentSuccessful;
use Illuminate\Contracts\Queue\ShouldQueue;
class HandleSuccessfulPayment implements ShouldQueue
{
public function handle(PaymentSuccessful $event)
{
$payment = $event->payment;
$paymentData = $event->paymentData;
// Your custom logic here
// For example, update order status, send notification, etc.
}
}