Paypal IPN - Getting + then losing data? - php

I am currently using the following library:
https://github.com/paypal/adaptiveaccounts-sdk-php/tree/master/samples/IPN
And Paypal IPN seems to be working fine... I can successfully checkout and all of the POST data from Paypal is logged to my server, only problem is - How do I check against it when the user is back on the site?
I currently have the return URL set to the same as the IPN url (look in github for code) and after I get all of the valid data, the user gets returned to the IPN page and it says invalid.
Not quite sure, any thoughts?

IPN should NOT be used as the return URL.
Return URL brings the buyer back to your site. The page at the return URL should display either a generic thank you page, or a page indicating the status of the buyer's order. Which one of the above to choose, depends on the PayPal product you select, your integration and your purchasing flow.
The script URL of the IPN listener is used to automate your order handling process. The IPN listener gets the IPN messages, processes your order in your own defined way.
IPN listener should post back very specific data to PayPal IPN endpoint with HTTP Status 200. If it also outputs a page, it makes things too complicated. You would need to differentiate between a buyer visit and a PayPal IPN message, and need to do corresponding display or IPN verification. To make things simple, separating the two things into two pages / scripts would be better and more logical.
I think this is the reason why your buyer gets "invalid": your IPN listener gets the request data when a buyer returns to your listener, and uses the data to verify IPN with PayPal. PayPal returns "invalid" as the data are not from valid IPN messages and thus cannot be verified.

Not a Php dev and haven't looked, but if you really want to use the same URL (I wouldn't - they serve different purposes), then the difference would be the (http) request method:
GET for a redirect and
POST for IPN
That would be one way you'd determine what kind of Request that url is getting and handle as you deem fit.

Related

How to validate a sale with PayPal REST API and IPN

