PayPal IPN

Discussion in 'PHP' started by GoglGourd, Mar 8, 2008.

  1. #1
    Hey,

    I'm a complete noob at PayPal IPN and have been looking for quite a while now but am failing to find a good tutorial on how it works/how to integrate it into your site..

    I'm trying to accept paypal payments and then enter the amount donated into a database. So people can add funds on my site, say $10, then that $10 is simply added to their 'credit' in a database. Then they can choose to spend the $10 wherever they like on the site. I can program all of this apart from the PayPal IPN stuff :(.

    Any help with either finding a really dumb-person's guide to paypal ipn or with this code itself would be GREATLY appreciated!
     
    GoglGourd, Mar 8, 2008 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    I think the free script I am giving away this month at zeroscripts has an old IPN routine in cgi format. Maybe you can use it as a model for your work.

    There also some other gpl products out there.

    The gist of it is there must be 2 way communication between your server and paypal. The when the transaction is confirmed then the rest of the script executes and performs whatever actions you have written. In this case it would be to credit member X with Y amount of credits.
     
    Colbyt, Mar 8, 2008 IP
  3. GoglGourd

    GoglGourd Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    yeah, i gathered that, it's putting these words to practise i'm struggling with :p. Cheers for the link, i'll have a study of it :D
     
    GoglGourd, Mar 8, 2008 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    PM me or pst back in this thread if you get stuck.

    I know I have at least one other GPL script somewhere on my hard drive. Though to be honest, I bought a ready-made solution for my own use.
     
    Colbyt, Mar 8, 2008 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The other thing you have to be careful of is the fact that Paypal supports 2 payment notification types. One is PDT, the other is IPN and in a lot of cases I find people talking about PDT and referring to it as IPN. If you know this then you can skip the rest (though there is a tip at the end for you).

    PDT generally happens when the user is sent from Paypal back to your site. When the user is finished making their payment and they click the link back to your site, the link they click has PDT information in it. When the user gets to your site you take that PDT information and then make a request to Paypal to essentially verify it. The important thing to note is this: if your user does NOT follow the link back to your site and if your site ONLY relies on PDT, your site will never know about the payment.

    IPN, on the other hand, is totally separate from the user process. IPN notifications can happen minutes, days or months after the user makes / approves a payment. Once something happens with a payment (eg. a payment is approved, a chargeback is made or a recurring payment is processed) then Paypal hits your IPN script to tell you this. So the good thing about IPN is that it doesn't require the user to be active and come back to your site to know about it. The bad thing is that although it normally happens at the same time that the user makes the payment, that is not necessarily the case...

    PDT is generally used so that you can retrieve information about a payment for your users while IPN is generally used for actually knowing when a payment is made and so on. You will probably need a mix of both for everything to be reliable. The reason I posted this was just so that you were aware that there are two separate processes: it took me a while to get my head around it all, I know that.

    As far as that tip goes... the IPN and PDT URLs you provide to Paypal should not have a query string component in them. Paypal will accept that URL but will silently strip the query string from it before sending data to the URL. This is something that I spent weeks on, I hate to admit.
     
    TwistMyArm, Mar 9, 2008 IP
    GoglGourd likes this.
  6. GoglGourd

    GoglGourd Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Hey,

    Thanks a lot! I've actually got a pretty useful script which uses PDT (just learnt that it's called that :D thanks!), it's nice but I always have the worry of the user not continuing to my site and things getting confusing! I didn't actually know PDT and IPN were different things, I assumed the script I have is some form of poorly done IPN :p *so confusing*

    I'm not sure I understand the need for both IPN and PDT? I thought IPN would update the correct status and the retuning page (should the user visit it) just being a "thanks for the payment, we'll credit your account once paypal confirms it" kinda thing. Would the returning page need to do anything more than perhaps change the transaction ID to "pending" or something of the sort?

    (Did any of what I said then make sense? :p)

    Thanks and +rep all round!
     
    GoglGourd, Mar 9, 2008 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The thing about IPN is that, like I said, it's totally separate from the user.

    With the PDT, the process is something like:
    1) the user makes a payment
    2) Paypal gives the user a link back to your site (with PDT data)
    3) the user clicks the link and goes back to your site, passing along the PDT data
    4) your site takes the PDT data, makes a connection to Paypal to verify the data then shows the user some confirmation page (possibly doing stuff in a database or whatever first).

    With IPN, the process is more like:
    1) The user makes a payment
    2) Paypal itself connects to your server, sending IPN data
    3) your server takes that data and connects back to Paypal to confirm it
    4) after confirmation, your server does what it needs to do with the data

    As you can see, with IPN there is zero interaction with the user after the payment is made. Via IPN your server will know that the payment is made and so can only really say to the user 'hey, thanks for paying' IF they go back to your site and IF the IPN has been made BEFORE they get to your site (some payments stay pending for a while).

    Generally your returning page (which works with PDT) will be able to set the transaction as 'paid' as a lot of the time, with the PDT data, you can still make sure that the payment was processed. You can't necessarily just have your return page set the transaction as pending as it is possible that the IPN has hit your server BEFORE the user gets there.

    If you want to manage pending / approved / denied states, I would set the transaction to 'pending' before forwarding the user to make the payment. Then, in both your IPN and PDT scripts check to see that state before continuing to process. So if the transaction is pending, go on to approve or deny it. Otherwise, just ignore the call. That way it doesn't matter which process hits the server first.

    You asked "Did any of what I said then make sense?'. I'm just going to throw that question straight back at you, because right now I'm hungry, tired and sorely lacking in caffeine :)
     
    TwistMyArm, Mar 9, 2008 IP
  8. GoglGourd

    GoglGourd Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Excellent :D that's really helpful and made sense :p. Thanks a lot, still doubt I can code my own but i'll have a better chance of editing an existing one! At least how it works it makes sense now! Thanks!
     
    GoglGourd, Mar 9, 2008 IP