Skip to main content

Python bindings for Bangladesh payment gateway SDK

Project description

bd-payment-gateway (Python)

bd-payment-gateway helps you collect online payments in Bangladesh using a Python API. It gives you a typed SSLCOMMERZ client for create-session, redirect, callback handling, and payment verification. You can start from environment variables and call strongly typed request models.

Supported Providers (Python)

  • SSLCOMMERZ: ✅ Supported and stable
  • PortWallet: ❌ Not supported yet (WIP)
  • shurjoPay: ❌ Not supported yet (WIP)
  • aamarPay: ❌ Not supported yet (WIP)

Only SSLCOMMERZ is supported right now in Python.

Install

pip install bd-payment-gateway

Configuration

Set these environment variables in your shell or .env file.

Environment variable Meaning Example
BDPG_SSLCOMMERZ_STORE_ID Your SSLCOMMERZ store ID testbox
BDPG_SSLCOMMERZ_STORE_PASSWD Your SSLCOMMERZ store password qwerty
BDPG_SSLCOMMERZ_ENVIRONMENT sandbox, production, or custom sandbox
BDPG_SSLCOMMERZ_CUSTOM_BASE_URL Required only when environment is custom https://sandbox.sslcommerz.com

Example:

BDPG_SSLCOMMERZ_STORE_ID=your_store_id
BDPG_SSLCOMMERZ_STORE_PASSWD=your_store_password
BDPG_SSLCOMMERZ_ENVIRONMENT=sandbox
# BDPG_SSLCOMMERZ_CUSTOM_BASE_URL=https://sandbox.sslcommerz.com

Quickstart (SSLCOMMERZ)

This example shows the full payment flow:

  1. Create payment session
  2. Send user to redirect URL
  3. Handle callback endpoints (success, fail, cancel, ipn)
  4. Verify payment status
from decimal import Decimal

from flask import Flask, jsonify, request

from bd_payment_gateway.errors import PaymentGatewayError
from bd_payment_gateway.sslcommerz import SslcommerzClient
from bd_payment_gateway.sslcommerz.models import (
    Customer,
    InitiatePaymentRequest,
    Product,
    Settings,
    Urls,
    VerifyPaymentRequest,
)

app = Flask(__name__)
settings = Settings()  # Reads BDPG_SSLCOMMERZ_* from env/.env
client = SslcommerzClient.from_settings(settings)


@app.post("/pay")
def pay() -> tuple[dict, int]:
    try:
        initiated = client.initiate_payment(
            InitiatePaymentRequest(
                total_amount=Decimal("500.00"),
                tran_id="TXN-10001",
                urls=Urls(
                    success_url="https://merchant.example/payment/success",
                    fail_url="https://merchant.example/payment/fail",
                    cancel_url="https://merchant.example/payment/cancel",
                    ipn_url="https://merchant.example/payment/ipn",
                ),
                customer=Customer(
                    name="Demo User",
                    email="demo@example.com",
                    address_line_1="Dhaka",
                    city="Dhaka",
                    country="Bangladesh",
                    phone="01700000000",
                ),
                product=Product(
                    name="Python Course",
                    category="Education",
                    profile="general",
                ),
            )
        )
    except PaymentGatewayError as err:
        return {"error": err.message, "code": err.code, "hint": err.hint}, 400

    # Frontend should redirect the customer to this URL.
    return {"redirect_url": str(initiated.redirect_url)}, 200


@app.get("/payment/success")
def payment_success() -> tuple[dict, int]:
    # SSLCOMMERZ sends sessionkey in callback query params.
    session_key = request.args.get("sessionkey", "")
    if not session_key:
        return {"error": "Missing sessionkey in callback"}, 400

    try:
        verified = client.verify_payment(
            VerifyPaymentRequest(session_key=session_key)
        )
    except PaymentGatewayError as err:
        return {"error": err.message, "code": err.code, "hint": err.hint}, 400

    return {
        "status": verified.status,
        "provider_reference": verified.provider_reference,
        "amount": str(verified.amount) if verified.amount else None,
    }, 200


