composer require msilabs/bkash
php artisan vendor:publish --provider="Msilabs\Bkash\BkashServiceProvider"
.env:
BKASH_SANDBOX=true
BKASH_APP_KEY=your_app_key
BKASH_APP_SECRET=your_app_secret
BKASH_USERNAME=your_username
BKASH_PASSWORD=your_password
php artisan bkash:create-payment --amount=100 --currency=BDT --merchantInvoice=INV123
Or hit the validation endpoint:
http://your-app.test/bkash-sandbox-validation/create-payment
config/bkash.php (published config)routes/web.php (sandbox validation routes)app/Providers/BkashServiceProvider.php (service binding)Payment Initiation:
use Msilabs\Bkash\Facades\Bkash;
$payment = Bkash::createPayment([
'amount' => 100.00,
'currency' => 'BDT',
'merchantInvoice' => 'ORD-' . time(),
'merchantOrderId' => 'ORD-' . time(),
'merchantOrderReference' => 'Reference-123',
'merchantCustomerMsisdn' => '01712345678',
'paymentRequestId' => 'PR-' . time(),
'callbackUrl' => route('bkash.callback'),
]);
paymentRequestId in your DB to track status later.Callback Handling:
Route::post('/bkash/callback', [PaymentController::class, 'handleCallback']);
public function handleCallback(Request $request) {
$response = Bkash::verifyCallback($request->all());
if ($response->isSuccess()) {
// Process successful payment
}
}
Payment Status Check:
$status = Bkash::checkPaymentStatus($paymentRequestId);
BkashMiddleware to validate requests in production.try-catch and log errors:
try {
$payment = Bkash::createPayment(...);
} catch (\Exception $e) {
Log::error('bKash Error: ' . $e->getMessage());
}
Bkash::listen() to subscribe to events.Sandbox vs Production:
BKASH_SANDBOX=false in production.app_key, app_secret from bKash Developer Portal).Callback Validation:
Bkash::verifyCallback().$response = Bkash::verifyCallback($request->all());
if (!$response->isValidSignature()) {
abort(403, 'Invalid callback signature');
}
Rate Limiting:
use GuzzleHttp\Exception\RequestException;
try {
$response = Bkash::createPayment(...);
} catch (RequestException $e) {
if ($e->getCode() === 429) {
sleep(2); // Retry after 2 seconds
retry();
}
}
Enable Debug Mode:
BKASH_DEBUG=true
Logs API requests/responses to storage/logs/bkash.log.
Test with Sandbox First:
Use the provided sandbox endpoints (/bkash-sandbox-validation/*) to validate flows before going live.
Custom Responses:
Override default responses by binding your own BkashResponse class:
Bkash::extend(function ($app) {
$app->bind('bkash.response', function () {
return new CustomBkashResponse();
});
});
Webhook Events:
Extend the BkashWebhook class to handle custom events:
class CustomBkashWebhook extends \Msilabs\Bkash\Webhook\BkashWebhook {
public function handlePaymentSuccess($data) {
// Custom logic
}
}
Register it in BkashServiceProvider.
API Versioning: The package supports multiple bKash API versions. Specify in config:
'api_version' => 'v1.2.0-beta',
How can I help you explore Laravel packages today?