How do I pass on vars during a paypal payment?
I would like to get the user's id and the item number back (when transaction is completed).
Thanks in advance
Unfortunately, it does not appear the api return a userid or email that i can find- but it does require you send it. there is a transaction number that can be cross referenced in your own database - here is a link for more reading.
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_APExecutePaymentAPI
I recommend maintaining all the customer information in your own database, and when using the api call, since you will receive an immediate ack, you will know if it is successful - and can use that to trigger a write to your database of the same information you just sent, along with the response.
Related
I'm just now trying to get up to speed with PayPal Express Checkout (i.e. checkout.js), using the client-side REST integration described here. I see that when payment is complete, my onAuthorize function is invoked with a "payment" object.
I can't find any documentation on this object, but some poking at it reveals the following properties (at least today):
paymentToken
payerID
paymentID
intent
returnUrl
Now I need to redirect the user to the next step on my website, where I show a receipt confirming they've paid, etc. I guess I send the above data to the server, but since that step could be easily spoofed by a malicious user, I will need to verify those details in the PHP code, server side.
How do I do that?
You can make a GET call on your server side to /v1/payments/payment/PAY-XXXXXX with the paymentID and the payerID to get the payment details, and verify those details there.
https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/advanced-payments-api/show-payment-details/
See https://developer.paypal.com/docs/api/overview/#make-your-first-call for some basics on calling the REST api from your server
Assuming you are using PayPal Encrypted Buttons, you don't actually need to verify the amounts sent through server-side. Although a user could indeed manipulate the $_POST data, PayPal's got you covered, and won't allow the transaction to go through. This is because PayPal Encrypted Buttons are generated with your variables such as price built-in to the ID. If the variables don't align with those used to create the button, the transaction is denied.
Alternatively, if you are simply using your own code to make the request, you can secure the payments with PayPal's Instant Payment Notification. Again, this allows any $_POST data to be sent through for the payment. Afterwards, PayPal makes a call to your IPN page in order to validate that the parameters are correct. This is demonstrated in the following workflow:
When communicating with your IPN, if PayPal finds that the values don't match up, the order is cancelled. Assuming that the values match up, you can safely redirect them to your confirmation page.
Hope this helps! :)
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.
Payal say that IPN can take a while for orders to be relayed. I am wondering if there is a way with the other Paypal APIs to instantly fetch information about an order, the moment the user is redirected to the success URL of my site.
The problem is that only two variables seem to be relayed in the querystring to my success page: token and PayerID.
The reason I want to do this is I want to allow users to complete the checkout process without logging into my site, but then once the order is complete and they're sent to my success page, I want my site to be able to link them to their previous orders as well as this one, which would be extracted from my database.
Can this be done using token and/or PayerID? If so, how? (I'm using the PHP scripts that the Express checkout wizard provides)
IPN is generally pretty much real-time. There are times when it gets a little lagged and can be kind of slow, but it doesn't seem to happen all that much.
That said, the API calls along within your checkout will indeed return lots of good info in the actual response. Your URL only has the parameters you mentioned (token and Payer ID), however, the GetExpressCheckoutDetails response would have all the buyer info you need, and DoExpressCheckoutPayment will return the transaction ID, payment status, payer status, etc. So the DECP probably has the majority of what you're after.
You can use session variables to save GECD response data and DECP response data accordingly and then update your database, send out email notifications, etc. after calling DECP.
The thing is, the payment could end up being "pending" for various reasons. As such, IPN would still be the best way to handle this sort of thing so that you can update your DB and send out one notification for the pending payment being received, and then another update/email once that payment actually clears.
I need to make a modification to our custom cart. Basically as it currently stands when we get a payment we use IPN to post back to our site and display the details of the purchase, we then get an email from PayPal with all the details in.
I have an excel spreadsheet that then accesses Outlook and prints an Invoice based on this email and adjusts the stock database etc, at this point the order is given an Invoice number.
However, I want to make it that the invoice number is given at the checkout stage, now I know you can set the 'invoice' variable as a pass thru variable, however, what I am unable to determine from the online literature is whether or not this 'invoice' value is returned in the the PayPal instant Payment Notification email, so my excel spreadsheet can use it?
If the invoice number is not sent in the email, is there any field you know of that is?
All the best and thank you in advance.
First, it sounds like you're confusing IPN and PDT. They're very similar, but if the data is getting sent to your return URL then that would be PDT. IPN gets POSTed to a separate script outside of your checkout altogether.
IPN is the recommended method of updating things and sending out emails, etc. because even with Auto-Return enabled there is no guarantee your buyers will make it back to your return URL (when using Payments Standard.) IPN will always be triggered regardless.
So, back to your question, yes, the invoice number will be sent with IPN notifications if you pass it to PayPal with the payment request. The parameter is called "invoice". You can see all the available parameters that IPN works with in the PayPal documentation.
I've set up my IPN on PayPal and I get the transaction id, product name and all that but my question is how can I set up custom fields that I can retrieve after payment using the IPN?
Thanks in advance
There is a parameter called CUSTOM that you include in your button code or API calls. Anything you pass here will come back in IPN as $_POST['custom']. If you need multiple parameters you could store them there as an NVP string or however you want to pass them and then parse them back out within your IPN script.
Alternatively, you could save all your order data to your database prior to sending the user over to PayPal. Then you can include your db record ID in the PayPal payment using the INVOICE parameter, which again, would come back in IPN as $_POST['invoice']. With that you could pull the data back out of your DB based on the record ID and process it accordingly.