irfan-chowdhury/bkash-tokenized-checkout

This is a bkash tokenized payment gateway solution with laravel. Integrate with the payment solution that fits all business needs and Enrich the customers' online payment experience.

v1.3.1 2024-07-06 06:09 UTC

This package is auto-updated.

Last update: 2024-12-06 07:03:14 UTC


README

Bkash Payment Gatway Integration in Laravel

Requirements

"require": {
    "php": ">= 8.0",
    "laravel/framework": ">= 9.0"
},

Installation

You can install the package via composer :

composer require irfan-chowdhury/bkash-tokenized-checkout

Configuration

After completing the installation, service provider need to register in config/app.php, add in providers array -

'providers' => [
    /*
    * Package Service Providers...
    */
    IrfanChowdhury\BkashTokenizedCheckout\BkashServiceProvider::class,
],

Now run this publish this -

php artisan vendor:publish --provider="IrfanChowdhury\BkashTokenizedCheckout\BkashServiceProvider"

Environement Variable (.ENV)

Please follow the instructions given below -

Guideline to setup bKash

Then are some types of bKash integration. Here we used Tokenized solution.

Step-1 : Registration

First of all you need a marchant account and then need to register your website there.

  • Setup Account Info

  • Product Request

  • Product List

  • Overview

Step-2 : Sandbox Validation

First of all follow the developer documentation.

Visit the link : https://developer.bka.sh/docs/tokenized-checkout-overview

Our Sandbox Credentials will be look like that -

1. Grand Token

Request URL: {base_URL}/tokenized/checkout/token/grant Here the base_URL will be - https://tokenized.sandbox.bka.sh/v1.2.0-beta/

Now follow the screenshot given below -

in POSTMAN -

Headers

Body

Response Result -

2. Create Payment

Follow the format -

POST /tokenized/checkout/create HTTP/1.1
Host: {base_URL}
Content-Type: application/json
Accept: application/json
authorization: id_token
x-app-key: x-app-key

{  
   "mode": "0011",
   "payerReference": "01723888888",
   "callbackURL": "yourDomain.com",
   "merchantAssociationInfo": "MI05MID54RF09123456One"
   "amount": "500",
   "currency": "BDT",
   "intent": "sale",
   "merchantInvoiceNumber": "Inv0124"
}

in Postman -

Header -

Body -

Response

3. Execute Payment

First copy the bKashURL and then goto the link and make payment from their site first -

For an example, sample link look like-

https://sandbox.payment.bkash.com/?paymentId=TR0011hEN7KTZ1718449546941&hash=)gqMffGT.r.4*dLWGcc)mE_6q9U)wmmh9SI6hTlkjzNc!IFGOZZCN5Fe1I0FGRtIXxl!sNdP00LAv)mjDLg6iu8cAKr**0g(WHZC1718449546941&mode=0011&apiVersion=v1.2.0-beta/


After creating the payment, now goto postman and follow the format -

POST /tokenized/checkout/execute HTTP/1.1
Host: {base_URL}
Accept: application/json
authorization: id_token
x-app-key: x-app-key

{
	"paymentID" : "TR0011ON1565154754797"
}

in Postman -

Header -

Body -

Response

4. Verify Sandbox Validation

Create Payment Sandbox Test

Execute Payment Sandbox Test

Success Output

5. Live Credentials

After doing all these, finally you will get the Live Credentials.

Step-3 : Setup the bkash credentials in your site

.ENV

Put the value in .env file -

APP_URL=your_domain.com

# Sandbox Credentials
APP_URL=your_root_domain
BKASH_TOKENIZE_SANDBOX=true
BKASH_TOKENIZE_BASE_URL=https://tokenized.sandbox.bka.sh/v1.2.0-beta/tokenized
BKASH_TOKENIZE_VERSION="v1.2.0-beta"
BKASH_TOKENIZE_APP_KEY=your_sandbox_app_key
BKASH_TOKENIZE_APP_SECRET=your_sandbox_app_secret
BKASH_TOKENIZE_USER_NAME=01XXXXXXXXX
BKASH_TOKENIZE_PASSWORD=your_sandbox_app_password


# Live Credentials
# APP_URL=your_root_domain
# BKASH_TOKENIZE_SANDBOX=false
# BKASH_TOKENIZE_BASE_URL=https://tokenized.pay.bka.sh/v1.2.0-beta/tokenized
# BKASH_TOKENIZE_VERSION="v1.2.0-beta"
# BKASH_TOKENIZE_APP_KEY=your_live_app_key
# BKASH_TOKENIZE_APP_SECRET=your_live_app_secret
# BKASH_TOKENIZE_USER_NAME=01XXXXXXXXX
# BKASH_TOKENIZE_PASSWORD=your_live_app_password

First of all test your application by Sandbox. If all test ok then comment or hide the sandbox credentials and use the live credentials.

Try to run the package in your app

  1. Please type on the url your-domain.com/payment/checkout

This is the bkash page. Click on the pay now button.

After Payment done -

Override

1. BkashController

After published, the BkashController file will be copied in app/Https/Controllers. You'll see two method

    public function checkout()
    {
        return view('bkash::checkout');
    }


    public function bkashCallback(PaymentService $paymentService, Request $request)
    {
        try {
            $payment = $paymentService->initialize('bkash');

            $payment->paymentStatusCheck($request);

            session()->put('paymentID', $request->paymentID);

            // Implement your other business logic after payment done.

            return redirect()->route('payment.success')->with(['success' => 'Payment Successfully Done']);
        }
        catch (Exception $e) {

            return redirect()->route('checkout')->withErrors(['errors' => $e->getMessage()]);
        }
    }

You can override the two methods. The bkashCallback() indicates that when the payment done, it'll redirect back this method. This time you can customize your other bussiness logic what you want. You'll see a line which is in comment mode. Basically you should customize after this line.

2. routes/web.php

When you customize the controller, you need to use the route also. You can use only these line -

use App\Http\Controllers\BkashPaymentController;
use Illuminate\Support\Facades\Route;

Route::controller(BkashPaymentController::class)->group(function () {
    Route::prefix('payment')->group(function () {
        Route::get('checkout', 'checkout')->name('checkout');
        Route::get('bkash/callback','bkashCallback');
    });
});

If you need to change the payment/bkash/callback, you should also update the corresponding data in config/bkash.php. However, it is recommended not to make these changes.

3. Blade files

You can customize it according to your choice. After publishing, you will find it in the resources/views/vendor/bkash directory.

Demo

Though you'll get Sandox details from Bkash, but you can try by using these in your app.

References

Note: This repository is not associated with or promoting bKash and does not provide any guarantees.