@app.get("/payment/fail")
def payment_fail() -> tuple[dict, int]:
    return {"status": "failed"}, 200


@app.get("/payment/cancel")
def payment_cancel() -> tuple[dict, int]:
    return {"status": "cancelled"}, 200


@app.post("/payment/ipn")
def payment_ipn() -> tuple[dict, int]:
    # IPN = Instant Payment Notification from SSLCOMMERZ server.
    return jsonify({"received": True}), 200

Common Errors and Troubleshooting

  • ValidationError when loading Settings:
    • Cause: missing BDPG_SSLCOMMERZ_* values.
    • Fix: set required environment variables exactly as shown above.
  • PaymentGatewayError with provider error code:
    • Cause: wrong credentials, invalid callback URLs, or invalid transaction data.
    • Fix: check err.code, err.message, and err.hint in your except block.
  • Payment stays pending:
    • Cause: customer has not completed payment yet.
    • Fix: verify again after callback/IPN or poll with your own retry logic.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bd_payment_gateway-0.1.4.tar.gz (96.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bd_payment_gateway-0.1.4-cp39-abi3-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.9+Windows x86-64

bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl (3.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

bd_payment_gateway-0.1.4-cp39-abi3-macosx_11_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

bd_payment_gateway-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file bd_payment_gateway-0.1.4.tar.gz.

File metadata

  • Download URL: bd_payment_gateway-0.1.4.tar.gz
  • Upload date:
  • Size: 96.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bd_payment_gateway-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5dbc59998c3f92e96f67a06b7c94fae583b8dcc496f38e8e82fd7165f5005d0e
MD5 f6b6ac33343df6e57cc1a6efe8511968
BLAKE2b-256 bd3ca3248b076a5aa193651bc035cb6d1397370092cd8a15918a107b8bd9b2bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4.tar.gz:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bd_payment_gateway-0.1.4-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for bd_payment_gateway-0.1.4-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 224d49abf54007fcfe2165a2b1104dd4592e190453728e069162ad74d3b9b663
MD5 6ad8e9cfa0492418b17e486d02acab1b
BLAKE2b-256 9c76d4e4b097c8a2535792047b4dbe3c9e7f5200186d1fbf6040a319d93914be

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4-cp39-abi3-win_amd64.whl:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a54d670b940abad13e49847549d7eac752d43ffea1b82b8ab09f0cfd94ae7685
MD5 75b7fc11c503a63d739617dcb47cde7f
BLAKE2b-256 24d21e34beb99745d55f78d18427a7a5df3acd241518ed5555ec3954baa39ce4

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f781117d7ddda78bb91dd79ea8353eda075dc03bbb8228d370649eb1da50a494
MD5 0d2cf63a751b76a3ae9c87c61a6d94c6
BLAKE2b-256 5ccc7c09ce03bd4afa245f71667b1045db880603b12d41ed7cf4a7cdccfbc4ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bd_payment_gateway-0.1.4-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bd_payment_gateway-0.1.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed53de97fceb1ed6ac030baa1ed87ff39a1632025c502fb4c54eb5240ea12b86
MD5 b253cb395f0fecdd01bf33e62a7cf786
BLAKE2b-256 731e8084936f9c422f4614f8eca6e9d81e59b80529d55512d81ca94b8befdb40

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bd_payment_gateway-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bd_payment_gateway-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d8c81ecc99589195ec90dfc1a3c1cdbe5801c47f8d8a278e2c155516787825ec
MD5 42e365287ff72661b98f96647f0379ca
BLAKE2b-256 a9baa32992add819050fed7cd101186e938c9210546944977132c2de118f5e80

See more details on using hashes here.

Provenance

The following attestation bundles were made for bd_payment_gateway-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: publish-python.yml on Barrzen/bd-payment-gateway

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page