Complete Steps to Integrate bKash Payment with PHP

# Complete Steps for PHP Integration with bKash Payments

1. Preparatory work

Register for a bKash Merchant Account
- Visit [bKash Merchant Portal](https://merchant.bka.sh/) to register for an account
- Completion of the necessary validation processes
- Get API credentials (API Key, API Secret, Username and Password)

Environmental requirements
- PHP >= 7.0 (7.4+ recommended)
- The cURL extension is enabled
- SSL certificate (HTTPS is required for production environments)

2. bKash API configuration

"`php
// config.php - bKash API configuration parameters
define('BKASH_BASE_URL', 'https://tokenized.sandbox.bka.sh/v1.2.0-beta'); // Sandbox URL
// define('BKASH_BASE_URL', 'https://tokenized.pay.bka.sh/v1.2.0-beta'); // Production URL

define('BKASH_APP_KEY', 'your_app_key');
define('BKASH_APP_SECRET', 'your_app_secret');
define('BKASH_USERNAME', 'your_username');
define('BKASH_PASSWORD', 'your_password');

// Callback URLs - replace with your actual URLs
define('BKASH_CALLBACK_SUCCESS_URL', 'https://yoursite.com/payment/success.php');
define('BKASH_CALLBACK_FAILURE_URL', 'https://yoursite.com/payment/failure.php');
“`

3. Token generation and processing

"`php
// token_generator.php - bKash authorisation token generator function

function getBkashToken() {
$post_token = array(
"app_key" => BKASH_APP_KEY.
"app_secret" => BKASH_APP_SECRET,
);

$url = BKASH_BASE_URL . "/tokenised/checkout/token/grant";

$headers = array(
"Content-Type: application/json".
"username: ". BKASH_USERNAME.
"password: ". BKASH_PASSWORD.
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_token));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT ,30);

// For production only (remove for sandbox testing)
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER ,true);
// curl_setopt ($ch,CURLOPT_CAINFO, "path_to_certificate");

if(!empty($_SERVER['HTTP_HOST'])){
if(strpos(BKAFH_BASE_URL,'sandbox') ! == false){
@ini_get("safe_mode") || set_time_limit(30);@ini_get("open_basedir") || ini_set( "max_execution_time",30); @ini_get("open_basedir") || ini_set(
}
}

try{
return json_decode(curl_exec ($ch),true) ;)
}catch(Exception$e){return null;}

finally{if(is_resource ($ch)){@close();}}
}
“`

4. Creating payment requests

"` php
=$_[…]){
/* Refresh token logic here */}

/* Prepare payment data */
$_data=[…];

/* Make API call to create payment */
[…]

}

}?>

Amount.

“`

5. Example of callback processing:

success_callback.html.

“`

“`

6. Complete class implementation (optional):

class.BkashPaymentGateway.PHP.

“`
“0011”, // Tokenized Payment (0011)
“payerReference” => “customer123”, // 客户唯一标识
“callbackURL” => BKASH_CALLBACK_SUCCESS_URL,
“amount” => (string)$amount,
“currency” => “BDT”,
“intent” => “sale”,
“merchantInvoiceNumber” => $invoiceNumber
];

$url = BKASH_BASE_URL . “/tokenized/checkout/create”;

try {
return makeBkashApiCall($url, json_encode($paymentData), [
‘Content-Type: application/json’,
‘Authorization: ‘.$id_token,
‘X-APP-Key: ‘.BKASH_APP_KEY
]);
} catch(Exception$e){return null;}
}

function makeBkashApiCall(string$_endpoint,$_payload=null,$headers=[],bool$_isPost=true){
/* … */
}
“`

5.执行支付流程

“` php

Amount.

“`

6. Callback handling and validation:

verify_payment_status.PHP.

“`
true,...])));

}catch(...){http response code(...);}
“`

7. A complete example of a class method:

Bk ash Payment Gateway.PHP (continued).

“`
public function refundTransaction(
string $_ Payment ID, string $_ Payment ID, string $_ Payment ID
float $_ amount,
string $_ trx ID ,
string $_ reason = ""
): array {

/* Validate inputs */

return$this-> make Authenticated Request (
"/[...]/refund".
compact ('[...]')
);

}
“`

8. Webhook processing (recommended):

webhook_handler.PHP.

“`