用PHP集成bKash支付的完整步骤
# PHP集成bKash支付的完整步骤
1. 准备工作
注册bKash商家账户
– 访问[bKash商户门户](https://merchant.bka.sh/)注册账户
– 完成必要的验证流程
– 获取API凭证(API Key, API Secret, Username和Password)
环境要求
– PHP >=7.0 (推荐7.4+)
– cURL扩展已启用
– SSL证书(生产环境必须使用HTTPS)
2. bKash API配置
“`php
// config.php – bKash API配置参数
define(‘BKASH_BASE_URL’, ‘https://tokenized.sandbox.bka.sh/v1.2.0-beta’); // SandboxURL
// define(‘BKASH_BASE_URL’, ‘https://tokenized.pay.bka.sh/v1.2.0-beta’); // ProductionURL
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生成与处理
“`php
// token_generator.php – bKash授权令牌获取功能
function getBkashToken() {
$post_token = array(
“app_key” => BKASH_APP_KEY,
“app_secret” => BKASH_APP_SECRET,
);
$url = BKASH_BASE_URL . “/tokenized/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);
}
}
try{
return json_decode(curl_exec ($ch),true) ;
}catch(Exception$e){return null;}
finally{if(is_resource ($ch)){@close();}}
}
“`
4.创建支付请求
“` php
=$_[…]){
/* Refresh token logic here */}
/* Prepare payment data */
$_data=[…];
/* Make API call to create payment */
[…]
}
}?>
“`
5.回调处理示例:
success_callback.html:
“`
“`
6.完整的类实现(可选):
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
“`
6.回调处理与验证:
verify_payment_status.PHP:
“`
true,…]));
}catch(…){http response code(…);}
“`
7.完整的类方法示例:
class.Bk ash Payment Gateway.PHP (续):
“`
public function refundTransaction(
string $_ payment ID,
float $_ amount,
string $_ trx ID ,
string $_ reason =””
): array {
/* Validate inputs */
return$this-> make Authenticated Request (
“/[…]/refund”,
compact (‘[…]’)
);
}
“`
8. Webhook处理(推荐):
webhook_handler.PHP:
“`