Architecture
A payment passes through four places. Only the Worker knows your bKash credentials, and only bKash's page ever sees the customer's PIN.
- Your post:
blogger.jsopenspopup.html?amount=…&invoice=…in a popup window. - Checkout popup:
POST {apiBase}/createasks the Worker for a payment. The Worker gets a grant token, calls bKash'screateAPI and returns abkashURL. - bKash: the popup navigates to
bkashURL, where the customer enters their number, OTP and PIN. - Callback: bKash redirects to
{worker}/callback. The Worker callsexecute(orpayment/statusif that fails) and redirects back to the popup with the result. The popup sends the result to your post withpostMessage.
| File | Role |
|---|---|
blogger.js | Script for your blog. Turns elements into pay buttons, opens the popup, fires events. |
popup.html, popup.css, popup.js | The checkout window: review, hand-off to bKash, result. |
config.js | Your Worker URL and shop name. Empty apiBase means demo mode. |
bkash-payment.js | Browser client for the Worker, used by the popup. Refuses to redirect anywhere but bKash. |
worker/src/index.js | Cloudflare Worker: bKash Tokenized Checkout (grant, create, execute, status). |
worker/wrangler.toml | Worker settings for the Wrangler CLI. |
Installing blogger.js
Load the script from your GitHub Pages fork, in a post, a gadget or the theme. Loading it more than once on a page is harmless.
<script src="https://YOUR-GITHUB-NAME.github.io/bkash-blogger-payment/blogger.js"></script>
Optional attributes on the script tag:
| Attribute | Default | Description |
|---|---|---|
data-popup | popup.html next to the script | URL of the checkout popup. |
data-api | none | Your Worker URL. Needed only for bkashBlogger.verify(). |
data-toast | true | Set to false to hide the built-in result notices. |
Button attributes
Any element with data-bkash-amount or data-bkash-amount-from becomes a payment button, including elements added after the page loads.
| Attribute | Description |
|---|---|
data-bkash-amount | Price in BDT, such as 500 or 99.50. 1,500, ৳ 990, Tk. 500 and Bangla digits (৫০০) also work. |
data-bkash-amount-from | ID of an input, select or element holding the amount, for customer-chosen amounts. The input's min is respected. |
data-bkash-product | Product name shown in the checkout and sent to the Worker. Must match PRICES if you use it. |
data-bkash-success-url | Page to open after a successful payment, such as a thank-you page. |
data-bkash-paid-text | Button text after success. Default: Paid ✓. |
data-bkash-invoice-prefix | Prefix for generated invoice numbers. Default: INV. |
data-bkash-style="none" | Keep your own button styling instead of the built-in bKash look. |
JavaScript API
bkashBlogger.pay(options)
Opens the checkout. Returns a promise that resolves when the checkout ends, either with a success or when the window closes. It resolves with the last result (see event detail).
bkashBlogger.pay({
amount: 500, // required
product: 'Premium eBook', // optional
invoice: 'ORDER-1042', // optional, generated if missing
onSuccess: function (detail) { console.log(detail.trxID); },
onFailure: function (detail) { console.log(detail.message); },
onCancel: function (detail) {},
onClose: function (detail) {},
}).then(function (result) {
if (result.status === 'success') {
// show the download link
}
});
bkashBlogger.initButton(buttonId, amountInputId, options)
Wires an existing button by ID. amountInputId is optional; options takes the same callbacks as pay(). Kept for snippets written for version 1.
bkashBlogger.verify(paymentID)
Asks your Worker for the authoritative status from bKash. Requires data-api on the script tag. Resolves with { verified, transactionStatus, trxID, amount, invoice }.
bkashBlogger.configure(options)
Changes popupUrl, apiBase or toast at runtime.
Events
Events bubble from the button that started the payment, or fire on document if there isn't one.
| Event | When |
|---|---|
bkash:success | bKash completed the payment. |
bkash:failure | bKash refused or couldn't complete the payment. The customer may still retry. |
bkash:cancel | The customer cancelled on bKash's page or in the checkout. |
bkash:result | After any of the three above. |
bkash:close | The checkout window closed. |
event.detail contains:
| Field | Description |
|---|---|
status | success, failure, cancel or closed |
trxID | bKash transaction ID (success only) |
paymentID | bKash payment ID, usable with verify() |
invoice, amount, product | What was paid for |
message | Reason for a failure, if bKash gave one |
demo | true when the checkout ran in demo mode |
Checkout popup
config.js
| Setting | Description |
|---|---|
apiBase | Your Worker URL. Leave empty for demo mode. |
merchantName | Shown to customers as the payee. |
URL parameters
| Parameter | Set by | Description |
|---|---|---|
amount | blogger.js / Worker | Amount in BDT. Required to start. |
invoice | blogger.js / Worker | Merchant invoice number. |
product | blogger.js | Product name to display. |
origin | blogger.js | Your blog's origin; the only place the result is posted to. |
return | blogger.js | Page to go back to when the checkout ran in the same tab. |
status | Worker | success, failure or cancel. Switches the popup to the result screen. |
paymentID, trxID, message | Worker | Result details from bKash. |
When the popup is blocked, the checkout runs in the same tab and sends the customer back to return with bkash_status, bkash_trx, bkash_invoice, bkash_amount and bkash_payment. blogger.js reads them, fires the usual events and removes them from the address bar.
Worker API
| Endpoint | Description |
|---|---|
GET / | Health check: { ok, mode, configured, missing }. |
POST /create | Body { amount, invoice?, product? }. Returns { paymentID, bkashURL, amount, invoice }. |
GET /callback | bKash redirects here with paymentID and status. Executes the payment and redirects to POPUP_URL. |
GET /verify?paymentID= | Returns { verified, transactionStatus, trxID, amount, currency, invoice } straight from bKash. |
Environment variables
| Name | Description |
|---|---|
BKASH_USERNAME, BKASH_PASSWORD, BKASH_APP_KEY, BKASH_APP_SECRET | Secrets. Your Tokenized Checkout credentials. |
POPUP_URL | Full URL of popup.html on your GitHub Pages. |
BKASH_MODE | sandbox (default) or live. |
ALLOWED_ORIGINS | Comma-separated sites allowed to call the Worker from a browser. Defaults to the origin of POPUP_URL. Add your blog if you use verify() there. |
PRICES | JSON such as {"Premium eBook": 500, "Donation": "any"}. When set, only these products can be paid, at these prices. |
MAX_AMOUNT | Reject payments above this many taka. |
BKASH_BASE_URL | Override the bKash API URL (rarely needed). |
Errors
Errors come back as { "error": { "code", "message" } }, and the popup shows the message to the customer.
| Code | HTTP | Meaning |
|---|---|---|
invalid_amount | 400 | Not a positive number with at most two decimals. |
unknown_product, price_mismatch | 400 | Rejected by PRICES. |
amount_too_large | 400 | Above MAX_AMOUNT. |
origin_not_allowed | 403 | The calling site isn't in ALLOWED_ORIGINS. |
not_configured | 500 | A secret or POPUP_URL is missing. |
bkash_error | 502 | bKash refused the request; message and bkashCode say why. |
bkash_unreachable | 502 | bKash didn't answer within 30 seconds. |
Security model
- Customer secrets stay with bKash. The checkout never asks for a number, OTP or PIN; bKash's own page does.
- Merchant secrets stay on the Worker. Nothing in your fork or on Blogger is secret.
- Redirects are pinned to bKash. The popup only navigates to
https://hosts underbkash.comorbka.sh. - The popup only answers your blog. Results are posted to the
originthat opened it, andblogger.jsonly accepts messages from the popup's origin. - The popup can't be framed and ships a strict Content Security Policy.
- Prices can be enforced on the server with
PRICES.
Blogger has no server, so a determined visitor can fake a success event in their own browser. Use events to improve the experience, and confirm the transaction ID in your merchant portal or with /verify before you deliver anything valuable.
Browser support
Current Chrome, Edge, Firefox and Safari on desktop and mobile. In-app browsers that block popups, like Facebook's, fall back to checkout in the same tab and return to your post afterwards.