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

360 views
Skip to first unread message

Michael Goytia

unread,
Mar 21, 2017, 4:59:29 PM3/21/17
to Django users
Hello All,

I have a similar issue to the  post "Updating Django database when using django-paypal"

What we are trying to do(I don't know this other person but I assume we stand together on this topic) is to understand how to deduce a path to receiving a paypal IPN signal in which we then can update the django database.

I have implemented the django-paypal API by following the directions here

Overall what I do is I create a view in my views.py as follows
 
def payment_page(request):
   """This fucntion returns payment page for the form"""
   if not request.session.get('form-submitted', False):
       return HttpResponseRedirect(reverse('grap_main:error_page'))
   else:
       amount = set_payment()
       paypal_dict = {
           "business": "busi...@gmail.com",
           "amount": str(amount),
           "item_name": "2017 USGF Championships",
           "notify_url": "https://15b6b6cb.ngrok.io" + reverse('paypal-ipn'),
           "return_url": "https://15b6b6cb.ngrok.io/Confirm",
           "cancel_return": "https://15b6b6cb.ngrok.io/RegistrationForm",

  }
       form = PayPalPaymentsForm(initial=paypal_dict)
       context = confirm_information()
       context["amount"] = amount
       context["form"] = form
       request.session['form-submitted'] = False
       valid_ipn_received.connect(show_me_the_money)
       return render(request, "grap_main/payment.html", context)


Where my then I have payment.html which then create the paypal button simply by using the line as advised in the documentation 
{{ form.render }}

Now I can receive a POST to the paypal url that I specified in the documentation however I don't know where I should put my signal function that will grab the IPNs once someone has completed a purchase. 

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"


Right now I am calling this function in the view payment_page() however I know this is before the POST to the paypal and is not correct 
I don't understand where I should call the show_me_the_money() function. I am used to creating a views in which is called from the html script as shown below

def register(request):
   """aquire information from new entry"""
   if request.method != 'POST':
       form = RegisterForm()
   else:
       if 'refill' in request.POST:
           form = RegisterForm()
       else:
           form = RegisterForm(data=request.POST)
           if form.is_valid():
               form.save()
               request.session['form-submitted'] = True
               return HttpResponseRedirect(reverse('grap_main:payment_page'))

<form action="{% url 'grap_main:register' %}" method='post' class="form">
     {% csrf_token %}
     {% bootstrap_form form %}
     <br>
     {% buttons %}
     <center>
       <button name='submit' class="btn btn-primary">Submit</button>
     </center>
     {% endbuttons %}
   </form>


I believe I need to call the function after a person has completed a purchase however I don't know how to target that time window in my code. I want to make sure I handle a case in which the user once there are done paying don't always return to the merchant site.

Any help on this subject would not only benefit me but also the earlier poster. I hope to make a tutorial when I figure this out to help others that might get stuck as well. 

Please note that I have also use ngrok to make sure my project is accessible to the paypal IPN service. I am using two urls.py files as well, i which the main one looks as so 

urlpatterns = [
   url(r'^paypal/', include('paypal.standard.ipn.urls')),
   url(r'^admin/', admin.site.urls),
   url(r'', include('grap_main.urls', namespace='grap_main')),
]

where the 'grap_main.urls' are all the specific views for my site, ex.

urlpatterns = [
   url(r'^$', views.index, name='index'),
   url(r'^Confirm', views.confirm, name='confirm'),
   url(r'^Events', views.events, name='events'),
   
.........]



Thanks.


Michael Goytia

unread,
Mar 22, 2017, 10:30:25 PM3/22/17
to Django users
Well... I wasn't using a paypal test business account... changed that and it worked. O well

See here for the full answer to question 
Reply all
Reply to author
Forward
0 new messages