kbz
kbz

Reputation: 1006

Not writing to MySQL database correctly

I have set up an auto-donation feature for a website, but the data is not being written into the database when the IPN is verified by paypal. The IPN log is written to the webhost and it says the payment is verified, but there is no data being written to the database.

Here's the small snippet that handles the ipn

  if ($p->validate_ipn()) {

     $fh = fopen(".ipn", "a");
     fwrite($fh, print_r($p->ipn_data, true));
     fclose($fh);

     if($p->ipn_data["payment_status"] != "Completed") die();

     if($p->ipn_data["mc_gross"] > 0) {
         $user = $p->ipn_data["custom"];
         $date = $p->ipn_data["payment_date"];
         $prodid = $p->ipn_data["item_number"];
         $amount = $p->ipn_data["mc_gross"];
         $amountTickets = 1;

         //$user = str_replace("-", "_", $user);
         //$user = str_replace(" ", "_", $user);
         $user = str_replace("-", " ", $user);
         $user = str_replace("_", " ", $user);
         $user = mysql_real_escape_string($user);
         $conn = mysql_connect("localhost", "USERNAME", "PASSWORD");
         mysql_select_db('web_donations', $conn);
         mysql_query("INSERT INTO donation (username, time, productid, price, tickets) VALUES ('" . $user . "', '" . $date . "', '" . $prodid . "', " . $amount . ", " . $amountTickets . ");");
         $fh = fopen(".ipnerr", "a");
         fwrite($fh, mysql_error());
         fclose($fh);
         mysql_close($conn);
      }
  }

I don't receive any errors. Just the data is not being written to the database, everything else is verified in the logs.

Edit: I have solved the issue, the issue was this:

 if($p->ipn_data["payment_status"] != "Completed") die();

The payments were held in pending which means it would never pass this stage

Upvotes: 0

Views: 69

Answers (1)

Brian
Brian

Reputation: 414

There are some things that you have do in order for this to work, some unrelated to PHP programming, so, i will not post source code. Your comments about it working when invoked manually suggest that the culplrit is the SSL certificate.

Been there! done that!

1.- Make sure your website has a SSL certificate installed. PayPal requires the website to be able to have support for HTTPS instead of just HTTP, it sends the payment notification via HTTPS.

2.- If you are using a saved button Make sure the button points to the correct HTTPS notification url.

3.- There is no way you can read the errors with error_reporting() as sugested, because this is a transaction between PayPal and your website. The only thing you can do is to save somewhere (a file for example) a "flag" to see if the URL was invoked by PayPal, which wont do unless you do the step 1 and 2.

I will add more points if i remember, for now check those. :)

Upvotes: 1

Related Questions