A Python SDK for the Lygos API.
Project description
Lygos Python SDK
A Python SDK for interacting with the Lygos API.
Installation
You can install the SDK using pip (once it's published):
pip install lygos-sdk
For now, you can install it directly from the source code:
pip install .
Or, for development, in editable mode:
pip install -e .
Usage
First, you need to initialize the LygosClient with your API key.
from lygos_sdk.client import LygosClient
from lygos_sdk.exceptions import LygosAPIError, LygosNetworkError, LygosNotFoundError
# It's recommended to load the API key from environment variables
# or a secure configuration management system.
api_key = "your_lygos_api_key"
client = LygosClient(api_key=api_key)
Create a Payment Link
You can create a payment link by calling the create_payment_link method.
try:
payment_link = client.create_payment_link(
amount=2500, # Amount in the smallest currency unit
shop_name="My Awesome Shop",
message="Payment for Order #XYZ-123",
order_id="unique_order_id_xyz_123",
success_url="https://example.com/payment/success",
failure_url="https://example.com/payment/failure"
)
print(f"Payment link successfully created: {payment_link}")
# Redirect your user to this link to complete the payment
except LygosAPIError as e:
print(f"An API error occurred: {e} (Status: {e.status_code})")
except LygosNetworkError as e:
print(f"A network error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Verifying Payments (Recommended)
After the user is redirected back to your application from the Lygos payment page, you must verify the status of the transaction to confirm if the payment was successful. A user landing on your success_url is not a guarantee of a successful payment.
The validate_payment method simplifies this process. It checks the status and raises an exception if the payment was not successful, making your success handler clean and secure.
# In your success handler (e.g., a Flask route)
from lygos_sdk.exceptions import LygosPaymentValidationError, LygosNotFoundError
order_id = "unique_order_id_xyz_123" # The order_id from your success_url
try:
payment_details = client.validate_payment(order_id=order_id)
print(f"Payment for order {payment_details['order_id']} was successful!")
# Safely activate the user's subscription, ship the product, etc.
except LygosPaymentValidationError as e:
# This exception is raised if the status is 'pending', 'failed', etc.
print(f"Payment not complete: {e}")
# Advise the user that the payment is not yet confirmed.
# You can inspect e.response_body for more details.
except LygosNotFoundError:
print("The requested order was not found. Invalid order_id.")
# Handle invalid order
except LygosAPIError as e:
print(f"An API error occurred: {e}")
# Handle other generic API errors
Retrieving Transaction Status
If you need to check the status of a transaction without raising an exception for non-paid statuses, you can use the get_payin_status method.
try:
status = client.get_payin_status(order_id="unique_order_id_xyz_123")
print(f"Transaction status: {status}")
except LygosNotFoundError:
print("The requested order was not found.")
except LygosAPIError as e:
print(f"An API error occurred: {e} (Status: {e.status_code})")
except LygosNetworkError as e:
print(f"A network error occurred: {e}")
Error Handling
The SDK uses custom exceptions to make error handling more specific and intuitive. All exceptions inherit from LygosError.
LygosAPIError: The base exception for errors returned by the Lygos API.LygosAuthenticationError: Raised for authentication issues (e.g., an invalid API key).LygosInvalidRequestError: Raised for invalid request parameters (e.g., a missing field).LygosNotFoundError: Raised when a requested resource (like an order) is not found.LygosServerError: Raised for errors on the Lygos server side.LygosNetworkError: Raised for network-related issues, such as a connection timeout.
Development
To contribute to this project, please follow these steps.
Running Tests
First, install the development dependencies:
# Install pytest and other testing requirements
pip install pytest requests
# Install the SDK in editable mode
pip install -e .
Then, run the test suite using pytest from the root directory:
pytest
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 lygos_sdk-0.1.0.tar.gz.
File metadata
- Download URL: lygos_sdk-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f156cdca3418e9d127db52b43091ebf0d339263ad7cd67667f8298b304da1c46
|
|
| MD5 |
65f6182063b03a4e50aad83dc698743c
|
|
| BLAKE2b-256 |
f6faf8bd666ae238703f3f3e731bc8d7a60c8c764c46f9ec8b94a27043f69e1a
|
File details
Details for the file lygos_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lygos_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2aa99145fae084058e2fd0e86d3d0d48f00918bf3f59361371271222ddd5f5e8
|
|
| MD5 |
2c2e6d2b86fa23119c2e3b7faf517dab
|
|
| BLAKE2b-256 |
4ced87b4524f0eab550384ea9d26af02e3b46356dc9b3ecc9f489e88fe41afee
|