Package: drf-instamojo
Instamojo | Django REST FrameworkA package for Instamojo integration in Django REST FrameworkInstamojo | Django REST Frameworkis a Django packaged app that provides necessaryviewsbased in Django REST
Framework. It enables easy integration of Instamojo Payment Gateway with Web/Mobile Application with a RESTful API
based server.Contributors:WE'RE LOOKING FOR SOMEONE WHO CAN CONTRIBUTE IN DOCSCivil Machines Technologies Private Limited: For providing me platform and
funds for research work. This project is hosted currently withCMTonly.Himanshu Shankar: Himanshu Shankar has initiated this project and worked on this
project to collect useful functions and classes that are being used in various projects.InstallationDownload and Install viapippip install drf_instamojoorDownload and Install viaeasy_installeasy_install drf_instamojoAdd, if wanted,drfaddonsinINSTALLED_APPS(This is although not required!)INSTALLED_APPS = [
    ...
    'drf_instamojo',
    ...
]Also add other dependencies inINSTALLED_APPSINSTALLED_APPS = [
    ...
    'drfaddons',
    ...
]Include urls ofdrf_instamojoinurls.pyurlpatterns = [
    ...
    path('api/instamojo/', include('drf_instamojo.urls')),
    ...
]

# or

urlpatterns = [
    ...
    url(r'^api/instamojo/', include('drf_instamojo.urls')),
    ...
]Run migrate command:python manage.py migrateMODELSThe application has three models:InstamojoConfiguration: You need to define your Instamojo configurations in this model. Only one object can haveis_activeset toTruewhich will be used with Instamojo API.PaymentRequest: This will contain all the Instamojo Payment Request that one will create with Instamojo.Payment: This will contain all the responses received from Instamojo API against payment.VIEWSThe application has following views:ListAddPaymentRequestView: All payment request should be made on this view. Requires a logged in user.
It'll provide user with required data, includinglongurlthat will be used to make payment.ListAddPaymentView: All response data should be posted on this view. Doesn't requires a logged in user.URLSrequest/: All payment request to be made via this URL.payment/: All payment reponses to be posted on this URL.Quickstart GuideCompleteInstallation Steps(mentioned above)Create a configuration viaDjango AdmininInstamojo ConfigurationSetis_activetoTrueNote: Use sandbox mode credential at firstHow to integrate with appsUseserializersto integrate with custom apps.Creating Payment RequestExampleCreate a view:PaymentViewUsePaymentRequestSerializerto create a payment request.Check example code inserializers.py:# Initialize serializer with proper data
prs = PaymentRequestSerializer(data={'amount': 120.00, 'purpose': 'Test', 'send_sms': False,
                                     'redirect_url': 'http://127.0.0.1/api/test/'})

# Check if data is valid
prs.is_valid(raise_exception=True)

instance = prs.save(created_by=user)Saveinstanceas foreign key in app pointing to bill for which the payment request is made.Returnlongurlto client for making paymentProblem with created_by requirement in .save() of serializerIf app doesn't require user to login, use following logic:Inside payment view, take following data from user:Name of the customerMobile Number of the customermobile: 9987987345Email of the customeremail: test@abc.comUse following code to create a logic around getting an existing or creating a new user:from django.contrib.auth import get_user_model

user_model = get_user_model()
try:
    user = user_model.objects.get(email=email)
except user_model.DoesNotExist:
    try:
        user = user_model.objects.get(mobile=mobile)
    except user_model.DoesNotExist:
        # Now you're dead sure that user does not exists.
        user = user_model.objects.create(name=name, mobile=mobile, email=email, password="RANDOM_PASSWORD")Otherwise, simply useuser = request.useras user while calling.save()on serializer.Bonus Pointer: Check outDjango REST Framework - UserPayment CompletionUsepayment_donesignalCreatesignalspython directory with__init__.py, handlers.pyin appInhandlers.py, create a function for handlingpayment_donesignalfrom django.dispatch import receiver

from drf_instamojo.models import PaymentRequest
from drf_instamojo.signals import payment_done


@receiver(signal=payment_done, sender=PaymentRequest)
def payment_done_handler(instance: PaymentRequest, sender, *args, **kwargs):
    ...
    # Payment completed
    # bill.paid()
    # item.dispatch()
    ...