Python SDK for CinetPay Payment API.
Project description
New CinetPay Python Library 😎
Python SDK for CinetPay Payment API.
Currently Supported Services
- Payment API
Next
We will add progressively support for following CinetPay Services:
- Transfert API
- SMS API
- Airtime API
Prerequisites
You need to have at least 3.9 version of python to be able to continue. To do this:
Next you can go ahead with CinetPay.
Install
pip install CinetPay
API
This Python library's API is designed around the CinetPay REST API
Settings
There are two ways to setup CinetPay Client:
- Using a Config object
from cinetpay import (
Client, Config, Credential
Channels, Currencies, Languages,
)
# Setting up a client configuration
config = Config(
currencies = Currencies.XOF,
languages = Languages.FR,
channels = Channels.ALL,
lock_phone_number = True,
raise_on_error = True,
credentials = Credential(
apikey = 'XXXXXXXXXXXXXXXXXX',
site_id = 'XXXXXX'
)
)
# Initializing the client
client = Client(
configs = config
)
- Using a dictionary object
from cinetpay import (
Client, Config,
Channels, Currencies, Languages,
)
# Setting up a client configuration
config = {
'currencies': Currencies.XOF,
'languages': Languages.FR,
'channels': Channels.ALL,
'lock_phone_number': True,
'raise_on_error': False, # If True, client will raise on request errors
'credentials':{
'apikey':'XXXXXXXXXXXXXXXXXX',
'site_id':'XXXXXX',
}
}
# Initializing the client
client = Client(
configs = config
)
Transactions
Create Order
from cinetpay import (
Order, Customer, Currencies,
)
# CREATING A TRANSACTION
order = Order(
id = 'ORDER1234',
amount = 1000,
currency = Currencies.XOF,
description = 'Description of the transaction',
notify_url = 'https://www.exemple.com/notify',
return_url = 'https://www.exemple.com/return',
customer = Customer(
customer_id = 'mycustomer123',
customer_email = 'mycustomer@mydomain.com',
customer_phone_number = '+22890000000',
customer_name = 'John',
customer_surname = 'Doe',
)
)
# GET ORDER JSON REPRESENTATION
print(order.to_representation())
{
"transaction_id":"ORDER1234",
"amount":1000,
"notify_url":"https://www.exemple.com/notify",
"return_url":"https://www.exemple.com/return",
"description":"Description of the transaction",
"customer_id":"mycustomer123",
"customer_name":"John",
"customer_surname":"Doe",
"customer_phone_number":"+22890000000",
"apikey":"XXXXXXXXXXXXXXXXXXXXX",
"site_id":"XXXXXX",
"channels":"ALL",
"lang":"fr",
"currency":"XOF",
"lock_phone_number":false
}
Create Transaction
from cinetpay import (
Client, Config, Order, Customer,
Channels, Currencies, Languages,
Credential
)
# Setting up a client configuration
config = Config(
currencies = Currencies.XOF,
languages = Languages.FR,
channels = Channels.ALL,
lock_phone_number = True,
raise_on_error = True,
credentials = Credential(
apikey = 'XXXXXXXXXXXXXXXXXX',
site_id = 'XXXXXX'
)
)
# Initializing the client
client = Client(
configs = config
)
# CREATING A TRANSACTION
order = Order(
id = 'ORDER1234',
amount = 1000,
currency = Currencies.XOF,
description = 'Description of the transaction',
notify_url = 'https://www.exemple.com/notify',
return_url = 'https://www.exemple.com/return',
customer = Customer(
customer_id = 'mycustomer123',
customer_email = 'mycustomer@mydomain.com',
customer_phone_number = '+22890000000',
customer_name = 'John',
customer_surname = 'Doe',
)
)
# INITIALIZE PAYMENT
res = client.initialize_transaction(order)
print(res)
Retrieve Transaction
To get a specific transaction you can use the get_transaction function with the transaction_id or token.
response = client.get_transaction(transaction_id)
Get a Specific Transaction payment url
You can also get a payment URL for a specific Transaction using get_bill_url function.
response = client.get_bill_url(transaction_id)
Responses
-
Response class
The default Request response class, is returned when detail is set to True in the request.
response = client.create_transaction(my_transaction)
print(response) # <Class Response status: 201, error: False>
# Use json attribute to access response data.
print(response.json) # {"id":transaction_id,...}
-
PagginatedResponse Class
PagginatedResponse is used to manage Pagginated response data. It provides properties such as :
has_next: bool ,Trueif the PagginatedResponse has a next page elseFalse.has_prev: bool,Trueif the PagginatedResponse has a next page elseFalsenext_page: PagginatedResponse, the next page ifhas_nextisTrueprevious_page: PagginatedResponse, the next page ifhas_previsTrueresult: sequence[ Mapping[ str,any ] ], the page data.
Errors
Errors are raised if requests return an error status.
-
ErrorResponse class
This Error is returned if raise_on_error is set to True in Client configs.
from cinetpay import (
Client, Config, Credential,
Channels, Currencies, Languages,
)
# Setting up a client configuration
config = Config(
currencies = Currencies.XOF,
languages = Languages.FR,
channels = Channels.ALL,
lock_phone_number = True,
raise_on_error = True,
credentials = Credential(
apikey = 'WRONG-API-KEY',
site_id = 'SIDE-ID'
)
)
# Initializing the client
client = Client(
configs = config
)
response = client.get_transaction('TRANSACTION-ID')
# With raise_on_error set to False
print(type(response)) # <class 'cinetpay._types.ErrorResponse'>
print(response.get_message()) # Client - Client error '400 Bad Request' for url 'https://api-checkout.cinetpay.com/v2/payment'.
# Access ErorResponse infos
print(response.json)
# {'code': '624', 'message': 'UNKNOWN_ERROR', 'description': 'An error occurred while processing the request', 'api_response_id': '1716800180.4474'}
print(response.description)
# An error occurred while processing the request.
print(response.cause)
# 1- l'apikey saisi est incorrect
# 2- La valeur de lock_phone_number est à true mais la valeur du customer_phone_number est incorrect.
print(response.hint)
# 1- Récupérer l'apikey correcte dans votre back-office(menu integration)
# 2- La variable doit être false.
# For more information check: https://docs.cinetpay.com/api/1.0-fr/checkout/initialisation#problemes-frequents
-
ResponseError class
This Error is raised if raise_on_error is set to True in Client configs.
from cinetpay import (
Client, Config, Credential,
Channels, Currencies, Languages,
)
# Setting up a client configuration
config = Config(
currencies = Currencies.XOF,
languages = Languages.FR,
channels = Channels.ALL,
lock_phone_number = True,
raise_on_error = True,
credentials = Credential(
apikey = 'WRONG-API-KEY',
site_id = 'SIDE-ID'
)
)
# Initializing the client
client = Client(
configs = config
)
response = client.get_transaction('TRANSACTION-ID')
# Will raise a ResponseError with response message.
Made with ❤️ By #Einswilli
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
File details
Details for the file cinetpay-0.0.1.tar.gz.
File metadata
- Download URL: cinetpay-0.0.1.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
093db0c77e5ece01f22fc88a24593a5174e112d535044fe2ed7d9b7304539c8e
|
|
| MD5 |
6b4e8d55fea716db46fb455718140465
|
|
| BLAKE2b-256 |
45651df37cc73f37746672129f28edb2e4742ab6bce6a365cc2e285e55b5f6eb
|