If you run a custom PHP website in Bangladesh, sooner or later a customer will ask, "Can I pay with bKash?" This guide walks through bKash payment gateway integration in PHP from a practical point of view: how the payment flow works, what your PHP code needs to do at each step, the security checks you should never skip, and how to add Nagad, Rocket, Upay, Cellfin and internet banking through the same integration using KarentPay.
The guide is written for plain (core) PHP projects. If you use Laravel or WordPress, the same concepts apply, but you may prefer a framework package or the KarentPay WooCommerce plugin instead.
How bKash Payment Gateway Integration in PHP Works
Every hosted payment gateway follows the same basic pattern. Your PHP site never touches the customer's bKash PIN; it only creates a payment request, sends the customer to a secure payment page, and then confirms the result on the server side.
- Customer places an order on your PHP site and chooses "Pay online".
- Your server creates a payment request with the amount, an order/invoice ID and a callback (return) URL, authenticated with your merchant API credentials.
- The gateway returns a payment URL, and your site redirects the customer there.
- The customer pays with bKash (or Nagad, Rocket, bank and so on) on the secure checkout page.
- The customer is sent back to your callback URL, and your server verifies the transaction with the gateway before marking the order as paid.
The most common mistake in PHP integrations is trusting the redirect alone. A redirect URL can be copied or edited by anyone, so step 5 (server-side verification) is the part that actually protects your money.
What You Need Before You Start
- A PHP 7.4+ (ideally PHP 8.x) website running on HTTPS with a valid SSL certificate.
- The cURL extension enabled, so PHP can call the gateway API.
- A database table for orders with at least: order ID, amount, status (pending/paid/failed) and the gateway transaction reference.
- A merchant account with a payment gateway such as KarentPay, and your API credentials.
- The official API documentation. For KarentPay, read the developer docs at karentpay.com/documentations for the exact endpoints, parameters and response format.
Step-by-Step: Integrating bKash Payments in Core PHP
1. Store credentials safely
Keep your API keys in environment variables or a config file outside the public web root, never in JavaScript or a public repository. Load them in PHP with getenv() or a small config include.
2. Create the order first
Before talking to the gateway, insert the order into your database with status pending and the final amount calculated on the server. Never accept the amount from a hidden form field, because customers can edit it in the browser.
3. Send the payment request with cURL
Use PHP cURL to send a POST request to the gateway's "create payment" endpoint with the amount, currency (BDT), your order ID and your callback URL. The general shape looks like this (illustrative only; use the field names from the official documentation):
$ch = curl_init($createPaymentUrl);- Set headers for your API key and
Content-Type: application/json. - Send
json_encode(['amount' => $amount, 'invoice' => $orderId, 'callback_url' => $callback]). - Decode the JSON response and read the payment URL.
4. Redirect the customer
If the response is successful, save the gateway reference against the order and redirect with header('Location: ' . $paymentUrl); exit;. Show a friendly error if the request fails, and log the response for debugging.
5. Handle the callback and verify
On your callback page, read the transaction reference, then call the gateway's verification/status endpoint from your server. Only when the gateway confirms the payment as successful, and the verified amount matches the order amount, update the order to paid.
6. Make it idempotent
Customers refresh pages and networks retry requests. Check whether an order is already marked paid before updating it, so a single payment never ships two orders or sends two confirmation emails.
Security Checklist for PHP Payment Integration
- Always use HTTPS for checkout, callback and API calls.
- Calculate prices on the server; never trust amounts from the browser.
- Verify every payment server-to-server before fulfilling an order.
- Use prepared statements (PDO) for all database queries.
- Keep API keys out of your code repository and front-end files.
- Log gateway responses (without sensitive personal data) so you can reconcile later.
- Test the full flow, including failed and cancelled payments, before going live.
Why Use One Gateway Instead of Separate bKash, Nagad and Bank Integrations?
You could integrate each wallet separately, but every provider has its own approval process, API style and maintenance. A unified Bangladeshi payment gateway like KarentPay lets your PHP site accept bKash, Nagad, Rocket, Upay, Cellfin, internet banking and various banks through a single integration, so you write the create-payment and verify logic once.
- One API for multiple payment methods.
- Support for both auto and manual transfer flows.
- SSL-secured payment processing.
- A ready-made WooCommerce plugin (search "KarentPay" on WordPress.org) if part of your business runs on WordPress.
- Developer documentation at karentpay.com/documentations.
Frequently Asked Questions
How do I integrate bKash payment gateway in PHP?
Create the order on your server, send a create-payment request to the gateway with cURL, redirect the customer to the returned payment URL, then verify the transaction on your callback page before marking the order paid.
Can I integrate bKash in PHP without Laravel?
Yes. Core PHP with the cURL extension is enough. Frameworks only organise the same steps differently.
Is a bKash payment gateway integration secure?
It is secure when you use HTTPS, keep keys server-side and verify every payment with the gateway rather than trusting the redirect.
Can the same PHP code accept Nagad and bank payments?
With a unified gateway like KarentPay, yes. The customer chooses bKash, Nagad, Rocket, Upay, Cellfin or internet banking on the checkout page, while your PHP code stays the same.
Where can I find the API documentation?
KarentPay's developer documentation is available at karentpay.com/documentations.
Start Accepting bKash Payments on Your PHP Site
A clean bKash payment gateway integration in PHP comes down to three things: server-side pricing, a secure redirect, and strict verification. Do those well and your checkout will be fast for customers and safe for you. Ready to add bKash, Nagad, Rocket, Upay, Cellfin and internet banking to your website through one API? Create your KarentPay merchant account at karentpay.com and start integrating today.

