PHPackages                             theihasan/laravel-bkash - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. theihasan/laravel-bkash

ActiveLibrary[Payment Processing](/categories/payments)

theihasan/laravel-bkash
=======================

This is my package laravel-bkash

1.3.0(10mo ago)58892↓50%24[1 PRs](https://github.com/theihasan/laravel-bkash/pulls)MITPHPPHP ^8.1|^8.2CI passing

Since Mar 14Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/theihasan/laravel-bkash)[ Packagist](https://packagist.org/packages/theihasan/laravel-bkash)[ Docs](https://github.com/theihasan/laravel-bkash)[ GitHub Sponsors]()[ RSS](/packages/theihasan-laravel-bkash/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (6)Used By (0)

[![Free Palestine](https://private-user-images.githubusercontent.com/142471724/430788404-2b796609-c819-4cf6-b454-993e47a6e0f2.jpeg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzQ0MjA5NDQsIm5iZiI6MTc3NDQyMDY0NCwicGF0aCI6Ii8xNDI0NzE3MjQvNDMwNzg4NDA0LTJiNzk2NjA5LWM4MTktNGNmNi1iNDU0LTk5M2U0N2E2ZTBmMi5qcGVnP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MDMyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjAzMjVUMDYzNzI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWMzNWRjOTQ0NTVlYzRkZmQwZGQ3MzcyOTk2NDA3OWJlY2VjYjBkNTQ2YzZhZDZkMDA3OWRiNmVjOTc4Nzg3NyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.sV8-sbp5zrD9E2VfA5QQrSDCOvUuM4biPqYUISgUNTA)](https://private-user-images.githubusercontent.com/142471724/430788404-2b796609-c819-4cf6-b454-993e47a6e0f2.jpeg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzQ0MjA5NDQsIm5iZiI6MTc3NDQyMDY0NCwicGF0aCI6Ii8xNDI0NzE3MjQvNDMwNzg4NDA0LTJiNzk2NjA5LWM4MTktNGNmNi1iNDU0LTk5M2U0N2E2ZTBmMi5qcGVnP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MDMyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjAzMjVUMDYzNzI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWMzNWRjOTQ0NTVlYzRkZmQwZGQ3MzcyOTk2NDA3OWJlY2VjYjBkNTQ2YzZhZDZkMDA3OWRiNmVjOTc4Nzg3NyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.sV8-sbp5zrD9E2VfA5QQrSDCOvUuM4biPqYUISgUNTA)

Laravel bKash
=============

[](#laravel-bkash)

A simple Laravel package for integrating the bKash Tokenized Payment Gateway into your application. With built-in payment flow and full control via manual methods, this package supports payment creation, execution, status queries, refunds, and token management.

---

Table of Contents
-----------------

[](#table-of-contents)

1. [Overview](#overview)
2. [Requirements](#requirements)
3. [Installation](#installation)
4. [Configuration](#configuration)
5. [Usage](#usage)
    - [Initiating a Payment](#initiating-a-payment)
    - [Handling the Callback](#handling-the-callback)
    - [Querying Payment Status](#querying-payment-status)
    - [Processing a Refund](#processing-a-refund)
    - [Managing Tokens](#managing-tokens)
6. [Built-in Payment Flow](#built-in-payment-flow)
7. [Error Handling](#error-handling)
8. [Customization](#customization)
9. [Test Credentials](#test-credentials)
10. [Testing](#testing)
11. [Contributing](#contributing)
12. [Credits and License](#credits-and-license)

---

Overview
--------

[](#overview)

The **Laravel bKash** package simplifies integrating bKash’s tokenized payment gateway into your Laravel projects. It provides:

- Quick installation and configuration.
- Built-in controllers, routes, and views for out-of-the-box payment flow.
- Manual methods for complete control.
- Detailed error handling through custom exceptions.

---

Requirements
------------

[](#requirements)

- **PHP:** 8.0 or higher
- **Laravel:** 8.x or later
- **cURL Extension:** Enabled

---

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require theihasan/laravel-bkash
```

Publish the migrations

```
php artisan vendor:publish --tag=bkash-migrations
```

Migrate the database

```
php artisan migrate
```

Then, run the setup command to test the connection and publish assets:

```
php artisan bkash:setup --test --publish-views --publish-controllers
```

Alternatively, publish individual assets as needed:

- **Configuration:**```
    php artisan vendor:publish --tag="bkash-config"
    ```

---

Configuration
-------------

[](#configuration)

After publishing, update the `config/bkash.php` file with your bKash credentials and settings:

```
return [
    'sandbox' => env('BKASH_SANDBOX', true),

    'credentials' => [
        'app_key'    => env('BKASH_APP_KEY', ''),
        'app_secret' => env('BKASH_APP_SECRET', ''),
        'username'   => env('BKASH_USERNAME', ''),
        'password'   => env('BKASH_PASSWORD', ''),
    ],

    'sandbox_base_url' => env('SANDBOX_BASE_URL', 'https://tokenized.sandbox.bka.sh'),
    'live_base_url'    => env('LIVE_BASE_URL', 'https://tokenized.pay.bka.sh'),

    'version' => 'v1.2.0-beta',

    'cache' => [
        'token_lifetime' => 3600,
    ],

    'default_currency' => 'BDT',
    'default_intent'   => 'sale',

    'redirect_urls' => [
        'success' => '/payment/success',
        'failed'  => '/payment/failed',
    ],

    'routes' => [
        'enabled' => true,
    ],

        /*
    |--------------------------------------------------------------------------
    | Event Configuration
    |--------------------------------------------------------------------------
    | Configure whether to fire events on successful payments
    */
    'events' => [
        'payment_success' => env('BKASH_FIRE_PAYMENT_SUCCESS_EVENT', true),
    ],

        /*
    |--------------------------------------------------------------------------
    | Database Configuration
    |--------------------------------------------------------------------------
    */
    'database' => [
        'table_prefix' => env('BKASH_TABLE_PREFIX', 'bkash_'),
    ],
];
```

Also, add the necessary variables to your **.env** file:

```
BKASH_SANDBOX=true
BKASH_APP_KEY='0vWQuCRGiUX7EPVjQDr0EUAYtc'
BKASH_APP_SECRET='jcUNPBgbcqEDedNKdvE4G1cAK7D3hCjmJccNPZZBq96QIxxwAMEx'
BKASH_USERNAME='01770618567'
BKASH_PASSWORD='D7DaC '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());
    }
}
```

Multi-tenant Support
--------------------

[](#multi-tenant-support)

Starting from version 1.3.0, the package supports multi-tenant applications. This is useful when you have multiple tenants (organizations, businesses, etc.) using the same application but with different bKash credentials.

```
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());
    }
}
```

### Handling the Callback

[](#handling-the-callback)

After payment, bKash will redirect to your callback URL:

```
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');
}
```

Check a payment’s status using:

```
use Ihasan\Bkash\Facades\Bkash;

public function queryPaymentStatus($paymentId)
{
    try {
        $response = Bkash::queryPayment($paymentId);
        $status = $response['transactionStatus'];
        return response()->json([
            'success' => $status === 'Completed',
            'message' => 'Payment is ' . $status,
            'data'    => $response,
        ]);
    } catch (\Exception $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
    }
}
```

Multi-tenant Support
--------------------

[](#multi-tenant-support-1)

```
use Ihasan\Bkash\Facades\Bkash;

public function queryPaymentStatus($paymentId)
{
    try {
        $response = Bkash::forTenant($tenantId)->queryPayment($paymentId);
        $status = $response['transactionStatus'];
        return response()->json([
            'success' => $status === 'Completed',
            'message' => 'Payment is ' . $status,
            'data'    => $response,
        ]);
    } catch (\Exception $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
    }
}
```

### Processing a Refund

[](#processing-a-refund)

Initiate a refund (partial or full) with:

```
use Ihasan\Bkash\Facades\Bkash;

public function refundPayment(Request $request)
{
    $refundData = [
        'payment_id' => $request->input('payment_id'),
        'trx_id'     => $request->input('trx_id'),
        'amount'     => $request->input('amount'),
        'reason'     => $request->input('reason'),
    ];

    try {
        $response = Bkash::refundPayment($refundData);
        return response()->json(['success' => true, 'message' => 'Refund processed successfully', 'data' => $response]);
    } catch (\Exception $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
    }
}
```

Multi-tenant Support
--------------------

[](#multi-tenant-support-2)

```
use Ihasan\Bkash\Facades\Bkash;

public function refundPayment(Request $request)
{
    $refundData = [
        'payment_id' => $request->input('payment_id'),
        'trx_id'     => $request->input('trx_id'),
        'amount'     => $request->input('amount'),
        'reason'     => $request->input('reason'),
    ];

    try {
        $response = Bkash::forTenant($tenantId)->refundPayment($refundData);
        return response()->json(['success' => true, 'message' => 'Refund processed successfully', 'data' => $response]);
    } catch (\Exception $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
    }
}
```

### Managing Tokens

[](#managing-tokens)

For manual token operations:

```
// Get a token
$token = Bkash::getToken();

// Refresh a token
$token = Bkash::refreshToken();
```

---

Multi-tenant Support
--------------------

[](#multi-tenant-support-3)

### Token Management

[](#token-management)

Tokens are tenant-specific, so you can manage them for each tenant:

```
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();
```

Built-in Payment Flow
---------------------

[](#built-in-payment-flow)

By default, the package registers these routes:

- **GET /bkash/callback** – Payment callback handling.
- **GET /bkash/success** – Payment success page.
- **GET /bkash/failed** – Payment failure page.

To define your own routes, simply disable the built-in ones in `config/bkash.php` by setting:

```
'routes' => [
    'enabled' => false,
],
```

---

Error Handling
--------------

[](#error-handling)

The package provides clear exception classes to help you handle errors:

- **TokenGenerationException:** When token generation fails.
- **RefreshTokenException:** When token refresh fails.
- **PaymentCreationException:** When payment creation fails.
- **PaymentExecutionException:** When executing payment fails.
- **PaymentQueryException:** When payment status query fails.
- **RefundException:** When refund processing fails.

Handle exceptions as shown in the usage examples above.

---

Customization
-------------

[](#customization)

Customize the built-in views and controllers to match your needs:

- **Views:**

    ```
    php artisan bkash:setup --publish-views
    ```

    Files will be copied to `resources/views/vendor/bkash/`.
- **Controllers:**

    ```
    php artisan bkash:setup --publish-controllers
    ```

    Controllers will appear in `app/Http/Controllers/Vendor/Bkash/`. Adjust namespaces as needed.

---

Database Configuration
----------------------

[](#database-configuration)

Starting from version 1.1.0, you can customize the database table prefix used by the package. This is useful when you want to avoid table name conflicts or organize your database schema.

### Setting a Custom Table Prefix

[](#setting-a-custom-table-prefix)

By default, all tables created by this package use the `bkash_` prefix. You can change this by updating your `.env` file:

```
BKASH_TABLE_PREFIX=custom_prefix_
```

Or by directly modifying the `config/bkash.php` file:

```
'database' => [
    'table_prefix' => env('BKASH_TABLE_PREFIX', 'bkash_'),
],
```

### For Existing Installations

[](#for-existing-installations)

If you're updating from a previous version and want to use a custom table prefix:

1. Publish the new migration file:

    ```
    php artisan vendor:publish --tag="bkash-migrations"
    ```
2. Set your desired prefix in the `.env` file or config file.
3. Run the migration to create new tables with your prefix and migrate existing data:

    ```
    php artisan migrate
    ```

> **Note:** The migration will automatically copy your existing data to the new tables with your custom prefix. Your original tables will remain untouched, so you can verify the data before removing the old tables if needed.

### Important Considerations

[](#important-considerations)

- Changing the table prefix after you've already been using the package will create new tables with the new prefix.
- The package will automatically use the tables with the configured prefix.
- If you're using direct database queries in your application that reference these tables, make sure to update those queries to use the new table names.

---

Events
------

[](#events)

Starting from version 1.1.0, the package can fire Laravel events when certain actions occur. You can listen for these events to perform additional actions in your application.

### Available Events

[](#available-events)

#### Payment Successful Event

[](#payment-successful-event)

This event is fired when a payment is successfully executed:

```
Ihasan\Bkash\Events\PaymentSuccessful
```

The event contains:

- `$payment` - The BkashPayment model instance
- `$paymentData` - The raw payment data from bKash

### Configuring Events

[](#configuring-events)

By default, all events are enabled. You can disable specific events in your `.env` file:

```
BKASH_FIRE_PAYMENT_SUCCESS_EVENT=false
```

Or in your `config/bkash.php` file:

```
'events' => [
    'payment_success' => false,
],
```

### Listening for Events

[](#listening-for-events)

You can listen for these events in your `EventServiceProvider`:

```
protected $listen = [
    \Ihasan\Bkash\Events\PaymentSuccessful::class => [
        \App\Listeners\HandleSuccessfulPayment::class,
    ],
];
```

Or if you are using Laravel 11 or higher Laravel will automatically register the listener. Just run this command for listener

```
php artisan make:listener SendBkashPaymentNotification --event=PaymentSuccessful
```

Example listener:

```
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.
    }
}
```

Test Credentials
----------------

[](#test-credentials)

For sandbox testing, you may use these credentials (or update your **.env** accordingly):

- **Testing Numbers:**
    - 01929918378
    - 01619777283
    - 01619777282
    - 01823074817
- **OTP:** 123456
- **PIN:** 12121

---

Testing
-------

[](#testing)

Run package tests with:

```
composer test
```

Ensure your testing environment is set up as required by your Laravel configuration.

---

Contributing
------------

[](#contributing)

Contributions are welcome. When submitting a pull request:

- Follow PSR-4 coding standards.
- Include tests for new features or bug fixes.
- Update the documentation as needed.

---

Credits and License
-------------------

[](#credits-and-license)

**Credits:**

- Developed by [Abul Hassan](https://github.com/theihasan)
- Special thanks to:
    - [Ahmed Shamim Hassan Shaon](https://github.com/me-shaon) for his invaluable guidance in package development.
    - [Anis Uddin Ahmed](https://github.com/ajaxray) for his valuable insights and support.

**License:**
Licensed under the [MIT License](LICENSE.md).

---

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance53

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

5

Last Release

323d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a2e7cdad2cb0dbeaae3c80313aec7da5810a00c5a71760028980ec045613741e?d=identicon)[theihasan](/maintainers/theihasan)

---

Top Contributors

[![theihasan](https://avatars.githubusercontent.com/u/142471724?v=4)](https://github.com/theihasan "theihasan (70 commits)")

---

Tags

laravelphplaravellaravel-bkashMd Abul Hassan

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/theihasan-laravel-bkash/health.svg)

```
[![Health](https://phpackages.com/badges/theihasan-laravel-bkash/health.svg)](https://phpackages.com/packages/theihasan-laravel-bkash)
```

###  Alternatives

[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[danestves/laravel-polar

A package to easily integrate your Laravel application with Polar.sh

7812.3k](/packages/danestves-laravel-polar)[musahmusah/laravel-multipayment-gateways

A Laravel Package that makes implementation of multiple payment Gateways endpoints and webhooks seamless

852.2k1](/packages/musahmusah-laravel-multipayment-gateways)[creagia/laravel-redsys

Laravel Redsys Payments Gateway

2013.6k](/packages/creagia-laravel-redsys)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
