Jump to content

Recommended Posts

Hi Guys

 

I have created a registration page where when users login they can pay to become premium, however everything looks fine but paypal wont update the field. The field is 0 and needs to be 1 once payment has been made. Below is my code,

 

<?php

 

    //connect to database

    $error = "Problem connecting";

    mysql_connect('localhost','_prince','getmeout') or die($error);

    mysql_select_db('hmt_cms') or die($error);

 

 

 

 

// 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'];

 

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")

 

{

 

if ($payment_amount==1.00&&$payment_currency=="GBP")

{

 

$update = mysql_query("UPDATE cms SET premium='1' WHERE email='$payer_email'");

 

}

 

 

}

 

 

// 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);

}

?>

 

 

im testing it from sanbox and these are my settings.

 

IPN handler URL is correct

Transaction type : cart checkout

payment_status : completed

payment type: instant

 

email is the same email registered

mc_fee  = 1

the rest of mcs are set to 0

 

currency is GBP

 

 

any help would be appreciated,

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/219383-paypal-ipn-wont-update/
Share on other sites

This seems to be a common problem wiht PayPal IPN.

 

I always make sure I send myself an e-mail using the PHP mail function, this allows me to see what variables PayPal sends back and whther or not it is working.

Add one mail after each section of code, this allows you to find out where the script is stopping, if it is stopping at all.

Try adding some IF ELSE statements around the MySQL querys to check that they are functioning correctly and that indeed you are getting back the data and using it how you want too.

 

Regards, Paul.

<?php

     //connect to database
    $error = "Problem connecting";
    mysql_connect('localhost','_prince','getmeout') or die($error);
    mysql_select_db('hmt_cms') or die($error);

mail('you@example.com', 'IPN Progress', 'Section 1 Completed');
   

// 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";
}

mail('you@example.com', 'IPN Progress', 'Section 2 Completed');

// 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);

mail('you@example.com', 'IPN Progress', 'Section 3 Completed');

// 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) {
mail('you@example.com', 'IPN Progress', 'IPN Error 1');
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

if ($payment_status=="completed")

{
mail('you@example.com', 'IPN Progress', 'Section 4 Completed');
if ($payment_amount==1.00&&$payment_currency=="GBP")
{
mail('you@example.com', 'IPN Progress', 'Section 5 Completed');
$update = mysql_query("UPDATE cms SET premium='1' WHERE email='$payer_email'");

}


}


// 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
mail('you@example.com', 'IPN Progress', 'IPN Error 2');
}
}
fclose ($fp);
}
?>

 

Try using the code above in place of your own, and change the email address to your own address.

 

Then see how many emails you get, if you don't get 1 of the completed ones theres an error before that section, if you get an error email you know you aren't getting the correct details back from PayPal.

 

Regards, Paul.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.