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

Rules for creating payment gateway of bkash

Nov 17, 2024 am 06:40 AM

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
<?php
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
<?php
$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' =&gt; $config['app_key'],
    'app_secret' =&gt; $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>
?&gt;<br>
ধাপ ৫: Create Payment (bkash/create_payment.php)<br>
php<br>
Copy code<br>
&lt;?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' =&gt; $amount,
    'currency' =&gt; 'BDT',
    'intent' =&gt; 'sale',
    'merchantInvoiceNumber' =&gt; 'INV-' . uniqid(),
    'callbackURL' =&gt; $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>
?&gt;<br>
ধাপ ৬: Execute Payment (bkash/execute_payment.php)<br>
php<br>
Copy code<br>
&lt;?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);
Copy after login

}

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
<?php
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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles