April 3, 2016 9:24 pm

Executing code if user made a purchase in PayPal

Using PDT, you can verify that a user actually made a purchase. If you have some auto redirect after a user made a purchase and check only the GET parameters of the return URL then any user could trigger the “thank you” page without actually having purchased anything. This might be a problem if you want to send the user a file, allow him access to a portion of the website only if he actually makes the payment or set some database value for the user to indicate that he has paid for the thing.

Executing code if user made a purchase in PayPal

To use PDT, first you would have to go to paypal.com, login and click on Profile. Therafter, you ought to click on My selling tools -> Website preferences.

There, you have to turn on Payment Data Transfer. When you do this you will be given an identity token. You would have to use this in your programming code to use PDT to check if a user has paid.

<img src="http://i1.wp.com/images pharmacie achat cialis.phpgang.com/2016/04/Executing-code-if-user-made-a-purchase-in-PayPal-settings.jpg?resize=394%2C196″ alt=”Executing code if user made a purchase in PayPal settings” data-recalc-dims=”1″ />

It is also a good idea to enable the auto return (in the same page). You can specify a return URL different than what you have set up in the buttons you create through the PayPal page.

Executing code if user made a purchase in PayPal settings

Now, handle the URL where users are going to be returned and add the code below.

The snippet works by using the tx GET parameter which is set when users are auto returned to make an HTTP request to PayPal which will return to us whether the transaction was successful (based on that transaction id, tx)

You just have to add your identity token to the $auth_token variable and add your company name to the User-Agent header and you can only worry about what your application actually needs to do in such cases.

Now, whenever a customer makes a successful payment he will be shown You really made a payment. You can edit that line and add whatever logic your application needs in such cases.

The code above is taken from  https://github.com/paypal/pdt-code-samples where code samples are offered in several programming languages. It was refactored a bit by adding a User-Agent header to the cURL request because PayPal rejects requests which do not set a user agent HTTP header. You can add your PayPal company name there and by checking if there is no transaction id (thetx GET parameter) – in that case the user would be trying to access the page without any traces of a transaction having been made.

Tutorial Categories:

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories: