Help with listener script for PayPal IPN
Posted: Wed Jun 27, 2012 6:49 am
IF you have ever tried implementing PayPal's Instant Purchase Notification (IPN) you might be able to help with this:
I had this thing working okay at some point. Now it doesn't work at all. I have tested the listener script extensively and it seems that the variable $_POST['payment_status'] is always "pending" and never "completed". I have located the point where it seems to be hanging up as noted below. I am testing this from a sandbox account.
I had this thing working okay at some point. Now it doesn't work at all. I have tested the listener script extensively and it seems that the variable $_POST['payment_status'] is always "pending" and never "completed". I have located the point where it seems to be hanging up as noted below. I am testing this from a sandbox account.
Code: Select all
<?php
// This page handles the Instant Payment Notification communications with PayPal.
// Most of the code comes from PayPal's documentation.
require ('includes/commonDefs.inc.php');
require(CONNECTION);
$uid = "215"; //USER IDENTIFICATION HARD WIRED IN FOR TESTING PURPOSES. THIS IS A CUSTOMER FROM MY DB WHO IS MAKING THE PURCHASE
// The config file also starts the session
// Start by creating a request variable:
$req = 'cmd=_notify-validate';
// Add each received key=value pair to the request:
foreach ($_POST as $key => $value) { // Get all the POST vars from Paypal
$value = urlencode(stripslashes($value));
$req .= "&$key=$value"; // the $req will be sent back to PP for confirmation
}
// Open a socket connection to PayPal:
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live
if (!$fp) {
trigger_error('Could not connect for the IPN!');
} else { // Send the request to PayPal:
$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";
fputs ($fp, $header . $req); // Paypal must get back exactly what it sent
// Read in the response:
while (!feof($fp)) { // keep reading until no more to read
// =================================
$res = fgets ($fp, 1024); // get the completed response from Paypal
if (strcmp ($res, "VERIFIED") == 0) { // a-okay, proceed
// ============ THE ABOVE CONDITIONAL HAS TESTED FOR TRUE
// ============ THE CONDITIONAL BELOW NEVER SEEMS TO BE TRUE
if ($_POST['payment_status'] == 'Completed'){
I PUT IN AN UPDATE QUERY HERE TO TEST TO SEE IF IT WAS GETTING WITHIN THIS CONDITIONAL. IT DOES NOT. ABOVE THIS CONDITIONAL, IT SEEMS TO WORK OKAY. THIS CONDITIONAL IS A DUPLICATE OF THE ONE IN THE SCRIPT RIGHT BELOW HERE
}
// ====== THE ABOVE CONDITIONAL NEVER SEEMS TO BE TRUE =====
// Check for the right values:
if ( isset($_POST['payment_status'])
&& ($_POST['payment_status'] == 'Completed')
&& ($_POST['receiver_email'] == 'chopth_1337786135_biz@gmail.com')
&& ($_POST['mc_gross'] == 25.00)
&& ($_POST['mc_currency'] == 'USD')
&& (!empty($_POST['txn_id']))
) {
// Need the database connection now:
// Check for this transaction in the database:
$txn_id = mysqli_real_escape_string($dbc, $_POST['txn_id']);
$q = "SELECT maaMembersID FROM payments WHERE trans_id='$txn_id'";
$r = mysqli_query ($dbc, $q);