Skip to main content

epay-gateway-django

Chargily ePay Gateway (Django Package)

Chargily ePay Gateway

This Plugin is to integrate ePayment gateway with Chargily easily.

  • Currently support payment by CIB / EDAHABIA cards and soon by Visa / Mastercard
  • This repo is recently created for Django plugin, If you are a developer and want to collaborate to the development of this plugin, you are welcomed!

Requirements

  1. Python 2.7 or higher.
  2. Django 1.11 or higher.
  3. API Key/Secret from ePay by Chargily dashboard for free.
  4. Our package chargily-epay-gateway for python.

Installation

Using pip

pip install chargily-epay-gateway-django-plugin

or pipenv

pipenv install chargily-epay-gateway-django-plugin

Quick start

1- Make sure to secure your credentials by setting up the Environment Variables or using .env file.

2- Load CHARGILY_APP_KEY and CHARGILY_APP_SECRET environment variables in settings.py file.

Usage

1- Make Payment:

  • You can use class or function based view to make a payment
  • If you are using POST method, make sure to disable Django's CSRF validation.
  • Class based View Example
  • In views.py :
from django.shortcuts import render

from chargily_epay_gateway_django.views import InvoiceView
from chargily_epay_gateway_django.forms import InvoiceForm  # You can customize this form if you like

class MakePayment(InvoiceView):

    def get(self, request):
        form = InvoiceForm()
        context = {
            'form': form,
        }
        return render(request, 'payment.html', context)

    def post(self, request):
        form = InvoiceForm(data=request.POST) 
        context = {
            'form': form,
            'data': None,
            'errors': None,
        }
        if form.is_valid():
            invoice = self.make_payment(**form.data)
            response = self.load_invoice(invoice.content)
            if invoice.status_code == 201:
                context['data'] = response['checkout_url']
            else:
                context['errors'] = [value for value in response['errors'].items()]
        return render(request, 'payment.html', context)

Output example:

  • Wrong inputs Wrong inputs

  • Correct inputs Correct inputs

You can use CSRFExemptInvoiceView instead of InvoiceView if you want to ignore csrf validation.

  • in payment.html:
<form method="POST" >

    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="MAKE PAYMENT">

    <!-- Handle Data -->
    {% if data %}
        <p><a target='__blank' href='{{data}}'>{{data}}</a></p>
    {% endif %}

    <!-- Handle Errors -->
    {% if errors %}
    <ul>
        {% for error in errors %}
            <li>{{error}}</li>
        {% endfor %}
    </ul>
    {% endif %}

</form>
  • If you want to return JSON instead, you can use it like this:
from django.http import JsonResponse

from chargily_epay_gateway_django.views import InvoiceView


class MakePayment(InvoiceView):

    def post(self, request):
        invoice = self.make_payment(  # You can use InvoiceForm
            client='Client Name',
            client_email='Client Email',
            invoice_number='Invoice ID',
            amount='Amount',
            discount='Discount',
            back_url='https://example.com/',
            webhook_url='https://example.com/webhook/',
            mode="CIB",
            comment='for integration test',
        )
        return JsonResponse(self.load_invoice(invoice.content))
  • Function based view
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from chargily_epay_gateway.api import make_payment

@csrf_exempt
def invoice(request):
    if request.method != 'POST':
        return JsonResponse({'message': 'Method {} not allowed'.format(request.method)}, status=403)
    response = make_payment(  # You can use InvoiceForm
        client='Client Name',
        client_email='Client Email',
        invoice_number='Invoice ID',
        amount='Amount',
        discount='Discount',
        back_url='https://example.com/',
        webhook_url='https://example.com/webhook/',
        mode="CIB",
        comment='for integration test',
    )
    return JsonResponse(response.json(), status=response.status_code)

2- Webhook Usage:

from django.http import JsonResponse

from chargily_epay_gateway_django.views import WebhookView

from chargily_epay_gateway.utils import signature_is_valid


class ChargilyReceiver(WebhookView):

    def post(self, request):
        valid_signature = signature_is_valid(self.SECRET_KEY, request)
        if valid_signature:
            ...
            # Do whatever you want
        return JsonResponse({}, status=200)

3- Don't forget to register your views in urls.py

from django.urls import path

from your_app.views import MakePayment, ChargilyReceiver


urlpatterns = [
    path('payment/', MakePayment.as_view(), name='payment'),
    path('webhook/', ChargilyReceiver.as_view(), name='webhook'),
]

Configurations

  • Available Configurations
key description redirect url process url
CHARGILY_APP_KEY must be string given by organization required required
CHARGILY_APP_SECRET must be string given by organization required required
back_url must be string and valid url required not required
webhook_url must be string and valid url _ required required
mode must be in CIB,EDAHABIA required not required
invoice_number string or int required not required
client_name string required not required
clientEmail must be valid email This is where client receive payment receipt after confirmation required not required
amount must be numeric and greather or equal than 75 required not required
discount must be numeric and between 0 and 99 (discount in %) required not required
description must be string_ required not required

Notice

Metadata

Release files for chargily-epay-gateway-django-plugin 0.1

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

Source distribution (sdist)

Source distribution for chargily-epay-gateway-django-plugin 0.1
File Size Uploaded
chargily_epay_gateway_django_plugin-0.1.tar.gz 8.8 kB Details

Release files / chargily_epay_gateway_django_plugin-0.1.tar.gz

Download URL chargily_epay_gateway_django_plugin-0.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3182dc8e97014c9eb167db90458fd8f349b6cb8bad649b6ca11e9e5b68c9de31
BLAKE2b-256 checksum
How to use checksums
97df664abbae87149045900b988db574008cd20c06c3a8e666e17a68c0015ea4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

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