Bkash Payment Gateway

Controller Example

Controller Example

This is example controller. You need to modify it to fit your application's requirements.

Create PaymentController

Create PaymentController controller in the app/Http/Controllers/PaymentController.php file.

bash
php artisan make:controller PaymentController

Controller Dependencies

Before implementing the payment flow, import the required Laravel classes, application models, and the Bkash facade into your controller. These dependencies are used to process the checkout request, create the order, manage order items and inventory, handle database transactions, record errors, and initiate the Bkash payment session.

php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SyedMahamudul\Bkash\Bkash;

View Controller

View the controller example in the app/Http/Controllers/BkashPaymentController.php file.

php
  public function showForm()
    { 
        return view('payment.form');
    }

Initiate Payment Controller

Initiate payment controller example in the app/Http/Controllers/PaymentController.php file.

This is example controller. You need to modify it to fit your application's requirements.

php
  public function initiatePayment(Request $request)
    {
        try {
            // Validate
            $request->validate([
                'amount' => 'required|numeric|min:1'
            ]);
                  $inv = uniqid();
            // Create payment
            $result = Bkash::createPayment($request->amount,$inv);
            if ($result['success']) {
                // Redirect to bKash payment page
                return redirect()->away($result['payment_url']);
            }
           
            return back()->with('error', 'Payment initiation failed');

        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }

View Controller For Handle success callback

View the controller example in the app/Http/Controllers/PaymentController.php file.

php
 public function callback(Request $request)
    {
        $paymentId = $request->query('paymentID');

        if (!$paymentId) {
            return redirect()->route('payment.cancel')
                ->with('error', 'Invalid callback');
        }

        try {
            // Execute payment
            $result = Bkash::executePayment($paymentId);

            if ($result['success']) {
                return redirect()->route('payment.success')
                    ->with('success', 'Payment completed successfully');
            }

            return redirect()->route('payment.cancel')
                ->with('error', 'Payment execution failed');

        } catch (Exception $e) {
            return redirect()->route('payment.cancel')
                ->with('error', $e->getMessage());
        }
    }

Success

After payment success, you can show the success view with order details.

php
 public function success()
    {
        return view('payment.success');
    }

Payment failed

After payment failed, you can show the failed view with order details.

php
 public function failed()
    {
        return view('payment.failed');
    }

Payment cancel

After payment cancel, you can show the cancel view with order details.

php
 public function cancel()
    {
        return view('payment.cancel');
    }

Refund

After refund refund, you can show the success view with order details.

php
public function refund($bankID)
{
    $refund = Bkash::refund($bankID, 100, 'Customer request');

    if ($refund['status'] === 'SUCCESS') {
        return back()->with('success', 'Refund processed successfully!');
    }

    return back()->with('error', 'Refund failed!');
}

IPN

After IPN notification, you can show the success view with order details.

php
public function ipn(Request $request)
    {
        $transactionId = $request->input('tran_id');
        $status = $request->input('status');

        Log::info('IPN Received', [
            'transaction_id' => $transactionId,
            'status' => $status,
            'all_data' => $request->all()
        ]);

        if ($transactionId) {
            $updateData = ['updated_at' => now()];

            if ($status === 'VALID') {
                $updateData['status'] = 'completed';
                $updateData['bank_transaction_id'] = $request->input('bank_tran_id');
                $updateData['payment_date'] = now();
            } elseif ($status === 'FAILED') {
                $updateData['status'] = 'failed';
            }

            DB::table('orders')
                ->where('transaction_id', $transactionId)
                ->update($updateData);

            Log::info('Order updated via IPN', [
                'transaction_id' => $transactionId,
                'status' => $status
            ]);
        }

        return response()->json(['status' => 'success']);
    }

Configuration

The package supports Laravel Package Discovery, so there is no need to manually register the service provider or facade in your config/app.php file.

Add the following lines to your .env file:

Sandbox/Test Mode :

.env
      SSLC_SANDBOX=true
      SSLC_STORE_ID=your_sandbox_store_id
      SSLC_STORE_PASSWORD=your_sandbox_store_password
      SSLC_CURRENCY=BDT

Live/Production Mode :

.env
      SSLC_SANDBOX=false
      SSLC_STORE_ID=your_live_store_id
      SSLC_STORE_PASSWORD=your_live_store_password
      SSLC_CURRENCY=BDT

Package size

The core runtime is 90kb gzipped and has zero dependencies. The compiler and devtools packages are optional and installed separately.

Verify the install

Run php artisan Bkash:install to automatically publish the Bkash configuration file, register the service provider, and set up the required payment routes for your application.