I currently have a fully working cart and checkout process through PayPal _xcart method, but I want to migrate it to REST API, mainly because I want to mitigate the possibility of price-jacking. Currently my IPN does check for price jack and sets the according flags so the product doesn't get downloaded (selling digital products only). Anyway more to the point, I found the PayPal documentation very confusing and I'm struggling to get the full grips of it.
This is what I have understood and worked out till now.
Using my PHP script (let's call it page A) I create the cart content, then I create a new PayPal sale and redirect the client to PayPal for authentication
Client authentication on PayPal, then it's redirected back to my site to page B (page B is defined in page A)
Page B needs to get the PaymentID (from page A) and use it to effectively complete the transaction. Once competed finalize the checkout.
Now here are my problems:
a) I had read quite a few forums and tutorials and they all mention that I should use a session to store the PaymentId from Page A and then use it in Page B to finalize the transaction. Some threads on SO suggests that PayPal should actually include the PaymentID in the call to page B, along with the token and the PayerID. Those are almost 3 years old posts and during my testing I see that PayPal now does return the PaymentID as well.
Is this a new thing, did PayPal really started to send the PaymentID as a GET variable? OK I found this on PayPal SDK documentation (not PHP of course some other language) that they return the PaymentID as well as a GET.
Is there any disadvantage on using the GET presented by PayPal compared to storing a session from page A to B? I don't really wan't sessions, so GET will be ideal for me. I guess if it is on PayPal documentation it's safe to use.
Will this work on the live page as well or only in Sandbox?
b) On Page B when I execute the Payment I get a nice JSON as response, but in the same time my IPN listener also gets called, and this is what confuses me big time. Can/Should I just trust all the data which is in the JSON response and more or less ignore the IPN listener? This will make sense for instant download, for example, much more easier to process, or should I still rely on the IPN for data validation?
If I use only the JSON returned to Page B, which are the correct fields to look for and what values? For example, there is a state field which is approved and another (Transactions -> related items -> state) which is competed. Which one do I need to check?
If I rely on the JSON do I still need to check if the paid amount matches the original amount or can I trust that the payment is equal to the amount I have requested in my call?
If I use the IPN how can I pare it with the transaction? The PaymentID doesn't show up in the variables posted to the IPN. The only way I could think of is to get the txn_id from the JSON response, but somehow that feels odd, plus how can I know if the JSON response hit the server BEFORE the IPN?
Can/Should I just trust all the data which is in the JSON response and more or less ignore the IPN listener?
Yes, and no. In that order.
In a nutshell, you can't trust the payment ID in the call to "Page B" (it could be forged, faked, repeated etc) but you can trust the response YourServer->PayPalServer as it can't be intercepted and faked by the end user.
So your process is (as you describe above)
Page A: Create a sessionID (cookie), amount, cart details etc and store in a local database/storage. You can also create a "custom" field to store your own saleID
Send off amount etc to Paypal, which returns you to ....
Page B: Grab the PayPal TransactionID and send back (server->server) to PayPal. Paypal returns the amount, state etc. Then check your database that the amount is the same and that it belongs to the sessionID. If you also use custom fields, check that too. If everything marries, you're good. If not, it's up to you how to handle.
The status should be "complete" for a simple sale at this point; but (as with IPN below) you should verify this.
Do check the amounts, just in case. They should match, but if not, the PayPal one will be what you receive and it's up to you to accept it, flag it (and phone up) or refund through the API and reject the order etc.
So why have IPN?
It is possible that the user completes the transaction on Paypal and then closes their browser before "Page B" is called. In this case, the only way you know about the order is through the IPN.
If you get an IPN notification going to your IPN handler, IPN can still be faked, but there's a slightly different way of verifying.
You actually send the IPN information back to Paypal (server to server) and Paypal confirms it's correct or wrong (https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNIntro/). Your IPN handler will then check the transaction ID (yes, that's what you use) and verify everything matches in the database (just like you do in "Page B"). If it does, mark the order as complete if the status is complete (and if not already marked as complete in "Page B").
Obviously you can't display anything to the user at this point as they are not the ones that called the page.
The docs above warns you that you can get multiple IPNs for the same transaction so you also need to check status.
(Note: you could use the APIs to verify the transactionID as you do in "Page B").
So why not reply on IPN?
Paypal warn that the IPN may not arrive. Paypal explains it best:
Although PayPal usually processes IPN messages immediately, IPN is not synchronized with actions on your website. Internet connectivity is not always 100% reliable and IPN messages can be lost or delayed. The IPN service automatically resends messages until the listener acknowledges them. The service resends messages for up to 4 days.
Because IPN is not a real-time service, your checkout flow should not wait for the IPN message before it is allowed to complete. If the checkout flow is dependent on receiving an IPN message, processing can be delayed by system load or other reasons. You should configure your checkout flow to handle a possible delay.
So back to the original question
Yes: rely on the JSON (server->server) call you make to verify the parameters to "Page B" (and in and the IPN handler if you choose)
No: Don't ignore the IPN in case Page B never gets called. But still run the verification checks here too.
Yes: Check state = complete for both "Page B" and "IPN handler"
Yes: Use the Paypal TransactionID, but mix into your own database with either custom fields or sessionID.
Yes, you can/will get both Page B and IPN notifications, I'd suggest ignoring the IPN if the payment is already marked as completed, otherwise process and handle appropriately. They should be using the same database.

Paypal ION different servers

so I got a guy to code this IPN feature for my website.
Basically a user pays thought the main site then it forwards the ipn stuff to another server and that server sends ipn payment to paypal. For some reason it keeps saying "retrying" under the paypal IPN history. Is paypal blocking this and does the payment have to be made from the same server? Thanks hopefully this makes sense!
IPN does not happen until after any transaction has occurred, so what you're saying here doesn't really make sense. There's no way IPN would be sending any payment to PayPal. PayPal simply POSTs transaction data to your IPN URL after the transaction has taken place.
That said, if you're seeing "retrying" in your IPN History that means your IPN script is not completing successfully, and as such, is not returning a 200 result code back to PayPal's server. You need to troubleshoot your IPN script to figure out what's failing and get it fixed.
Your web server logs should provide the information you need. If you look there you should see a log of when your IPN URL was hit and it should also provide the result including any errors that were generated from the request to that script.
I like to create a simple HTML form with the action set to my IPN URL and then include hidden fields that match the fields/values that I would expect to get from IPN. This way you can load that in a browser and submit it directly so that you can see the result on screen which can help with troubleshooting and debugging.
Keep in mind that when testing that way, though, the data is not coming from PayPal's servers, so it will not verify. You'll need to make sure your script is setup to handle this accordingly for testing purposes.

PHP - Paypal payment transaction

