Skip to main content

Chapa

Linter Version Build License

Unofficial Python SDK for Chapa API.

Introduction

This document provides a comprehensive guide to integrating and using the Chapa Payment Gateway SDK in your application. Chapa is a powerful payment gateway that supports various payment methods, facilitating seamless transactions for businesses. This SDK simplifies interaction with Chapa’s API, enabling operations such as initiating payments, verifying transactions, and managing subaccounts.

Installation

To use the Chapa SDK in your project, you need to install it using pip, as it is a dependency for making HTTP requests it will also install httpx as a dependency.

pip install chapa

Usage

To begin using the SDK, import the Chapa class from the module and instantiate it with your secret key.

Initializing the SDK

from chapa import Chapa

# Replace 'your_secret_key' with your actual Chapa secret key
chapa = Chapa('your_secret_key')

Async Support

The Chapa SDK implements async support using the AsyncChapa class. To use the async version of the SDK, import the AsyncChapa class from the module and instantiate it with your secret key.

from chapa import AsyncChapa

# Replace 'your_secret_key' with your actual Chapa secret key
chapa = AsyncChapa('your_secret')

All of the below methods are available in the async version of the SDK. So you can just use it as you would use the sync version.

response = await chapa.initialize(
    ...
)

Making Payments

To initiate a payment, use the initialize method. This method requires a set of parameters like the customer's email, amount, first name, last name, and a transaction reference.

response = chapa.initialize(
    email="customer@example.com",
    amount=1000,
    first_name="John",
    last_name="Doe",
    tx_ref="your_unique_transaction_reference",
    callback_url="https://yourcallback.url/here"
)
print(response)

Verifying Payments

After initiating a payment, you can verify the transaction status using the verify method.

transaction_id = "your_transaction_id"
verification_response = chapa.verify(transaction_id)
print(verification_response)

Creating Subaccounts

You can create subaccounts for split payments using the create_subaccount method.

subaccount_response = chapa.create_subaccount(
    business_name="My Business",
    account_name="My Business Account",
    bank_code="12345",
    account_number="0012345678",
    split_value="0.2",
    split_type="percentage"
)
print(subaccount_response)

Bank Transfers

To initiate a bank transfer, use the transfer_to_bank method.

transfer_response = chapa.transfer_to_bank(
    account_name="Recipient Name",
    account_number="0987654321",
    amount="500",
    reference="your_transfer_reference",
    bank_code="67890",
    currency="ETB"
)
print(transfer_response)

Verifying Webhook

The reason for verifying a webhook is to ensure that the request is coming from Chapa. You can verify a webhook using the verify_webhook method.

from chapa import verify_webhook

# request is just an example of a request object
# request.body is the request body
# request.headers.get("Chapa-Signature") is the Chapa-Signature header

verify_webhook(
    secret_key="your_secret_key",
    body=request.body,
    chapa_signature=request.headers.get("Chapa-Signature")
)

Getting Testing Cards and Mobile Numbers

For testing purposes, you can retrieve a set of test cards and mobile numbers.

from chapa import get_testing_cards, get_testing_mobile 

# Get a list of testing cards
test_cards = get_testing_cards()
print(test_cards)

# Get a list of testing mobile numbers
test_mobiles = get_testing_mobile()
print(test_mobiles)

Get Webhook Events

You can get webhook events details with description like below

from chapa import WEBHOOK_EVENTS, WEBHOOKS_EVENT_DESCRIPTION

# Get a list of webhook events
print(WEBHOOK_EVENTS)

# Get a list of webhook events with description
print(WEBHOOKS_EVENT_DESCRIPTION)

Conclusion

The Chapa Payment Gateway SDK is a flexible tool that allows developers to integrate various payment functionalities into their applications easily. By following the steps outlined in this documentation, you can implement features like payment initialization, transaction verification, and sub-account management. Feel free to explore the SDK further to discover all the supported features and functionalities.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. After that free to contribute to this project. Please read the CONTRIBUTING.md file for more information.

Please make sure to update tests as appropriate.

Run Locally

Clone the project

git clone https://github.com/chapimenge3/chapa.git

Go to the project directory

cd chapa

Install dependencies

pip install -r requirements.txt

License

MIT

Author

Temkin Mengistu

portfolio linkedin twitter

Metadata

Release files for chapa-py9 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chapa-py9 0.1.6
File Size Uploaded Provenance Actions
chapa_py9-0.1.6.tar.gz 11.9 kB Without provenance Details for chapa_py9-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chapa-py9 0.1.6
File Interpreter ABI Platform Actions
chapa_py9-0.1.6-py3-none-any.whl Python 3 none any Details for chapa_py9-0.1.6-py3-none-any.whl

Total release size: 22.0 kB

Release files / chapa_py9-0.1.6.tar.gz

Download URL chapa_py9-0.1.6.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e539141e41f9bdbd342a9773b3044c2e4fd84330d4a98a5e24feb7cda0fe4d1e
BLAKE2b-256 checksum
How to use checksums
31534f8927c77f3c8867c29de37e96fecc56eb45fdd9793829882ed0bbb3b57d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.18

Release files / chapa_py9-0.1.6-py3-none-any.whl

Download URL chapa_py9-0.1.6-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10395b4ab6fbeb8f5496fb38fd30bb2f34c78bd06f96b76bea8ac730040b3dab
BLAKE2b-256 checksum
How to use checksums
5e2519210b426751cadec0f52da22de497acdb52a81d02abe583a6d4d9fccac4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.18

Security for chapa-py9 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page