<?

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  include "../application.php";
  include "paypal.inc.php";

	ignore_user_abort(true);
	set_time_limit(300);

  // 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 ('www.paypal.com', 80, $errno, $errstr, 30);

  if ($fp) {
    fputs ($fp, $header . $req);

    while (!feof($fp)) {
      $res = fgets ($fp, 1024);

      if (strcmp ($res, "VERIFIED") == 0) {

        if ($receiver_email == $paypal_email) {

          // get the custom value
          $raw          = explode('&',$GLOBALS['custom']);
          $val_username = $raw[0];
          $val_type     = $raw[1];
          $val_id       = $raw[2];
          
          // mark invoice as paid
          MarkAsPaid($val_id);

        }

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

?>
