Skip to main content

Unofficial Django SDK for Credo payment integration

Project description

Django Credo SDK

PyPI version Python Versions Django Versions

Unofficial Django SDK for integrating Credo Payment Gateway into your Django applications. This SDK provides a clean, type-safe interface for accepting payments, verifying transactions, and handling webhooks.

✨ Features

  • 🚀 Easy Integration - Drop-in Django app with REST API views
  • 🔒 Type Safety - Fully typed request/response models
  • 💳 Payment Routing - Support for multi-destination payments (split payments)
  • 🔔 Unified Webhooks - Single endpoint for both customer redirects and server notifications
  • 🎨 Model Mixins - Add payment functionality directly to your Django models
  • 🧪 Debug Interface - Built-in test page for development
  • 🌍 Production Ready - Sandbox and production environment support

📦 Installation

pip install django-credo-sdk

🚀 Quick Start

1. Add to Django Settings

# settings.py
INSTALLED_APPS = [
    ...
    'credo_pay',
]

# Credo Configuration
CREDO_PUBLIC_KEY = 'your_public_key'
CREDO_SECRET_KEY = 'your_secret_key'
CREDO_TERMINAL_ID = 'your_terminal_id'
CREDO_ENVIRONMENT = 'sandbox'  # or 'production'
CREDO_DEFAULT_CALLBACK_URL = 'https://yoursite.com/api/credo/webhook/'

2. Initialize Payment

from credo_pay import CredoClient

client = CredoClient()

response = client.initialize_payment(
    amount=15000,  # Amount in Naira (₦150.00)
    email="customer@example.com",
    callback_url="https://yoursite.com/api/credo/webhook/",
    first_name="John",
    last_name="Doe"
)

# Redirect customer to payment page
payment_url = response.authorization_url

3. Verify Payment

# After payment callback
verification = client.verify_payment(reference='transaction_reference')

if verification.is_successful:
    print(f"Payment successful: ₦{verification.trans_amount}")
elif verification.is_failed:
    print(f"Payment failed: {verification.status_description}")

4. Handle Webhooks

Create a custom webhook view to handle payment notifications:

# views.py
from credo_pay import CredoWebhookView
from django.shortcuts import redirect

class MyWebhookView(CredoWebhookView):
    """Custom webhook handler for your application."""
    
    def handle_callback(self, callback_data, verify_response):
        """Handle customer redirect (GET request)"""
        if verify_response and verify_response.is_successful:
            # Update your order/transaction
            Order.objects.filter(
                reference=callback_data.reference
            ).update(status='paid')
            return redirect('/payment/success/')
        return redirect('/payment/failed/')
    
    def handle_webhook(self, event):
        """Handle server notification (POST request)"""
        if event.is_successful:
            # Process successful payment
            Order.objects.filter(
                reference=event.business_ref
            ).update(status='paid')
# urls.py
from django.urls import path
from .views import MyWebhookView

urlpatterns = [
    path('api/credo/webhook/', MyWebhookView.as_view(), name='credo-webhook'),
]

📚 Advanced Usage

Payment Routing (Split Payments)

Route payments to different bank accounts using service codes:

response = client.initialize_routed_payment(
    amount=15000,
    email="customer@example.com",
    service_code="YOUR_SERVICE_CODE",
    bank_account="0114877128",
    callback_url="https://yoursite.com/webhook/"
)

Model Mixins

Add payment functionality to your models:

from django.db import models
from credo_pay.mixins import CredoPaymentMixin

class Order(CredoPaymentMixin, models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    
    def get_payment_amount(self):
        return int(self.total_amount)
    
    def get_payment_email(self):
        return self.user.email

# Initialize payment for an order
order = Order.objects.get(id=1)
payment_response = order.initialize_credo_payment()
redirect_url = payment_response.authorization_url

Register Webhook Handlers

Add custom logic for specific webhook events:

def on_payment_success(event):
    print(f"Payment received: {event.trans_ref}")
    # Send confirmation email, trigger fulfillment, etc.

client.register_webhook_handler(
    "transaction.successful",
    on_payment_success
)

🔧 Configuration Options

Setting Description Default
CREDO_PUBLIC_KEY Your Credo public key Required
CREDO_SECRET_KEY Your Credo secret key Required
CREDO_TERMINAL_ID Your terminal ID Required
CREDO_ENVIRONMENT sandbox or production sandbox
CREDO_DEFAULT_CALLBACK_URL Default webhook URL None
CREDO_WEBHOOK_SECRET_TOKEN Webhook signature verification token None
CREDO_BUSINESS_CODE Your business code None

🧪 Testing

The SDK includes a debug interface for testing:

# Run development server
python manage.py runserver

# Visit debug interface
http://localhost:8000/api/credo/debug/

📖 Documentation

For complete documentation, visit the GitHub repository.

Transaction Status Codes

Code Status Description
0 Successful Transaction completed
3 Failed Transaction failed
5 Settled Transaction settled
7 Declined Declined by fraud check
9 Abandoned Customer abandoned
13 Attempted Payment attempted
14 Initialised Payment page loaded
15 Initialising Payment request sent

🤝 Support

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

⚠️ Disclaimer

This is an unofficial SDK and is not affiliated with or endorsed by Credo. Use at your own discretion.


Made with ❤️ for the Django community

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

django_credo_sdk-0.1.1.tar.gz (40.6 kB view details)

Uploaded Source

Built Distribution

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

django_credo_sdk-0.1.1-py3-none-any.whl (47.2 kB view details)

Uploaded Python 3

File details

Details for the file django_credo_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: django_credo_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for django_credo_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 64139cd7c725f1f6eb2a6eb607c2591c7d6d7d1ad3b9887179b888bd9d320b55
MD5 405403266197ed1aecba20bed16972c8
BLAKE2b-256 0b82e3c84645b53917af041d7d37211695679af1eb5ea9e83f521118e06da58b

See more details on using hashes here.

File details

Details for the file django_credo_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_credo_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bdcd80da6bd3299e73026488a78a89f1d9a989a19756ff2d2f7877825200a658
MD5 219b166c1e2264509fbfbe3fe3dda405
BLAKE2b-256 5e13d40dd58ac45573288cc513cf2a83a5fb01724c99d276c98dce9ebf38e98d

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