I've a problem with Paypal IPN. The callback works very well.
I've a site and anyone can signup for free in my site. Later, the user can upgrade the account and here intervenes paypal.
How can I be sure that the user (A) paid to the transaction_id (0123) ?
Ex.
User (A) -> Click to button (item_number = (0009)) -> redirect to paypal page;
User (?) paid -> Paypal IPN sended and server received data -> [Which user has paid?]
!! This, without the user return to the site !!
Thank You
I'm sorry #Vincenzo Raco, but that is bad advice.
PDT and IPN are essentially the same exact thing except that PDT sends data to your return URL and IPN sends it to a silent listener.
There is no guarantee that users will make it back to your return URL, even if you have Auto Return enabled, so it's never a good idea to handle post-payment processing tasks on the return URL. If your IPN is getting hacked that means you have a problem with your configuration.
All IPN's are verified with PayPal's servers, and if you're using API calls, hosted buttons, encrypted buttons, etc. people can't see the original code, copy it, and make adjustments to pricing, which would be the only potential "hack" that anybody could do. Just make sure not to use basic, standard HTML buttons and that potential problem goes away.
IPN is definitely what you want to use to handle your post-payment processing. You can save your user record or whatever data you're working with in your database prior to sending the user over to PayPal. You can include the record ID of your database in the PayPal payment request (I typically use the invoice parameter for this) and then that will be returned in IPN so that you can pull that data back out of your database or add new related accordingly.

Test Paypal IPN in the Sandbox

I am writing a script to handle the paypal IPN, so when someone buys something via paypal it will send them an email and write some info into a database.
I am using the Instant Payment Notification simulator, and it is sending my email ok but i would like it to actually re-direct to my php page, so i can test my error checking etc. When you submit it does call the page but i would like to actually be re-directed to it.
Anyone know if this is possible, or if there is anything i can do to 'fake' this process
Cheers
Luke
Well, you can just test your IPN by using a temporary Payment Data Transfer page. Read this page for more information about this.
So basically after your payment has been done it redirects you to that success page and does everything. So you can use the success page as a test version of your future IPN page. After your script is working exactly the way you want, you can use the code on that page by simply copy-pasting it into your IPN page and deleting that test PDT page.
Another way is to catch the entire output of your IPN page and mail it to you. Or store it somewhere. To do this, make use of PHP's output buffering.
I hope that makes sense to you...
I usually use micahcarrick Paypal IPN class for Paypal IPN.In this class you can see
success case in switch case statement, There you can redirect to your success page.
Actually, Paypal IPN will return response to us. you can see the action parameter in return url. using this you can find is payment is success or cancel or any error during payment.
Hope it helps,Thanks
Paypal IPN messages are Server to Server Messages for asynchronously receiving messages from Paypal regarding transactions. The HTTP Client used by Paypal to deliver IPN message to your server may not support redirect requests. As these HTTP requests are not coming from users browsers' output from your Php script is irrelevant. If you want to do any extended processing you have to incorporate that into your IPN Handler.

$_POST from paypal response after payment is empty

I created a hosted button using a business account and integrated that code with my php application.
I use 'Website Payments Standard' and generated the 'Buy Now Button' using the tool provided in 'Merchant Services' page.
I am using http://sandbox.paypal.com/ to test this.
Configurations in Merchant Profile:
Instant Payment Notification (IPN) is
'Enabled' and 'Notification URL'
given.
Auto Return is On.
Return URL specified (the same given above in notification URL)
Payment Data Transfer is On
Website Payment is Off
PayPal Account Optional: is Off
While creating the button also in the third option I gave the same notify_url.
Transaction happens successfully and gets redirected to my notify_url.
The issue is, $_POST from paypal response in my notified page is empty.
I printed the $_POST in the first line of my notifier page itself. Do I need to add any other configuration? Am I making any mistake?
The data is going to the IPN url. Log the $_POST data to a file from the script at the IPN URL and see what you are getting. I think if you turn off the IPN, you may then get the data on the notify_url (been a while though, not 100% sure on that part).
the notify url is just for receiving the data from paypal about the transaction, it's not a webpage that you can display, because it doesn't happen inside the normal page flow, paypal simple opens a curl command (or similar) to your server and dumps all the data over $_POST
the return url is only used when the user clicks to return to your website.
so don't use the return url for recording the status of the payment, or sending emails, it'll fail badly, always use the notify url for doing that, the return url is just to show the user something pretty after he's paid, or not pretty if it failed

Categories