title: php - PayPal IPN Not Working (on Sandbox)
body: <p><strong>UPDATE: I've even tried having the whole PHP script just be a single line that emails me when the script is viewed, and it still doesn't email me.</strong></p>
  
  <p>I've tried everything, or so I feel like. I have the script set up (very basic just so I can learn how to use it) to insert a row based upon the IPN response (completed, invalid, etc.)</p>
  
  <p>However, it does not appear to be doing anything.</p>
  
  <p>I've tried using the sandbox's built in IPN tester, and it does not do anything. I've tried using a Sandbox-based donator button with "notify_url" as a hidden input type, and it does not do anything.</p>
  
  <p>The only time a row is actually inserted, is when I visit the file directly over my browser (the IPN file, of course). In that case, it inserts a row saying it was an invalid payment.</p>
  
  <p>What in the dickens am I doing wrong or is PayPal's sandbox temporarily screwed up?</p>
  
  <pre><code>&lt;?php
  require("../functions.php");
  sql();
  
  // read the post from PayPal system and add 'cmd'
  $req = 'cmd=' . urlencode('_notify-validate');
  
  foreach ($_POST as $key =&gt; $value) {
      $value = urlencode(stripslashes($value));
      $req .= "&amp;$key=$value";
  }
  
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
  $res = curl_exec($ch);
  curl_close($ch);
  
  
  // 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 (strcmp ($res, "VERIFIED") == 0) {
      // 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
      if($payment_status == "Completed") {
          mysql_query("INSERT INTO donators (username)
  VALUES ('completed')");
      } else {
          mysql_query("INSERT INTO donators (username)
          VALUES ('wrong payment status')");
      }
  
  }
  else if (strcmp ($res, "INVALID") == 0) {
      mysql_query("INSERT INTO donators (username)
      VALUES ('invalid')");
  }
  ?&gt;
  </code></pre>
  
  <p>Donator form:</p>
  
  <pre><code>&lt;form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"&gt;
  &lt;input type="hidden" name="cmd" value="_s-xclick"&gt;
  &lt;input type="hidden" name="hosted_button_id" value="QJUZ2AGANUWYS"&gt;
  &lt;input name="notify_url" value="http://mysite.com/libs/paypal/ipn.php" type="hidden"&gt;
  &lt;input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"&gt;
  &lt;img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"&gt;
  &lt;/form&gt;
  </code></pre>