What's next with PayPal IPN?

Hi all, I’m working in the payment process and thinking I will go with PayPal. I have taken a look at the following code from PayPal’s developer centre. Not sure how to implement it.

Do I have a submit button which when pressed runs the code on their site or is there something I’m missing? I’ve as yet never needed a payment gateway but that time has now come in my developing career and hoping someone can help me along that journey…

// PHP 4.1

// 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\
";
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
";
$header .= "Content-Length: " . strlen($req) . "\\r\
\\r\
";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// 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
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// 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
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

No, it’s not code you run when a button is pressed. It’s code you run when PayPal tells your website a payment has been made.
There’s a nice explanation of how it works over here: Paypal IPN tutorial | photoshop and PHP tutorials

Hey man, thank you so much for a reply. Looks very interesting reading, I will take a mooch through it. I’ve managed to do some pretty complex coding so I shouldn’t think it will be impossible, just in unknown territory.

I’ll post back on here if I hit any brick walls if that’s cool?

Sure thing :slight_smile:

One thing that a lot of people overlook with Paypal IPN scripts is that it gets called every time the status of a payment changes.

When someone pays by echeck the payment is pending until cleared by the bank (usually 3-4 days) and so the IPN script will generally get run to indicate that the payment is complete about four days after the person who made the payment has left your site.

Hey guys, I’m a little stuck on which PayPal product I need…I just need a Make a Payment button on the checkout which then takes the user to PayPal and then returns them to the site. Had a look on PayPal and all seem to be using their shopping cart…I’m stuck :confused: