Jump to content

Paypal Ipn Doesn't Update Database.


Nightasy

Recommended Posts

Hello everyone,

 

I'm having a real issue here with getting a paypal IPN to update the database. When I sandbox tested the IPN it connected successfully but when I used the sandbox to fake a real world transaction the database doesn't get updated.

 

Here's the code for the form that I send to paypal.

 

 

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
 <input type="hidden" name="cmd" value="_xclick">
 <input type="hidden" name="business" value="private@yahoo.com">
 <input type="hidden" name="item_name" value="private.com Lifetime Membership">
 <input type="hidden" name="item_number" value="1">
 <input type="hidden" name="amount" value="7.00">
 <input type="hidden" name="no_shipping" value="1">
 <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">
 <input type="hidden" name="return" value="http://private.com/login.php">
 <input type="hidden" name="cancel_return" value="http://private.com">
 <input type="hidden" name="rm" value="2">
 <input type="hidden" name="notify_url" value="http://private.com/ipn.php" />
 <input type="hidden" name="custom" value="<?php echo $_SESSION['username']; ?>">
<input type="submit" value="Activate Lifetime Membership">
</form>

 

 

Here is the IPN code.

 

<?php
// Connect to database
require 'config.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.sandbox.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'];
$username = $_POST['custom']; // Identify User
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($payment_status=='Completed'){
$txn_id_check = mysql_query("SELECT 'txn_id' FROM 'log' WHERE 'txn_id'='".$txn_id."'");
if (mysql_num_rows($txn_id_check) !=1){
if ($receiver_email=='private@email.com'){

   if ($payment_amount=='7.00' && $payment_currency=='USD'){

 // add txn_id to database
 $log_query = mysql_query("INSERT INTO 'log' VALUES ('','".$txn_id."','".$payer_email."')");

 // update premium to 1
 $update_premium = mysql_query("UPDATE 'members' SET premium='1' WHERE 'username'='".$username."'");

   }
}
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
	 // log for manual investigation
}
}
fclose ($fp);
}
?>

 

 

No matter what I have tried via scouring various search engines and forums I can not seem to find an answer to why this set up is not updating the databases.

 

If anyone could please help I would appreciate it, this little snag is the only thing holding me back from launching the site. I should note that I am a rather novice coder so please do provide examples.

 

Thank you much,

Nightasy

Edited by Nightasy
Link to comment
Share on other sites

Put this at the top of your paypal IPN return script:

$posted_data = print_r($_POST,true);
file_put_contents('IPN_data.txt',$posted_data);

 

This will create a file (in the same directory)named "IPN_data.txt" with the contents of Paypals POST array. Verify that the expected data is arriving correctly, and indexes are not mispelled.

Link to comment
Share on other sites

Alright, I did that.

 

While checking through the values I found out that the payment status is being returned from the fake sandbox account as PENDING.

 

That would explain why it's stopping dead in it's tracks, now I need to figure out how to get the paypal sandbox to verify the payment for testing.

 

Oh, and Thank you very much for your help.

 

I'll look into this and get back to you.

Edited by Nightasy
Link to comment
Share on other sites

Hi,

 

Try changing:

 

$update_premium = mysql_query("UPDATE 'members' SET premium='1' WHERE 'username'='".$username."'");

 

 

To:

 

$update_premium = mysql_query("UPDATE members SET premium = '1' WHERE username = '".$username."'");

 

 

-Lee

Link to comment
Share on other sites

If your premium field is set as integer type, remove the quotes, leaving:

 

$update_premium = mysql_query("UPDATE members SET premium = 1 WHERE username = '".$username."'");

 

It's funny you made that reply and I thank you soo much for it. I had actually scrapped this method and wound up using a different one that worked but when I tried to update that value it was failing. I was just about to ask about that same thing and here you've already answered the question. Thanks soo much man, saved me a lot of time and now I'm almost ready to launch.

Link to comment
Share on other sites

  • 3 months later...

It's funny you made that reply and I thank you soo much for it. I had actually scrapped this method and wound up using a different one that worked but when I tried to update that value it was failing. I was just about to ask about that same thing and here you've already answered the question. Thanks soo much man, saved me a lot of time and now I'm almost ready to launch.

Hi, I have the similar problem. I looked at the content of POST array and couldn't find $res which is supposed to have "VERIFIED". Can you tell me how did you end up fixing the problem? Thanks a lot.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.