Python Client
Code example for python
import hashlib
import hmac
import json
import time
import requests
class PaymentEngineClient:
def __init__(self, api_key: str, secret_key: str, base_url: str = 'https://api.2settle.io'):
self.api_key = api_key
self.secret_key = secret_key
self.base_url = base_url
def _sign(self, method: str, path: str, body: dict = None) -> tuple:
timestamp = str(int(time.time() * 1000))
body_str = json.dumps(body or {}, separators=(',', ':'))
body_hash = hashlib.sha256(body_str.encode()).hexdigest()
payload = f"{timestamp}|{method}|{path}|{body_hash}"
hmac_key = hashlib.sha256(self.secret_key.encode()).hexdigest()
signature = hmac.new(hmac_key.encode(), payload.encode(), hashlib.sha256).hexdigest()
return timestamp, signature, body_str
def _request(self, method: str, path: str, body: dict = None):
timestamp, signature, body_str = self._sign(method, path, body)
response = requests.request(
method,
f"{self.base_url}{path}",
headers={
'Content-Type': 'application/json',
'X-API-Key': self.api_key,
'X-Timestamp': timestamp,
'X-Signature': signature,
},
data=body_str if method != 'GET' else None,
)
return response.json()
def create_payment(self, payment_type: str, fiat_amount: float, fiat_currency: str,
crypto: str = None, network: str = None, charge_from: str = None,
payer: dict = None, receiver: dict = None, **kwargs):
body = {
'type': payment_type,
'fiatAmount': fiat_amount,
'fiatCurrency': fiat_currency,
}
if crypto: body['crypto'] = crypto
if network: body['network'] = network
if charge_from: body['chargeFrom'] = charge_from
if payer: body['payer'] = payer
if receiver: body['receiver'] = receiver
body.update(kwargs)
return self._request('POST', '/v1/payments', body)
def get_payment(self, reference: str):
return self._request('GET', f'/v1/payments/{reference}', {})
def claim_gift(self, reference: str, receiver: dict):
return self._request('POST', f'/v1/payments/gifts/{reference}/claim/confirm', {'receiver': receiver})
def fulfill_request(self, reference: str, payer: dict, crypto: str, network: str):
return self._request('POST', f'/v1/payments/requests/{reference}/fulfill', {
'payer': payer,
'crypto': crypto,
'network': network,
})
# Usage
client = PaymentEngineClient('pk_xxx', 'sk_xxx')
# Create a transfer
result = client.create_payment(
payment_type='transfer',
fiat_amount=10000,
fiat_currency='NGN',
crypto='USDT',
network='trc20',
charge_from='fiat',
payer={'chatId': '7389201648'},
receiver={
'bankCode': '090405',
'accountNumber': '8012345678',
}
)
print(f"Deposit {result['payment']['cryptoAmount']} USDT to:")
print(result['payment']['depositAddress'])