Home >Backend Development >PHP Tutorial >Rules for creating payment gateway of bkash

Rules for creating payment gateway of bkash

Linda Hamilton
Linda HamiltonOriginal
2024-11-17 06:40:03657browse

bkash এর পেমেন্ট গেটওয়ে তৈরী করার নিয়ম

If you want to setup Bkash Payment Gateway you need to follow some steps. Below I am giving a step by step guide on how you can integrate Bkash Payment Gateway. For this we will use Bkash's Merchant API.

1. Essentials for Bkash API Integration
Bkash Merchant Account: You must have a Bkash Merchant Account. If not you can apply for Bkash Merchant.
API Credentials: Bkash will provide you API Username, API Password, App Key, and App Secret after Merchant Account setup.
SSL Certificate: An SSL certificate is mandatory for Bkash payment gateway.
Webhook URL: You need to set a Webhook URL to receive payment notifications.
2. Payment Gateway Integration Process
Step 1: Environment Setup
First setup your PHP environment or any other server-side environment.

composer.json file (if using PHP)
json
Copy code
{
"require": {
"guzzlehttp/guzzle": "^7.0"
}
}
Then run composer install.

ধাপ ২: ফাইল এবং ডিরেক্টরি স্ট্রাকচার
plaintext
Copy code
project-root/
├── index.php
├── bkash/
│ ├── config.php
│ ├── token.php
│ ├── create_payment.php
│ ├── execute_payment.php
│ └── query_payment.php
└── success.php
ধাপ ৩: Bkash API Configuration (bkash/config.php)
php
Copy code
return [
'base_url' => 'https://checkout.sandbox.bka.sh/v1.2.0-beta',
'username' => 'YOUR_BKASH_USERNAME',
'password' => 'YOUR_BKASH_PASSWORD',
'app_key' => 'YOUR_APP_KEY',
'app_secret' => 'YOUR_APP_SECRET',
'callback_url' => 'https://fqrhost.com/success.php'
];
?>
ধাপ ৪: Access Token Generate (bkash/token.php)
php
Copy code
$config = include('config.php');

function generateToken() {
global $config;
$url = $config['base_url'] . '/checkout/token/grant';
$headers = [
'Content-Type:application/json',
'username:' . $config['username'],
'password:' . $config['password']
];

$data = [
    'app_key' => $config['app_key'],
    'app_secret' => $config['app_secret']
];

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);
curl_close($curl);

$response = json_decode($result, true);
return $response['id_token'];


</p>
<p>}</p>

<p>$token = generateToken();<br>
?><br>
ধাপ ৫: Create Payment (bkash/create_payment.php)<br>
php<br>
Copy code<br>
<?php<br>
include('token.php');</p>

<p>function createPayment($amount) {<br>
    global $config, $token;<br>
    $url = $config['base_url'] . '/checkout/payment/create';<br>
    $headers = [<br>
        'Content-Type:application/json',<br>
        'authorization:' . $token,<br>
        'x-app-key:' . $config['app_key']<br>
    ];</p>

<pre class="brush:php;toolbar:false">$data = [
    'amount' => $amount,
    'currency' => 'BDT',
    'intent' => 'sale',
    'merchantInvoiceNumber' => 'INV-' . uniqid(),
    'callbackURL' => $config['callback_url']
];

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);
curl_close($curl);

return json_decode($result, true);


</p>
<p>}</p>

<p>$response = createPayment(1000); // ১০০০ টাকা পেমেন্ট<br>
header('Location: ' . $response['bkashURL']);<br>
?><br>
ধাপ ৬: Execute Payment (bkash/execute_payment.php)<br>
php<br>
Copy code<br>
<?php<br>
include('token.php');</p>

<p>function executePayment($paymentID) {<br>
    global $config, $token;<br>
    $url = $config['base_url'] . '/checkout/payment/execute/' . $paymentID;<br>
    $headers = [<br>
        'Content-Type:application/json',<br>
        'authorization:' . $token,<br>
        'x-app-key:' . $config['app_key']<br>
    ];</p>

<pre class="brush:php;toolbar:false">$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);
curl_close($curl);

return json_decode($result, true);

}

if (isset($_GET['paymentID'])) {
$response = executePayment($_GET['paymentID']);
echo '

$data = [
    'app_key' => $config['app_key'],
    'app_secret' => $config['app_secret']
];

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);
curl_close($curl);

$response = json_decode($result, true);
return $response['id_token'];
';
}
?>
Step 7: Success Page (success.php)
php
Copy code
echo "Payment Successful!";
?>
3. System Testing
You can test your system using Bkash's Sandbox Environment.
To use Production Mode, update your API Key and URL from Bkash.
4. Webhook setup (optional)
Bkash can use Webhook URL for payment updates. A notification will be sent to you with payment information.

buy domain hosting

The above is the detailed content of Rules for creating payment gateway of bkash. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn