PHP code example of sabitahmad / laravel-bkash
1. Go to this page and download the library: Download sabitahmad/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/ */
sabitahmad / laravel-bkash example snippets
return [
'sandbox' => env('BKASH_SANDBOX', true),
'log_transactions' => env('BKASH_LOG_TRANSACTIONS', true),
'credentials' => [
'app_key' => env('BKASH_APP_KEY'),
'app_secret' => env('BKASH_APP_SECRET'),
'username' => env('BKASH_USERNAME'),
'password' => env('BKASH_PASSWORD'),
],
'base_url' => [
'sandbox' => 'https://tokenized.sandbox.bka.sh/v1.2.0-beta/tokenized/checkout',
'production' => 'https://tokenized.pay.bka.sh/v1.2.0-beta/tokenized/checkout',
],
'endpoints' => [
'token' => '/token/grant',
// ... other endpoints
],
'callback_url' => env('BKASH_CALLBACK_URL', '/bkash/callback'),
'redirect_url' => env('BKASH_REDIRECT_URL', '/payment/redirect'),
];
use SabitAhmad\Bkash\Facades\Bkash;
use SabitAhmad\Bkash\Exceptions\BkashException;
try {
$agreement = Bkash::createAgreement('CUST-001');
if ($agreement->isSuccess()) {
// Redirect user to bKash for agreement
return redirect()->away($agreement->getAgreementUrl());
}
throw new Exception('Agreement creation failed: ' . $agreement->getErrorMessage());
} catch (BkashException $e) {
// Handle exception
}
// After user completes agreement on bKash page
$agreement = Bkash::executeAgreement($paymentId);
if ($agreement->isSuccess()) {
$agreementId = $agreement->getAgreementId();
// Store agreementId for future payments
}
try {
$payment = Bkash::createPayment(
payerReference: 'CUST-001',
amount: 100.50,
invoiceNumber: 'INV-001'
);
if ($payment->isSuccess()) {
return redirect()->away($payment->getPaymentUrl());
}
throw new Exception('Payment creation failed: ' . $payment->getErrorMessage());
} catch (BkashException $e) {
// Handle exception
}
try {
$payment = Bkash::createPayment(
payerReference: 'CUST-001',
amount: 100.50,
invoiceNumber: 'INV-001',
callbackURL: '/payment/callback',
agreementId: 'AGR123456' // Use existing agreement
);
if ($payment->isSuccess()) {
return redirect()->away($payment->getPaymentUrl());
}
} catch (BkashException $e) {
// Handle exception
}
$execution = Bkash::executePayment($paymentId);
if ($execution->getTransactionStatus() === 'Completed') {
// Payment successful
$trxId = $execution->getTrxId();
}
Route::post('/bkash/callback', function (Request $request) {
$payment = Bkash::executePayment($request->paymentID);
if ($payment->isSuccess()) {
event(new PaymentCompleted($payment));
return redirect('/success');
}
return redirect('/failed');
});
$refund = Bkash::refundPayment('TRX123456', 100.50, 'Customer requested refund');
if ($refund->isSuccess()) {
$refundId = $refund->getRefundId();
echo "Refund successful. Refund ID: {$refundId}";
}
$query = Bkash::queryPayment('PAY123456');
if ($query->isCompleted()) {
echo "Payment completed successfully";
echo "Transaction ID: " . $query->getTrxId();
}
$search = Bkash::searchTransaction('TRX123456');
if ($search->isSuccess()) {
$transactions = $search->getTransactions();
foreach ($transactions as $transaction) {
echo "Amount: " . $transaction['amount'];
echo "Status: " . $transaction['transactionStatus'];
}
}
$refundStatus = Bkash::refundStatus('PAY123456', 'REF789012');
if ($refundStatus->isSuccess()) {
echo "Refund Status: " . $refundStatus->getTransactionStatus();
}
// Query Agreement
$agreement = Bkash::queryAgreement('AGR123456');
// Cancel Agreement
$cancelResult = Bkash::cancelAgreement('AGR123456');
use SabitAhmad\Bkash\Helpers\BkashHelper;
$helper = new BkashHelper(app(Bkash::class));
// Process complete payment flow
$payment = $helper->processPayment('CUST-001', 100.50, 'INV-001');
// Handle callback
$result = $helper->handleCallback($request->all());
// Get payment status with detailed info
$status = $helper->getPaymentStatus('PAY123456');
// Generate unique references
$payerRef = BkashHelper::generatePayerReference('CUSTOMER');
$invoiceNum = BkashHelper::generateInvoiceNumber('ORDER');
protected $listen = [
\SabitAhmad\Bkash\Events\PaymentCompleted::class => [
\App\Listeners\HandlePaymentCompleted::class,
],
\SabitAhmad\Bkash\Events\PaymentFailed::class => [
\App\Listeners\HandlePaymentFailed::class,
],
];
bash
php artisan vendor:publish --tag="laravel-bkash-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-bkash-config"
bash)
[//]: # (php artisan vendor:publish --tag="laravel-bkash-views")
[//]: # (