Reference

Everything blogger.js, the checkout popup and the Worker accept and return. New here? Start with the setup guide.

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.

  1. Your post: blogger.js opens popup.html?amount=…&invoice=… in a popup window.
  2. Checkout popup: POST {apiBase}/create asks the Worker for a payment. The Worker gets a grant token, calls bKash's create API and returns a bkashURL.
  3. bKash: the popup navigates to bkashURL, where the customer enters their number, OTP and PIN.
  4. Callback: bKash redirects to {worker}/callback. The Worker calls execute (or payment/status if that fails) and redirects back to the popup with the result. The popup sends the result to your post with postMessage.
FileRole
blogger.jsScript for your blog. Turns elements into pay buttons, opens the popup, fires events.
popup.html, popup.css, popup.jsThe checkout window: review, hand-off to bKash, result.
config.jsYour Worker URL and shop name. Empty apiBase means demo mode.
bkash-payment.jsBrowser client for the Worker, used by the popup. Refuses to redirect anywhere but bKash.
worker/src/index.jsCloudflare Worker: bKash Tokenized Checkout (grant, create, execute, status).
worker/wrangler.tomlWorker 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:

AttributeDefaultDescription
data-popuppopup.html next to the scriptURL of the checkout popup.
data-apinoneYour Worker URL. Needed only for bkashBlogger.verify().
data-toasttrueSet 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.

AttributeDescription
data-bkash-amountPrice in BDT, such as 500 or 99.50. 1,500, ৳ 990, Tk. 500 and Bangla digits (৫০০) also work.
data-bkash-amount-fromID of an input, select or element holding the amount, for customer-chosen amounts. The input's min is respected.
data-bkash-productProduct name shown in the checkout and sent to the Worker. Must match PRICES if you use it.
data-bkash-success-urlPage to open after a successful payment, such as a thank-you page.
data-bkash-paid-textButton text after success. Default: Paid ✓.
data-bkash-invoice-prefixPrefix 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.

EventWhen
bkash:successbKash completed the payment.
bkash:failurebKash refused or couldn't complete the payment. The customer may still retry.
bkash:cancelThe customer cancelled on bKash's page or in the checkout.
bkash:resultAfter any of the three above.
bkash:closeThe checkout window closed.

event.detail contains:

FieldDescription
statussuccess, failure, cancel or closed
trxIDbKash transaction ID (success only)
paymentIDbKash payment ID, usable with verify()
invoice, amount, productWhat was paid for
messageReason for a failure, if bKash gave one
demotrue when the checkout ran in demo mode

config.js

SettingDescription
apiBaseYour Worker URL. Leave empty for demo mode.
merchantNameShown to customers as the payee.

URL parameters

ParameterSet byDescription
amountblogger.js / WorkerAmount in BDT. Required to start.
invoiceblogger.js / WorkerMerchant invoice number.
productblogger.jsProduct name to display.
originblogger.jsYour blog's origin; the only place the result is posted to.
returnblogger.jsPage to go back to when the checkout ran in the same tab.
statusWorkersuccess, failure or cancel. Switches the popup to the result screen.
paymentID, trxID, messageWorkerResult 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

EndpointDescription
GET /Health check: { ok, mode, configured, missing }.
POST /createBody { amount, invoice?, product? }. Returns { paymentID, bkashURL, amount, invoice }.
GET /callbackbKash 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

NameDescription
BKASH_USERNAME, BKASH_PASSWORD, BKASH_APP_KEY, BKASH_APP_SECRETSecrets. Your Tokenized Checkout credentials.
POPUP_URLFull URL of popup.html on your GitHub Pages.
BKASH_MODEsandbox (default) or live.
ALLOWED_ORIGINSComma-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.
PRICESJSON such as {"Premium eBook": 500, "Donation": "any"}. When set, only these products can be paid, at these prices.
MAX_AMOUNTReject payments above this many taka.
BKASH_BASE_URLOverride the bKash API URL (rarely needed).

Errors

Errors come back as { "error": { "code", "message" } }, and the popup shows the message to the customer.

CodeHTTPMeaning
invalid_amount400Not a positive number with at most two decimals.
unknown_product, price_mismatch400Rejected by PRICES.
amount_too_large400Above MAX_AMOUNT.
origin_not_allowed403The calling site isn't in ALLOWED_ORIGINS.
not_configured500A secret or POPUP_URL is missing.
bkash_error502bKash refused the request; message and bkashCode say why.
bkash_unreachable502bKash 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 under bkash.com or bka.sh.
  • The popup only answers your blog. Results are posted to the origin that opened it, and blogger.js only 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.