Python SDK for Bank of Maldives Connect API with sync/async support
Project description
BML Connect Python SDK
Python SDK for Bank of Maldives Connect API with synchronous and asynchronous support.
Compatible with all Python frameworks including Django, Flask, FastAPI, and Sanic.
Features
- Sync/Async Support: Choose your preferred programming style
- Full API Coverage: Transactions, webhooks, and signature verification
- Type Annotations: Full type hint support for better development experience
- Error Handling: Comprehensive error hierarchy for easy debugging
- Framework Agnostic: Works with any Python web framework
- MIT Licensed: Open source and free to use
Installation
pip install bml-connect-python
Quick Start
Synchronous Client
from bml_connect import BMLConnect, Environment
client = BMLConnect(
api_key="your_api_key",
app_id="your_app_id",
environment=Environment.SANDBOX
)
try:
transaction = client.transactions.create_transaction({
"amount": 1500, # 15.00 MVR
"currency": "MVR",
"provider": "alipay",
"redirectUrl": "https://yourstore.com/success",
"localId": "order_123",
"customerReference": "Customer #456"
})
print(f"Transaction ID: {transaction.transaction_id}")
print(f"Payment URL: {transaction.url}")
except Exception as e:
print(f"Error: {e}")
finally:
client.close()
Asynchronous Client
import asyncio
from bml_connect import BMLConnect, Environment
async def main():
client = BMLConnect(
api_key="your_api_key",
app_id="your_app_id",
environment=Environment.SANDBOX,
async_mode=True
)
try:
transaction = await client.transactions.create_transaction({
"amount": 2000,
"currency": "MVR",
"provider": "wechat",
"redirectUrl": "https://yourstore.com/success"
})
print(f"Transaction ID: {transaction.transaction_id}")
finally:
await client.aclose()
asyncio.run(main())
Webhook Verification
Flask Example
from flask import Flask, request, jsonify
from bml_connect import BMLConnect
app = Flask(__name__)
client = BMLConnect(api_key="your_api_key", app_id="your_app_id")
@app.route('/webhook', methods=['POST'])
def webhook():
payload = request.get_json()
signature = payload.get('signature')
if client.verify_webhook_signature(payload, signature):
# Process webhook
return jsonify({"status": "success"}), 200
else:
return jsonify({"error": "Invalid signature"}), 403
FastAPI Example
from fastapi import FastAPI, Request, HTTPException
from bml_connect import BMLConnect
app = FastAPI()
client = BMLConnect(api_key="your_api_key", app_id="your_app_id")
@app.post("/webhook")
async def handle_webhook(request: Request):
payload = await request.json()
signature = payload.get("signature")
if client.verify_webhook_signature(payload, signature):
return {"status": "success"}
else:
raise HTTPException(403, "Invalid signature")
Sanic Example
from sanic import Sanic, response
from bml_connect import BMLConnect
app = Sanic("BMLWebhook")
client = BMLConnect(api_key="your_api_key", app_id="your_app_id")
@app.post('/webhook')
async def webhook(request):
payload = request.json
signature = payload.get('signature')
if client.verify_webhook_signature(payload, signature):
return response.json({"status": "success"})
else:
return response.json({"error": "Invalid signature"}, status=403)
API Reference
Main Classes
BMLConnect: Main entry point for the SDK.Transaction: Transaction object.QRCode: QR code details.PaginatedResponse: For paginated transaction lists.Environment: Enum forSANDBOXandPRODUCTION.SignMethod: Enum for signature methods.TransactionState: Enum for transaction states.
Error Classes
BMLConnectErrorAuthenticationErrorValidationErrorNotFoundErrorServerErrorRateLimitError
Utilities
SignatureUtils.generate_signature(data, api_key, method)SignatureUtils.verify_signature(data, signature, api_key, method)
Development
Requirements
- Python 3.7+
- See
requirements.txtandrequirements-dev.txtfor dependencies.
Testing
pip install -e .[dev]
pytest
Formatting & Linting
black .
flake8 .
mypy .
Packaging
- Uses
pyproject.tomlfor build configuration. - Source code is in
src/bml_connect/. - Examples in
examples/. - Tests in
tests/.
License
MIT License. See LICENSE for details.
Links
Contributing
Pull requests and issues are welcome! See the documentation for guidelines.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bml_connect_python-1.0.0.tar.gz.
File metadata
- Download URL: bml_connect_python-1.0.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8b1ed26b4423dfdf547a55060a41fe1a84f52f20caeaaf9281e05ad4492ffe4
|
|
| MD5 |
42f50a48190458312ce9a996c876cc35
|
|
| BLAKE2b-256 |
cef64dce3a17527b0efd246ccf8d686a5a9e972436aec2378f6b9c629695ba25
|
File details
Details for the file bml_connect_python-1.0.0-py3-none-any.whl.
File metadata
- Download URL: bml_connect_python-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca1b4b5d342c6421966f8e81cafe0e546a8f4bf8c61a913a464e93deba8eca16
|
|
| MD5 |
9094334a53cf0be73ec0e48fe8e2bc31
|
|
| BLAKE2b-256 |
e856e3194f36c1c1a82381944399b25b9762dd9131b0d9ca083655ce2a5ce386
|