Interactive Guide

Integrating the PHP bKash Payment Module

Payment Flow Overview

This section provides a high-level visual of the entire payment process, from the user initiating a payment to the final confirmation. Each step in the diagram is clickable and will navigate you to the detailed integration instructions for that part of the flow.

👤
User Input
`add_funds.php`
⚙️
Create Payment
`pay.php`
📱
bKash Gateway
User Approval
🔄
Process Callback
`callback.php`
Success
`success.php`

Exploring the Module Files

The module is organized into a few key files. This interactive file explorer helps you understand the role of each component. Click on a file name to view its code and a summary of its responsibilities. This clarifies what each part of the system does before you begin integration.

File Structure

  • your-project/
  • ├── 📂 bkash/
    • 📄 BkashPayment.php
  • ├── 📄 config.php
  • ├── 📄 add_funds.php
  • ├── 📄 pay.php
  • ├── 📄 callback.php

Select a file on the left to view its details.

Step-by-Step Integration

Follow these steps to integrate the bKash module into your own PHP project. Each card below represents a crucial step in the process, from file setup to customizing the logic for your specific application needs.

Step 1: Copy Core Files

Begin by placing the module files into your project directory. The core logic is encapsulated in `BkashPayment.php`, which should be placed in its own `bkash/` folder for clean organization.

Step 2: Set Up Database

Run the following SQL script in your project's database. This creates a `transactions` table to log all payment details and a `users` table with a default guest user for the no-login setup.

CREATE TABLE IF NOT EXISTS `users` ( ... );
CREATE TABLE IF NOT EXISTS `transactions` ( ... );

-- Add a default guest user for no-login transactions
INSERT INTO `users` (`id`, `username`, `balance`) 
VALUES(1, 'guest_transaction_user', 0.00)
ON DUPLICATE KEY UPDATE username = 'guest_transaction_user';

Step 3: Configure the Module

Edit `config.php`. This is the most critical step. You must provide your API credentials and, most importantly, set the `app_base_url` This URL is where bKash sends the user back after a payment attempt. If it's wrong, the callback will fail and the payment won't be verified. to your project's live domain.

<?php
return [
    'api_credentials' => [ /* ... Your credentials ... */ ],
    'charge_percentage' => 2.0,
    'app_base_url' => 'https://your-live-website.com', // <-- IMPORTANT
];

Step 4: Integrate the Payment Form

In your project's checkout or payment page, add an HTML form that posts to `pay.php`. The `amount` can be set dynamically from your application's logic (e.g., shopping cart total).

<!-- The bKash Payment Form -->
<form action="pay.php" method="POST">
    <!-- The amount should come from your application's logic -->
    <input type="hidden" name="amount" value="1250.00">

    <div>
        <label for="payerReference">Your bKash Number:</label>
        <input type="text" name="payerReference" required>
    </div>

    <button type="submit">Pay with bKash</button>
</form>

Step 5: Customize Callback Logic

This is where you connect the payment confirmation to your application's business logic. Open `callback.php` and add your code after the payment is successfully executed. For example, you might update an order's status in your own `orders` table.

// Inside callback.php, in the 'try' block...
$executeResponse = $bkash->executePayment($idToken, $paymentID);

// --- YOUR PROJECT'S LOGIC GOES HERE ---
$order_id = $_SESSION['current_order_id'];
$update_stmt = $conn->prepare("UPDATE orders SET status = 'paid' WHERE id = ?");
$update_stmt->bind_param("i", $order_id);
$update_stmt->execute();

// The module's transaction logging will run after your code.

Live Demo Simulation

This is a non-functional demo to help you visualize the payment flow you saw in the overview. Enter any valid-looking information and click "Simulate Payment". The flowchart at the top of the page will animate, highlighting each step of the process as it happens, from initiation to success.