Package: chargily-epay-gateway-django-plugin
epay-gateway-djangoChargily ePay Gateway (Django Package)This Plugin is to integrate ePayment gateway with Chargily easily.Currently support payment byCIB / EDAHABIAcards and soon byVisa / MastercardThis repo is recently created forDjango plugin, If you are a developer and want to collaborate to the development of this plugin, you are welcomed!RequirementsPython 2.7 or higher.Django 1.11 or higher.API Key/Secret fromePay by Chargilydashboard for free.Our packagefor python.InstallationUsing pippipinstallchargily-epay-gateway-django-pluginor pipenvpipenvinstallchargily-epay-gateway-django-pluginQuick start1- Make sure to secure your credentials by setting up the Environment Variables or using.envfile.2- LoadCHARGILY_APP_KEYandCHARGILY_APP_SECRETenvironment variables in settings.py file.Usage1- Make Payment:You can use class or function based view to make a paymentIf you are usingPOSTmethod, make sure todisable Django's CSRF validation.Class based View ExampleInviews.py:fromdjango.shortcutsimportrenderfromchargily_epay_gateway_django.viewsimportInvoiceViewfromchargily_epay_gateway_django.formsimportInvoiceForm# You can customize this form if you likeclassMakePayment(InvoiceView):defget(self,request):form=InvoiceForm()context={'form':form,}returnrender(request,'payment.html',context)defpost(self,request):form=InvoiceForm(data=request.POST)context={'form':form,'data':None,'errors':None,}ifform.is_valid():invoice=self.make_payment(**form.data)response=self.load_invoice(invoice.content)ifinvoice.status_code==201:context['data']=response['checkout_url']else:context['errors']=[valueforvalueinresponse['errors'].items()]returnrender(request,'payment.html',context)Output example:Wrong inputsCorrect inputsYou can useCSRFExemptInvoiceViewinstead ofInvoiceViewif you want to ignore csrf validation.inpayment.html:<formmethod="POST">{% csrf_token %}
    {{form.as_p}}<inputtype="submit"value="MAKE PAYMENT"><!-- Handle Data -->{% if data %}<p><atarget='__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:fromdjango.httpimportJsonResponsefromchargily_epay_gateway_django.viewsimportInvoiceViewclassMakePayment(InvoiceView):defpost(self,request):invoice=self.make_payment(# You can use InvoiceFormclient='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',)returnJsonResponse(self.load_invoice(invoice.content))Function based viewfromdjango.httpimportJsonResponsefromdjango.views.decorators.csrfimportcsrf_exemptfromchargily_epay_gateway.apiimportmake_payment@csrf_exemptdefinvoice(request):ifrequest.method!='POST':returnJsonResponse({'message':'Method{}not allowed'.format(request.method)},status=403)response=make_payment(# You can use InvoiceFormclient='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',)returnJsonResponse(response.json(),status=response.status_code)2- Webhook Usage:fromdjango.httpimportJsonResponsefromchargily_epay_gateway_django.viewsimportWebhookViewfromchargily_epay_gateway.utilsimportsignature_is_validclassChargilyReceiver(WebhookView):defpost(self,request):valid_signature=signature_is_valid(self.SECRET_KEY,request)ifvalid_signature:...# Do whatever you wantreturnJsonResponse({},status=200)3- Don't forget to register your views in urls.pyfromdjango.urlsimportpathfromyour_app.viewsimportMakePayment,ChargilyReceiverurlpatterns=[path('payment/',MakePayment.as_view(),name='payment'),path('webhook/',ChargilyReceiver.as_view(),name='webhook'),]ConfigurationsAvailable Configurationskeydescriptionredirect urlprocess urlCHARGILY_APP_KEYmust be string given by organizationrequiredrequiredCHARGILY_APP_SECRETmust be string given by organizationrequiredrequiredback_urlmust be string and valid urlrequirednot requiredwebhook_urlmust be string and valid url                                                                          _requiredrequiredmodemust be inCIB,EDAHABIArequirednot requiredinvoice_numberstring or intrequirednot requiredclient_namestringrequirednot requiredclientEmailmust be valid email This is where client receive payment receipt after confirmationrequirednot requiredamountmust be numeric and greather or equal than  75requirednot requireddiscountmust be numeric and between 0 and 99  (discount in %)requirednot requireddescriptionmust be string_requirednot requiredNoticeIf you faced IssuesClick here to open one