2012-01-29, 16:48:40
Here is the PayPal IPN (Instant Payment Notification) script I use to charge for memberships. Eventually I will add a feature to this plugin to make it easier.
IPN Postback script (/PayPal/ipn.php):
Sign up form code:
IPN Postback script (/PayPal/ipn.php):
Code:
<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($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";
$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);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
function XMLsave($xml, $file) {
$success = $xml->asXML($file) === TRUE;
if (defined('GSCHMOD')) {
return $success && chmod($file, GSCHMOD);
} else {
return $success && chmod($file, 0755);
}
}
// PAYMENT VALIDATED & VERIFIED!
$NUSR = strtolower($_POST['custom']);
$usrfile = $NUSR . '.xml';
$password = $_POST['option_selection1'];
$NEMAIL = $_POST['payer_email'];
// create user xml file - This coding was mostly taken from the 'settings.php' page..
$xml = new SimpleXMLElement('<item></item>');
$xml->addChild('Username', $NUSR);
$xml->addChild('Password', $password);
$xml->addChild('EmailAddress', $NEMAIL );
$xml->addChild('Comments', "");
$xml->addChild('Ratings', "");
$xml->addChild('Chatbox', "");
if (! XMLsave($xml, '../data/site-users/' . $usrfile) ) {
$error ='File Not Created';
}
$to = $email;
$subject = 'YourDomain.com Membership | Login Credentials';
$message = '
Thank you for your purchase
Your account information
-------------------------
Email: '.$email.'
Password: '.$password.'
-------------------------
You can now login at http://yourdomain.com/';
$headers = 'From:info@yourdomain.com' . "\r\n";
}
else if (strcmp ($res, "INVALID") == 0) {
// PAYMENT INVALID & INVESTIGATE MANUALY!
}
}
fclose ($fp);
}
?>
Sign up form code:
Code:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<label style="font-weight:bold">Username: </label><input type="text" name="custom" id="username" style="width:220px;padding:2px;margin-bottom:5px;margin-left:5px;"/><br />
<label style="font-weight:bold">Password:</label>
<input type="hidden" name="on1" value="Password" />
<input type="text" name="os1" maxlength="200" style="width:220px;padding:2px;margin-left:5px;" />
<br />
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="D7Y73JX2V9EES">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="MembershipS">
<input type="hidden" name="amount" value="3.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://yourdomain.com/register-success/">
<input type="hidden" name="cancel_return" value="http://yourdomain.com/register-cancel/">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" style="margin-top:15px;margin-left:80px;">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>