Package: tonb
TONB Merchant Backend API Python Client LibraryIntroductionThis 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 dataclassesInvoiceandWebhook, and a main client classTONBApiClient.InstallationpipinstalltonbUsageFirst, import the API client class from the library:fromtonbimportTONBApiClientCreate 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 merchantNow, you can use this instance to interact with the API.ClassesInvoiceThis data class defines the invoice object. It represents an invoice in the system. It can be used to retrieve or manipulate invoice data.Attributesid: 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 Methodsfrom_dict(source: dict): Creates an Invoice instance from a dictionary.WebhookThis data class defines the webhook object. It represents a webhook data received from the TONB API.Attributesorder_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 Methodsfrom_dict(source: dict): Creates a Webhook instance from a dictionary.TONBApiClientThis class is the main client class that provides methods to interact with the TONB Merchant backend API.Attributesbase_url: Base URL for the API (str).timeout: Timeout for the request (int, default is 10).Methodscreate_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.ExamplesBelow are a few examples demonstrating the usage of the TONB Merchant backend API Python client library.Basic OperationsThis example shows how to create and cancel an invoice and get statistics about invoices.fromtonbimportTONBApiClient# Create an instance of the API clientclient=TONBApiClient("https://merchant-app-api2.tonb.io","YOUR_API_KEY","YOUR_MERCHANT_ID")# Create an invoice with amount 1000000000 and order_id 12345invoice=client.create_invoice(1000000000,12345)# Print the invoice detailsprint(invoice)# Cancel the created invoiceclient.cancel_invoice(invoice)# Get and print the overall statistics about invoicesprint(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 WebhooksThis example shows how to set up a Flask application to receive and validate webhooks using thehandle_request()method.fromflaskimportFlask,request,jsonifyfromtonbimportTONBApiClient# Create a Flask appapp=Flask(__name__)# Create an instance of the API clientclient=TONBApiClient("https://merchant-app-api2.tonb.io","YOUR_API_KEY","YOUR_MERCHANT_ID")@app.route("/webhook",methods=["POST"])defhandle_webhook():try:# Use the client to validate and parse the webhook datadata=client.handle_request(request)# If the data is valid, you can continue processing itprint(data)# This could involve updating your database, triggering other actions, etc.returnjsonify({"message":"Webhook received and validated successfully."}),200exceptValueError:# If the data is not valid, respond with an error statusreturnjsonify({"message":"Invalid webhook data."}),400if__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/webhookendpoint. When it receives a request, it uses theTONBApiClient.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.0and80) inapp.run()if needed.ContributingWe welcome contributions to this library. If you have any suggestions or issues, feel free to open an issue or submit a pull request.