[Answered ]-Understanding where to put the IPN reciever function in django-paypal

2👍

[UPDATE]: Forgot to mention where and how to put the code that you wrote (the handler if you like). Inside the handlers.py file write it like this:

# grap_main/signals/handlers.py

from paypal.standard.ipn.signals import valid_ipn_received, invalid_ipn_received

@receiver(valid_ipn_received)
def show_me_the_money(sender, **kwargs):
    """Do things here upon a valid IPN message received"""
    ...

@receiver(invalid_ipn_received)
def do_not_show_me_the_money(sender, **kwargs):
    """Do things here upon an invalid IPN message received"""
    ...

Although signals (and handlers) can live anywhere, a nice convention is to store them inside your app’s signals dir. Thus, the structure of your grap_main app should look like this:

project/
    grap_main/
        apps.py
        models.py
        views.py
        ...
        migrations/
        signals/
            __init__.py
            signals.py
            handlers.py            

Now, in order for the handlers to be loaded, write (or add) this inside grap_main/apps.py

# apps.py

from django.apps.config import AppConfig


class GrapMainConfig(AppConfig):
    name = 'grapmain'
    verbose_name = 'grap main' # this name will display in the Admin. You may translate this value if you want using ugettex_lazy

    def ready(self):
        import grap_main.signals.handlers

Finally, in your settings.py file, under the INSTALLED_APPS setting, instead of 'grap_main' use this:

# settings.py

INSTALLED_APPS = [
    ... # other apps here
    'grap_main.apps.GrapMainConfig',
    ... # other apps here
]

Some side notes:

  1. Instead of using standard forms to render the paypal button use encrypted buttons. That way you will prevent any potential modification of the form (mostly change of prices).

  2. I was exactly in your track a few months ago using the terrific django-paypal package. However, I needed to upgrade my project to Python 3. But because I used the encrypted buttons I was unable to upgrade. Why? Because encrypted buttons depend on M2Crypto which doesn’t support (yet) Python 3. What did I so? I dropped django-paypal and joined Braintree which is a PayPal company. Now, not only I can accept PayPal payments but also credit card ones. All Python 3!

👤nik_m

0👍

This is is embarrassing but the problem was that I was not using a test business account… I implemented the answer nik_m suggegested as well so I would advise doing the same. Put the function:

def show_me_the_money(sender, **kwargs):
   """signal function"""
   ipn_obj = sender
   if ipn_obj.payment_status == ST_PP_COMPLETED:
       print 'working'
   else:
       print "not working"



valid_ipn_received.connect(show_me_the_money)

in handlers.py

Leave a comment