Skip to main content

A Python SDK for interacting with the Ercaspay payment gateway.

Project description

# Ercaspay SDK Documentation

Ercaspay SDK provides an easy and efficient way to interact with the Ercaspay payment gateway. This SDK simplifies the process of making payments, verifying transactions, and handling various payment methods.

## Installation

To install the SDK, run:

```bash
pip install ercaspysdk

Checkout Payment

Checkout payments allow your customers to make payments using the standard Ercaspay checkout page.

Usage

from ercaspy.client import Ercaspy
from ercaspy.exceptions import APIError

client = Ercaspy(api_key="Your_Api_Key")

payment_data = {
   "amount": "1500",
   "paymentReference": "unique_transaction_code",
   "customerName": "Sheden",
   "customerEmail": "shedenbright@gmail.com",
   "currency": "NGN",
   "redirectUrl": "https://frontendurl.com",
   "paymentMethods": "card, ussd, bank-transfer"
}

def initiate_payment():
    try:
        response = client.initiate_payment(payment_data=payment_data)
        print(response)
    except APIError as e:
        print(f"An error occurred while initiating payment: {e}")

Sample Response:

{
    "requestSuccessful": True,
    "responseCode": "success",
    "responseMessage": "success",
    "responseBody": {
        "paymentReference": "olamide123456",
        "transactionReference": "ERCS|20241216193301|1734373981563",
        "checkoutUrl": "https://sandbox-checkout.ercaspay.com/ERCS|20241216193301|1734373981563"
    }
}

Redirect the user to the checkoutUrl. After a successful payment, the user will be redirected back to the specified redirectUrl.


Verify Checkout Transaction

Call the verify_transaction method to verify a transaction.

Usage

transaction_ref = "transaction_reference_from_checkout"

def verify_transaction(transaction_ref):
    try:
        response = client.verify_transaction(transaction_ref)
        print(response)
    except APIError as e:
        print(f"An error occurred while verifying the transaction: {e}")

Direct Integration

Bank Transfer

The bank transfer method generates dynamic bank details for your customers to make payments. The account number can only be used once by the customer.

Usage

payment_data = {
   "amount": "1500",
   "paymentReference": "unique_transaction_ref",
   "customerName": "Sheden",
   "customerEmail": "shedenbright@gmail.com",
   "currency": "NGN",
   "redirectUrl": "https://frontendurl.com",
   "paymentMethods": "bank-transfer"
}

def bank_transfer(payment_data):
    try:
        response = client.initiate_bank_transfer(data=payment_data)
        print(response)
    except APIError as e:
        print(f"An error occurred while initiating bank transfer: {e}")

Verify Direct Integration Payment

Call the check_direct_payment_status method to verify the payment status.

Usage

transaction_ref = "unique_transaction_ref"
payment_method = "bank-transfer"

def verify_direct_payment(transaction_ref, payment_method):
    try:
        response = client.check_direct_payment_status(transaction_ref, payment_method)
        print(response)
    except APIError as e:
        print(f"An error occurred while verifying payment status: {e}")

USSD Payment

USSD payment allows customers to pay using their phone without internet access. The endpoint generates a USSD code for the payment.

Usage

payment_data = {
   "amount": "1500",
   "paymentReference": "unique_transaction_ref",
   "customerName": "Sheden",
   "customerEmail": "shedenbright@gmail.com",
   "currency": "NGN",
   "paymentMethods": "ussd"
}

def ussd_transaction(payment_data):
    try:
        response = client.initiate_ussd_transaction(data=payment_data, bank_name="fcmb")
        print(response)
    except APIError as e:
        print(f"An error occurred while initiating USSD transaction: {e}")

Verify the payment using check_direct_payment_status as shown in the Verify Direct Integration Payment section.


Card Payment

Card payment allows customers to pay with their debit or credit card.

Encrypt Card Details

Use the following function to encrypt card details before initiating the transaction:

def encrypt_card(card_number, cvv, pin, expiry_date):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_v1_5
    import base64
    import json

    # Load public key
    with open('key/rsa_public_key.pub', 'rb') as key_file:
        public_key_data = key_file.read()
    public_key = RSA.import_key(public_key_data)

    # Card details
    card_params = {
        'cvv': cvv,
        'pin': pin,
        'expiryDate': expiry_date,
        'pan': card_number
    }

    # Encrypt details
    cipher = PKCS1_v1_5.new(public_key)
    encrypted = cipher.encrypt(json.dumps(card_params).encode('utf-8'))
    return base64.b64encode(encrypted).decode('utf-8')

Initiate Card Transaction

data = {
   "card_number": "5123450000000008",
   "cvv": "100",
   "pin": "1234",
   "expiry_date": "0139",
   "transaction_reference": "ERCS|20241216191025|1734372625377"
}

encrypted_payload = encrypt_card(
    card_number=data["card_number"],
    cvv=data["cvv"],
    pin=data["pin"],
    expiry_date=data["expiry_date"]
)

def initiate_card_transaction(encrypted_payload, transaction_ref):
    try:
        response = client.initiate_card_transaction(payload=encrypted_payload, transaction_ref=transaction_ref)
        print(response)
    except APIError as e:
        print(f"An error occurred while initiating card transaction: {e}")

OTP Management

Submit OTP

def submit_otp(otp, transaction_ref, gateway_ref):
    try:
        response = client.submit_otp(otp, transaction_ref, gateway_ref)
        print(response)
    except APIError as e:
        print(f"An error occurred while submitting OTP: {e}")

Resend OTP

def resend_otp(transaction_ref, gateway_ref):
    try:
        response = client.resend_otp(transaction_ref, gateway_ref)
        print(response)
    except APIError as e:
        print(f"An error occurred while resending OTP: {e}")

Utilities

Get Supported Banks

Fetch the list of supported banks:

banks = client.get_bank_list()
print(banks)

Cancel Transaction

To cancel a transaction that has not been completed:

client.cancel_transaction(transaction_ref="transaction_reference")

Developed by: Shedenbright
Contact: shedenbright@gmail.com
Resume: sheden-resume.netlify.app

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

ercaspysdk-0.2.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ercaspysdk-0.2.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file ercaspysdk-0.2.1.tar.gz.

File metadata

  • Download URL: ercaspysdk-0.2.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for ercaspysdk-0.2.1.tar.gz
Algorithm Hash digest
SHA256 92e99ab4ee33cf3cb4b8695907878bc72d74a8b6a11db49817433606823981c4
MD5 a375a914ffc1913ed8967d4c072b13d4
BLAKE2b-256 cd44b59a796e1b7bb97fd2eed603485567ce135daa16670ad9f28796da352769

See more details on using hashes here.

File details

Details for the file ercaspysdk-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: ercaspysdk-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for ercaspysdk-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39d2cd62629f4ddf294d483ca83736b2ffc9576aff78576313a5433424371039
MD5 1ef9fcc66f097a19ff781fe5571e3206
BLAKE2b-256 fef61497aa65fd1774d2f76b01a579f481c4ec7353a6ee4e632dd34a2b0c33ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page