Reputation: 1619
Can I create a page that is only viewable to people that have made a PayPal payment?
I am looking for users to make a payment before they can access a page, and if someone tried to view the page without a payment being made they would be redirected to an error page
Is this even a simple task?? If so, how would I go about setting this up and is there any tutorials online?
It's just a simple 2 page site I'm creating. Nothing complex....
I'm using the standard PayPal button just now but I'm not sure it's that simple
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="V6RE5BUJCBAPU">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
Upvotes: 3
Views: 6449
Reputation: 7223
A possible solution is indeed to create a PayPal button. After the user has made the payment you can redirect him/her to a custom page which tells them they have successfully made the payment. You can let PayPal post the payment data to this page and you can verify this way that indeed the payment has been done successfully.
If so, you can set a SESSION or COOKIE variable and on your "target" page (the one they can only access after paying) you can verify if the SESSION or COOKIE has been set or not. If not, redirect them to the payment page.
Example
Make use of something like:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="return" value="http://www.yourwebsite.com/index.php?payment=success" />
</form>
So after the payment has been done the user is redirected to index.php?payment=success and there you read the post from PayPal:
<?php
$req = "cmd=_notify-synch";
$tx_token = $_GET['tx'];
$auth_token = "<your auth token here>";
$req .= "&tx=$tx_token&at=$auth_token";
//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";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
//HTTP ERROR
}
else {
fputs($fp, $header . $req);
//Read body data:
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
$headerdone = true;
}
else{
$res .= $line;
}
}
//Parse the data:
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp($lines[0], "SUCCESS") == 0) {
//Checks the payment_status is completed
//check that txn_id has not been previously processed
for ($i = 0; $i < count($lines); $i++) {
list($key, $val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
}
//Process payment:
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
//etc... you can either insert the payment data into your database for future reference
//Or set up a COOKIE for the user to be able to download your file.
//Or if your payment has been successful, redirect him to a "secret" page where the file is visible.
}
?>
Upvotes: 3
Reputation: 10806
There'll be many solutions to your problem and one could be as follows ..
You can create a Paypal IPN handler page which updates your database when a payment is made. Then you can manually process all the payments and allow those users to view the restricted content. [I am assuming that you have some sort of user management system and are able to allow/disallow users to view some content.]
Just fyi, When you create a paypal button, you can also provide success page URI and failed/canceled page URI.
I hope this helps.
Upvotes: 0