stock update not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

stock update not working

Post by jonnyfortis »

i have a cart set up that uses paypal as the merchant, i have a notify.php and a return.php
the return.php is meant to check to see if the transaction is fulfilled and if it is it redirect to a success.php (this works) then makes the stock up, what seems to be happening though is the notify.php isnt updating the database quick enough so the user is getting redirected to the failed page and therefor not updating the stock.

the notify.php is using the script papal shares.

Code: Select all

<?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.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);
// assign posted variables to local variables
// note: additional IPN variables also available -- see IPN documentation
$itemNumber = $_POST['item_number'];
$paymentStatus = $_POST['payment_status'];
$payerStatus = $_POST['payer_status'];
$pendingReason = $_POST['pending_reason'];
$txnID = $_POST['txn_id'];
$receiverEmail = $_POST['receiver_email'];

mysql_select_db($database_beau, $beau); // find Txn ID
$query_rsTxnId = "SELECT TransactID FROM beauSS14_orders WHERE TransactID = '" . $txnID . "'";
$rsTxnId = mysql_query($query_rsTxnId, $beau) or die(mysql_error());
$row_rsTxnId = mysql_fetch_assoc($rsTxnId);
$totalRows_rsTxnId = mysql_num_rows($rsTxnId);

// Check notification validation
if (!$fp) {
  $errmsg = "$errstr ($errno)";
} else {
  fputs ($fp, $header . $req);
  while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
      if ($paymentStatus != "Completed") {
        $errmsg = "Payment is " . $paymentStatus . " (" . $pendingReason . ")";
      } else
      {
        $errmsg = "";
      }
    }
    else if (strcmp ($res, "INVALID") == 0) {
      $errmsg = "Request is INVALID";
    }
  }
  fclose ($fp);
}
// last check
if ($errmsg != "") {
  $status = 0;
} else {
  $status = 1;
}

// update orders table
if ($itemNumber != "" && $txnID != "" && $paymentStatus != "") {
  mysql_select_db($database_beau, $beau);
  $IPN_upd = "UPDATE beauSS14_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $itemNumber;
  $IPN_rsId = mysql_query($IPN_upd, $beau) or die(mysql_error());
  $IPN_rsId = null;
}
?>
the return.php is the page the user is returned to, i have added a sleep of (3) just to give it more time but this doesnt work

Code: Select all

session_start();
sleep(3);

$colname_rsReturn = "-1";
if (isset($_SESSION['OrderID'])) {
  $colname_rsReturn = $_SESSION['OrderID'];
}
mysql_select_db($database_beau, $beau);
$query_rsReturn = sprintf("SELECT Fulfilled FROM beauSS14_orders WHERE beauSS14_orders.OrderID = %s", GetSQLValueString($colname_rsReturn, "int"));
$rsReturn = mysql_query($query_rsReturn, $beau) or die(mysql_error());
$row_rsReturn = mysql_fetch_assoc($rsReturn);
$totalRows_rsReturn = mysql_num_rows($rsReturn);

if ($row_rsReturn['Fulfilled'] == 1) {
  header ("Location: success.php");
} else header ("Location: failed-order.php");
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: stock update not working

Post by jonnyfortis »

i have done a work around that is a quick fix but am not happy with it.
on the failed page which would notify the use the page has failed i have added the same code as the success page so it double checks, i was getting a looping error in chrome so added more code to get around this

Code: Select all

session_start();
ob_start();

$colname_rsReturn = "-1";
if (isset($_SESSION['OrderID'])) {
  $colname_rsReturn = $_SESSION['OrderID'];
}
mysql_select_db($database_beau, $beau);
$query_rsReturn = sprintf("SELECT Fulfilled FROM beauSS14_orders WHERE OrderID = %s", GetSQLValueString($colname_rsReturn, "int"));
$rsReturn = mysql_query($query_rsReturn, $beau) or die(mysql_error());
$row_rsReturn = mysql_fetch_assoc($rsReturn);
$totalRows_rsReturn = mysql_num_rows($rsReturn);

if ($row_rsReturn['Fulfilled'] == 1) {
  header ("Location: success.php");
} else header ("Location: failed-order.php");
ob_end_flush();
with this code on the failed page the user is then sent to the success page then the stock is updated.
not happy but seems to work
Post Reply