Marko Maksimovic
Marko Maksimovic

Reputation: 87

Django PayPal delay

I'm creating a payment system for a project I'm working on and I'm using django-paypal. I followed their guide and implemented the signals and everything is working correctly (user clicks on a button -> gets redirected to paypal -> if everything is good success page is shown). The thing is in my signals I have a thing where I want to give users points when the do the buying function. I have that in my signals but there is a delay between showing the success page and the actual signal receiving the data and being executed. I don't know if this is because I'm using stuff like ngrok and localtunnel or is it something else.

This is my signals.py

from paypal.standard.models import ST_PP_COMPLETED
from paypal.standard.ipn.signals import valid_ipn_received, invalid_ipn_received
from account.models import Account

def show_me_the_money(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
        if ipn_obj.receiver_email != "**":
            # Not a valid payment
            print("BAD EMAIL")
        else:
            print("ALL GOOD")
            acc = Account.objects.get(account_url=ipn_obj.custom)
            acc.coins = acc.coins + int(ipn_obj.mc_gross)
            acc.save()
    else:
        print("FAIL")

valid_ipn_received.connect(show_me_the_money)

So if I'm understanding everything this is all correct but for some reason the delay is happening and I don't know whats causing it.

Upvotes: 0

Views: 118

Answers (1)

Preston PHX
Preston PHX

Reputation: 30377

IPN is an asynchronous post from the PayPal servers to yours, so you can always expect some unspecified delay which may vary. It is not truly "instant", despite the name (only more instant than checking your emails, which was the notion when it came out decades ago). It's an old piece of technology and certainly not an ideal solution to be relying on.

The recommended solution would be to do a server-side integration of a current PayPal API. Create two routes on your server that return JSON, one for 'Create Order' and one for 'Capture Order', documented here. The second route can check for success and do its show_me_the_money or any other business logic right before returning its JSON response.

The approval flow to pair with your above two routes is: https://developer.paypal.com/demo/checkout/#/pattern/server

Upvotes: 1

Related Questions