Python SDK for Payra payment signature generation (backend)
Project description
Payra Python SDK
Official Python SDK for integrating Payra's on-chain payment system into your backend applications.
This SDK provides:
- Secure generation of ECDSA signatures compatible with the Payra smart contract, used for order payment verification.
- Simple methods for checking the on-chain details of orders to confirm completed payments.
How It Works
The typical flow for signing and verifying a Payra transaction:
- The frontend prepares all required payment parameters:
- Network – blockchain name (e.g. Polygon, Linea)
- Token address – ERC-20 token contract address
- Order ID – unique order identifier
- Amount WEI – already converted to the smallest unit (e.g. wei, 10⁶)
- Timestamp – Unix timestamp of the order
- Payer wallet address – the wallet address from which the user will make the on-chain payment
- The frontend sends these parameters to your backend.
- The backend uses this SDK to generate a cryptographic ECDSA signature with its signature key (performed offline).
- The backend returns the generated signature to the frontend.
- The frontend calls the Payra smart contract (
payOrder) with all parameters plus the signature.
This process ensures full compatibility between your backend and Payra’s on-chain verification logic.
Features
- Generates Ethereum ECDSA signatures using the
secp256k1curve. - Fully compatible with Payra's Solidity smart contracts (
ERC-1155payment verification). - Supports
.envandconfig/payra.phpconfiguration for multiple blockchain networks. - Laravel IoC container integration (easy dependency injection)
- Verifies order payment details directly on-chain via RPC or blockchain explorer API.
- Provides secure backend integration for signing and verifying transactions.
- Includes optional utility helpers for:
- Currency conversion (via ExchangeRate API)
- USD ⇄ WEI conversion for token precision handling.
Setup
Before installing this package, make sure you have an active Payra account:
https://payra.cash/products/on-chain-payments/registration
Before installing this package, make sure you have a MerchantID
- Your Merchant ID (unique for each blockchain network)
- Your Signature Key (used to sign Payra transactions securely)
Additionally: To obtain your RPC URLs which are required for reading on-chain order statuses directly from the blockchain, you can use the public free endpoints provided with this package or create an account on one of the following services for better performance and reliability:
-
QuickNode – Extremely fast and excellent for Polygon/Mainnet. (quicknode.com)
-
Alchemy – Offers a great developer dashboard and high reliability. (alchemy.com)
-
DRPC – Decentralized RPC with a generous free tier and a strict no-log policy. (drpc.org)
-
Infura – The industry standard; very stable, especially for Ethereum. (infura.io)
Optional (recommended):
- Create a free API key at ExchangeRate API to enable automatic fiat → USD conversions using the built-in utility helpers.
Installation
From PyPI
Install the latest stable version from PyPI:
pip install payra-sdk
From Source (for development)
Clone and install locally (editable mode for development):
git clone https://github.com/payracash/payra-sdk-python.git
cd payra-sdk-python
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -e .
Dependencies
This SDK requires:
- Python 3.8+
- web3.py
- ecdsa
- python-dotenv
- (optional)
requestsandexchange-rate-apifor fiat conversion utilities.
Environment Configuration
Create a .env file in your project root (you can copy from example):
cp .env.example .env
This file stores your private configuration and connection settings for all supported networks. Never commit .env to version control.
Required Variables
Exchange Rate (optional)
Used for automatic fiat → USD conversions via the built-in Payra utilities.
# Optional only needed if you want to use the built-in currency conversion helper
PAYRA_EXCHANGE_RATE_API_KEY= # Your ExchangeRate API key (from exchangerate-api.com)
PAYRA_EXCHANGE_RATE_CACHE_TIME=720 # Cache duration in minutes (default: 720 = 12h)
PAYRA_POLYGON_OCP_GATEWAY_CONTRACT_ADDRESS=0xc56c55D9cF0FF05c85A2DF5BFB9a65b34804063b
PAYRA_POLYGON_SIGNATURE_KEY=
PAYRA_POLYGON_MERCHANT_ID=
PAYRA_POLYGON_RPC_URL_1=https://polygon-rpc.com
PAYRA_POLYGON_RPC_URL_2=
PAYRA_ETHEREUM_OCP_GATEWAY_CONTRACT_ADDRESS=
PAYRA_ETHEREUM_SIGNATURE_KEY=
PAYRA_ETHEREUM_MERCHANT_ID=
PAYRA_ETHEREUM_RPC_URL_1=
PAYRA_ETHEREUM_RPC_URL_2=
PAYRA_LINEA_OCP_GATEWAY_CONTRACT_ADDRESS=
PAYRA_LINEA_SIGNATURE_KEY=
PAYRA_LINEA_MERCHANT_ID=
PAYRA_LINEA_RPC_URL_1=
PAYRA_LINEA_RPC_URL_2=
Important Notes
- The cache automatically refreshes when it expires.
- You can adjust the cache duration by setting
PAYRA_EXCHANGE_RATE_CACHE_TIME: 5→ cache for 5 minutes60→ cache for 1 hour720→ cache for 12 hours (default)- Each network (Polygon, Ethereum, Linea) has its own merchant ID, signature key, and RPC URLs.
- The SDK automatically detects which chain configuration to use based on the selected network.
- You can use multiple RPC URLs for redundancy (the SDK will automatically fall back if one fails).
- Contract addresses correspond to the deployed Payra Core Forward contracts per network.
Usage Example
Generating signature
from payra_sdk import PayraUtils, PayraSignature, PayraSDKException
try:
# Convert amount to smallest unit (wei or token decimals
amount_wei = PayraUtils.to_wei(3.34, 'polygon', 'usdt')
PAYMENT_DATA = {
"network": "polygon",
"token_address": "0xc2132D05D31c914a87C6611C10748AEb04B58e8F", # USDT on Polygon
"order_id": "ord-258",
"amount_wei": amount_wei, # e.g. 3.34 USDT in smallest unit
"timestamp": 1753826059, # current Unix timestamp
"payer_address": "0xe6c961D6ad9a27Ea8e5d99e40abaC365DE9Cc162"
}
# Initialize signer
payra_signature = PayraSignature()
# Generate cryptographic signature
signature = payra_signature.generate(
network=PAYMENT_DATA["network"],
token_address=PAYMENT_DATA["token_address"],
order_id=PAYMENT_DATA["order_id"],
amount_wei=PAYMENT_DATA["amount_wei"],
timestamp=PAYMENT_DATA["timestamp"],
payer_address=PAYMENT_DATA["payer_address"]
)
print(f"Generated signature: {signature}")
except PayraSDKException as e:
print(f"Payra SDK error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Input Parameters
| Field | Type | Description |
|---|---|---|
network |
string |
Selected network name |
token_address |
string |
ERC20 token contract address |
order_id |
string |
Unique order reference (e.g. ORDER-123) |
amount_wei |
string or integer |
Token amount in smallest unit (e.g. wei) |
timestamp |
number |
Unix timestamp of signature creation |
payer_address |
string |
Payer Wallet Address |
Behind the Scenes
- The backend converts the amount to the smallest blockchain unit (e.g. wei).
- A
PayraSignatureGeneratorinstance is created using your signature key from.env - It generates an ECDSA signature that is fully verifiable on-chain by the Payra smart contract.
- The resulting signature should be sent to the frontend, which must call
payOrder(...)using the same parameters (timestamp,orderId,amount,tokenAddress, etc.) that were used to generate the signature.
Get Order Details
Retrieve full payment details for a specific order from the Payra smart contract. This method returns the complete on-chain payment data associated with the order, including:
- whether the order has been paid,
- the payment token address,
- the paid amount,
- the fee amount,
- and the payment timestamp.
Use this method when you need detailed information about the payment or want to display full transaction data.
from payra_sdk import PayraOrderService, PayraSDKException
try:
ORDER_ID = "ord-258"
# Initialize verifier for a specific network
order_service = PayraOrderService("polygon")
print("\nGet order details...")
details = order_service.get_details(ORDER_ID)
print("Order ID:", ORDER_ID)
print("Details:", details)
except PayraSDKException as e:
print(f"Payra SDK error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Behind the Scenes
- The backend initializes a
PayraOrderServiceobject for the desired blockchain network. - It calls
get_details(order_id)to check if the order transaction exists and is confirmed on-chain. - The function returns a dictionary with:
{
"success": True,
"paid": True,
"error": None.
"toke": '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
"amount": 400000,
"fee": 3600,
"timestamp": 1765138941
}
Check Order Paid Status
Perform a simple payment check for a specific order. This method only verifies whether the order has been paid (true or false) and does not return any additional payment details.
Use this method when you only need a quick boolean confirmation of the payment status.
from payra_sdk import PayraOrderService, PayraSDKException
try:
ORDER_ID = "ord-258"
# Initialize verifier for a specific network
order_service = PayraOrderService("polygon")
print("\nChecking order status...")
result = order_service.is_paid(ORDER_ID)
print("Order ID:", ORDER_ID)
print("Result:", result)
if result["success"] and result["paid"]:
print("Order is PAID on-chain")
elif result["success"]:
print("Order is NOT paid yet")
else:
print("Error:", result["error"])
except PayraSDKException as e:
print(f"Payra SDK error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Behind the Scenes
- The backend initializes a
PayraOrderServiceobject for the desired blockchain network. - It calls
is_order_paid(order_id)to check if the order transaction exists and is confirmed on-chain. - The function returns a dictionary with:
{
"success": True,
"paid": True,
"error": None
}
- If
paidisTrue, the order has been successfully processed and confirmed by the Payra smart contract.
Using Utility Functions
The PayraUtils module provides convenient helpers for token conversion, precision handling, and fiat currency operations.
from payra_sdk import PayraUtils
# Convert USD/token amount to smallest unit (Wei or token decimals)
amount_wei = PayraUtils.to_wei(3.34, 'polygon', 'usdt')
print("Amount in Wei:", amount_wei) # 3340000
# Convert from Wei back to readable token amount
amount = PayraUtils.from_wei(3340000, 'polygon', 'usdt', precision=2)
print("Readable amount:", amount) # "3.34"
# Get token decimals for any supported network
print("USDT decimals on Polygon:", PayraUtils.get_decimals("polygon", "usdt"))
print("POL decimals on Polygon:", PayraUtils.get_decimals("polygon", "pol"))
# Convert fiat currency to USD using the built-in ExchangeRate API
usd_value = PayraUtils.convert_to_usd(100, "EUR")
print(f"100 EUR = {usd_value} USD")
Behind the Scenes
to_wei(amount, network, token)– Converts a human-readable token amount into the smallest unit (used on-chain).from_wei(amount, network, token, precision)– Converts back from smallest unit to a formatted amount.get_decimals(network, token)– Returns the number of decimals for the given token on that network.convert_to_usd(amount, currency)– Converts fiat amounts (e.g. EUR, GBP) to USD using your ExchangeRate API key.
Testing
You can run the included examples to test signing and verification:
python3 example_signature.py
python3 example_order_get_details.py
python3 example_order_is_paid
python3 example_utils.py
Make sure your .env file contains correct values for the network being used.
Tips
- Always verify your
.envconfiguration before running any signing or on-chain verification examples. - The SDK examples are safe to run, they use read-only RPC calls (no real transactions are broadcast).
- You can modify
example_signature.pyto test custom token addresses or order parameters.
Projects
Project
- https://payra.cash
- https://payra.tech
- https://payra.xyz
- https://payra.eth - suporrted by Brave and Opera Browser or .limo
Social Media
License
MIT © Payra
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
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 payra_sdk-1.2.7.tar.gz.
File metadata
- Download URL: payra_sdk-1.2.7.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f1ca3307e340b49c1dd844252d9ab57113c36ccabea1f668d8a3a7bb2a0a1da
|
|
| MD5 |
eb8b45ef80bb2da3b33dd4c03814855f
|
|
| BLAKE2b-256 |
1908ea1434813e8a8d7b2ba922cb507845c5aeeedf6210a637251e5909fa8e59
|
File details
Details for the file payra_sdk-1.2.7-py3-none-any.whl.
File metadata
- Download URL: payra_sdk-1.2.7-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2526900469167f55ab2d230b5ae7faf0af57eaeac036f2c68170aa9189156f5a
|
|
| MD5 |
074adbc51e0da02b6486f8f62c24d7dc
|
|
| BLAKE2b-256 |
9d64b2f1737d9a105df0aca94d1bb48e5a2d6cfe81fc5e9c7a25110fdeb5bc56
|