TONB Merchant Backend API Python Client Library
Project description
TONB Merchant Backend API Python Client Library
Introduction
This library provides a Python client for the TONB Merchant backend API. It allows users to interact with the API endpoints for the management of invoices, such as creating, retrieving, canceling invoices, as well as handling webhooks and retrieving invoice statistics.
The library includes two dataclasses Invoice and Webhook, and a main client class TONBApiClient.
Installation
pip install tonb
Usage
First, import the API client class from the library:
from tonb import TONBApiClient
Create an instance of the API client:
api_client = TONBApiClient(base_url="<API_BASE_URL>", auth_token="<AUTH_TOKEN>", merchant_id="<MERCHANT_ID>")
Where:
<API_BASE_URL>is the base URL of the API<AUTH_TOKEN>is the authentication token used for the API requests<MERCHANT_ID>is the ID of the merchant
Now, you can use this instance to interact with the API.
Classes
Invoice
This data class defines the invoice object. It represents an invoice in the system. It can be used to retrieve or manipulate invoice data.
Attributes
id: Invoice's ID (int).status: Status of the invoice (str).code: The code of the invoice (str).amount: Amount to be paid (str).domain: The domain related to the invoice (str).order_id: The id of the order (int).transaction: The transaction associated with the invoice (Optional[str]).created_at: The time at which the invoice was created (datetime).updated_at: The time at which the invoice was last updated (datetime).user_from_id: The user ID from which the invoice is generated (Optional[str]).user_to_id: The user ID to which the invoice is issued (str).wallet_from_id: The wallet ID from which the invoice is generated (Optional[int]).wallet_to_id: The wallet ID to which the invoice is issued (int).user_from: The user from which the invoice is generated (Optional[str]).wallet_from: The wallet from which the invoice is generated (Optional[str]).
Static Methods
from_dict(source: dict): Creates an Invoice instance from a dictionary.
Webhook
This data class defines the webhook object. It represents a webhook data received from the TONB API.
Attributes
order_id: The order ID related to the webhook (int).amount: Amount related to the webhook (int).code: The code related to the webhook (str).status: The status related to the webhook (str).
Static Methods
from_dict(source: dict): Creates a Webhook instance from a dictionary.
TONBApiClient
This class is the main client class that provides methods to interact with the TONB Merchant backend API.
Attributes
base_url: Base URL for the API (str).timeout: Timeout for the request (int, default is 10).
Methods
create_invoice(amount: int, order_id: int): Creates a new invoice.get_invoice(invoice: Union[Invoice, int]): Retrieves an existing invoice.get_invoices_stats(): Retrieves statistics about invoices.cancel_invoice(invoice: Union[Invoice, int]): Cancels an existing invoice.handle_request(request: Request): Handles a request.
Examples
Below are a few examples demonstrating the usage of the TONB Merchant backend API Python client library.
Basic Operations
This example shows how to create and cancel an invoice and get statistics about invoices.
from tonb import TONBApiClient
# Create an instance of the API client
client = TONBApiClient(
"https://merchant-app-api2.tonb.io", "YOUR_API_KEY", "YOUR_MERCHANT_ID"
)
# Create an invoice with amount 1000000000 and order_id 12345
invoice = client.create_invoice(1000000000, 12345)
# Print the invoice details
print(invoice)
# Cancel the created invoice
client.cancel_invoice(invoice)
# Get and print the overall statistics about invoices
print(client.get_invoices_stats())
In the above example, replace "YOUR_API_KEY" and "YOUR_MERCHANT_ID" with your actual API key and Merchant ID.
Handling Webhooks
This example shows how to set up a Flask application to receive and validate webhooks using the handle_request() method.
from flask import Flask, request, jsonify
from tonb import TONBApiClient
# Create a Flask app
app = Flask(__name__)
# Create an instance of the API client
client = TONBApiClient(
"https://merchant-app-api2.tonb.io", "YOUR_API_KEY", "YOUR_MERCHANT_ID"
)
@app.route("/webhook", methods=["POST"])
def handle_webhook():
try:
# Use the client to validate and parse the webhook data
data = client.handle_request(request)
# If the data is valid, you can continue processing it
print(data)
# This could involve updating your database, triggering other actions, etc.
return jsonify({"message": "Webhook received and validated successfully."}), 200
except ValueError:
# If the data is not valid, respond with an error status
return jsonify({"message": "Invalid webhook data."}), 400
if __name__ == "__main__":
app.run("0.0.0.0", 80)
In the above example, replace "YOUR_API_KEY" and "YOUR_MERCHANT_ID" with your actual API key and Merchant ID.
This application listens for POST requests at the /webhook endpoint. When it receives a request, it uses the TONBApiClient.handle_request() method to validate and parse the webhook data. If the data is valid, it returns a success message; if the data is not valid, it returns an error message.
Remember to replace the IP address and port (0.0.0.0 and 80) in app.run() if needed.
Contributing
We welcome contributions to this library. If you have any suggestions or issues, feel free to open an issue or submit a pull request.
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 tonb-1.0.0.tar.gz.
File metadata
- Download URL: tonb-1.0.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06f61ba70f145ff22f3814e9fb80c9b6d5fe7f00419f2eb91cc63c5794292b35
|
|
| MD5 |
3d013da67486a501f64eacac2795c6db
|
|
| BLAKE2b-256 |
5cffd2860aa00d8274448bc8d438210dc6882f5cd84948a1711eb02d7810708a
|
File details
Details for the file tonb-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tonb-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4eb5baa4eaf36db73c07e080fb2d212b47c21c5a2239da3b2227ebc2a5bbbb7b
|
|
| MD5 |
64bff47871ef80ff2f25b69620853f5e
|
|
| BLAKE2b-256 |
dac97652d95eb8b243db4a0f81a39e4ee76523a2e24be39b200c9442ef983a54
|