0
votes

I am trying to change value in mysql database based on paypal ipn.

my ipn.php:

$db=mysqli_connect("localhost","root","toor","database");

// 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.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header .= "Host: www.sandbox.paypal.com\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'){

 $paylog = $db->query("UPDATE database.users SET money='$payment_amount' WHERE username='$item_name'");

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

c# pay button:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        int value;
        if (Int32.TryParse(textBox1.Text, out value))
            {
                if (value >= 10)
                {
                    Process myProcess = new Process();

                    try
                    {
                        // true is the default, but it is important not to set it to false
                        myProcess.StartInfo.UseShellExecute = true;
                        myProcess.StartInfo.FileName = "https://www.sandbox.paypal.com/cgi-bin/webscr?&cmd=_xclick&[email protected]&currency_code=EUR&amount=" + this.textBox1.Text + "&item_name=" + Form1.sendusername + "&notify_url=http://stormclap.it/ipn/ipn.php";
                        myProcess.Start();
                    }
                    catch (Exception em)
                    {
                        Console.WriteLine(em.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Minimum deposit is 10 EUR!");
                }
            }
    }

Just to understand. I set item_name = username of c# windows form user. User choose payment value and type it into textbox1. Must be >= 10.

When button is clicked ... url to sandbox paypal opens with no problem. Payment is processed and set to completed.

business sandbox ipn url is entered in button and paypal profile.

Value is mysql is not changed. What I did wrong? Please help. Thanks!

2
any reason you're doing your own sockets and http headers instead of using curl or a stream? - Marc B
there is no reason .. I thought it will work without any problems. - Matej Merc

2 Answers

0
votes

I found the solution. I made ipn.php file a bit simple. Now working great.

<?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.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header .= "Host: www.sandbox.paypal.com\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($payment_status=='Completed'){

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

mysql_query("UPDATE database.users SET money='$payment_amount' WHERE username='$item_name'") or die(mysql_error()); 

}
?>
0
votes

I am getting a weird problem. This is code of my notification.php page & this isnt working when Paypal send IPN , but this works fine when i resend the IPN notification or i try to post value from url.

<?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.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header .= "Host: www.sandbox.paypal.com\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($payment_status=='Completed'){

    mysql_connect('mysql.abc.net', 'admin', 'pw');
            mysql_select_db('wbguru_appdb') or die(mysql_error());

            $sql1 = "UPDATE account SET pay_status_ipn = '1' WHERE tx_id = '" . $txn_id . "'";
            mysql_query($sql1);

}

?>