If you want to validate a Paypal payment process before giving access a client to your product or service you just need tu customize this simple script and you will be safe about fraudulent downloads.
The procedure is very simple.
First you need to create your paypal form in a way that it returns to a script on your site. For example you could create a file named paypal-validator.php on your root site and create your paypal form this way:
-
<input type="hidden" name="cmd" value="_xclick" />
-
<input type="hidden" name="item_name" value="My Product" />
-
<input type="hidden" name="item_number" value="2" />
-
<input type="hidden" name="amount" value="7.00" />
-
<input type="hidden" name="no_shipping" value="1" />
-
<input type="hidden" name="rm" value="2" />
-
<input type="hidden" name="no_note" value="1" />
-
<input type="hidden" name="currency_code" value="USD" />
-
<input type="hidden" name="lc" value="US" />
-
<input type="hidden" name="bn" value="PP-BuyNowBF" />
-
</form>
You can build this form by hand or using the paypal wizard but the important field here is:
You will specify paypal that you want to return the client no directly to your download page but to your paypal php validator script.
Paypal validator file must contain the following code:
-
// First we make some simple validation, you can hardening this to your desire level.
-
echo 'Direct access to this file is not allowed.';
-
-
return;
-
}
-
-
// read the post from PayPal system and add 'cmd'
-
$req = 'cmd=_notify-validate';
-
-
foreach ( $_POST as $key => $value ) {
-
$req .= "&$key=$value";
-
}
-
-
// post back to PayPal system to validate
-
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
-
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
-
-
-
// assign posted variables to local variables
-
$item_name = $_POST['item_name'];
-
$item_number = $_POST['item_number'];
-
$payment_status = $_POST['payment_status'];
-
$payment_amount = $_POST['mc_gross'];
-
$payment_currency = $_POST['mc_currency'];
-
$txn_id = $_POST['txn_id'];
-
$receiver_email = $_POST['receiver_email'];
-
$payer_email = $_POST['payer_email'];
-
-
if (!$fp) {
-
// HTTP ERROR
-
echo 'There was an error connecting back to Paypal please contact us at: <a href="mailto:support@mysite.com">support@mysite.com</a> with your payment details.';
-
-
return;
-
} else {
-
// check the payment_status is Completed
-
// check that txn_id has not been previously processed
-
// check that receiver_email is your Primary PayPal email
-
// check that payment_amount/payment_currency are correct
-
// process payment
-
-
// Change here to give the client access to your product or service
-
echo 'This is a valid Paypal client';
-
-
return;
-
}
-
// log for manual investigation
-
echo 'Restricted Access';
-
echo 'If you got this page after a valid purchasing procedure please contact us at: <a href="mailto:support@mysite.com">support@mysite.com</a> with some details of your Paypal receipt.';
-
-
return;
-
}
-
}
-
}
Feel free to copy and paste this code and begin testing, if you have any question do not hesitate to leave a comment.













4 Comments
Hi,
This is just what I’ve looking for.
I’m pretty new to php. I’m trying to implement this code but I’m having problems, I’m using dreamweaver and have tried pasting the following line:
<input type=”hidden” name=”return” value=”http://www.mysite.com/paypal-validator.php” />
…into my paypal button. and have created a php page with the validation script in. When going through paypal it fails to redirect me to the page I’ve specified in the code above (I’ve changed http://www.mysite.com/etc to the relevant page on my site.)
The validator page seems to be working as I get the “no direct access allowed” message.
Any ideas… have I missed something?
I would begin by checking if the IPN feature in your Paypal Account is enabled, if, as you said, you get the message it seems as the script is ready to get the redirection.
This hidden field is important too: < input type="hidden" name="rm" value="2" />
Let me know what happen.
Thanks for that, I’m still having some trouble, would you mind if I emailed you my code and info on my paypal settings for you to look at? I’m happy to pay you for your time as this is something I’m sooper keen to know how to do.
Thanks in advance, and sorry for the hassle.
i need this Paypal validator in jsp can u give link to take this code