PayPal IPN没有更新MySQL数据库

时间:2013-06-18 11:04:22

标签: php mysql database paypal paypal-ipn

请提前原谅我的无知,但我对php很新,这个问题一直困扰着我。 我正在尝试为销售单个商品的在线商店编写IPN脚本。付款完成后,脚本将更新数据库并将可用性从“可用”更改为“不可用”。 除了更新数据库之外,IPN似乎工作得很好。我现在已经知道了,因为我看不出脚本有什么问题。这就是我所拥有的:

curl_close($ch);

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

$token = $_POST['invoice'];
$item= $_POST['invoice'];

$conn=new PDO("mysql:host=SERVER;dbname=MYDATABASE","NAME","PASS");
if ($_POST['payment_status'] == 'completed')
{
   $sql="UPDATE `tbl_products` SET `id_status` = 3 WHERE `id_product`=:idproduct";
   $stmt=$conn->prepare($sql);
   $stmt->bindParam(':idproduct',$item);
   $stmt->execute();
}
if ($_POST['payment_status'] == 'pending')
{
    $sql="UPDATE `tbl_products` SET `id_status` = 2 WHERE `id_product`=:idproduct";
    $stmt=$conn->prepare($sql);
    $stmt->bindValue(':idproduct',$item);
    $stmt->execute();
}
foreach ($_POST as $key => $value)
{
    $emailtext .= $key . " = " .$value ."\n\n";
}
mail("MYEMAIL", "Live-VALID IPN", $emailtext . "\n\n" . $req);
}
else if (strcmp ($res, "INVALID") == 0)
{
// log for manual investigation
foreach ($_POST as $key => $value)
{
    $emailtext .= $key . " = " .$value ."\n\n";
}
mail("MYEMAIL", "Live-INVALID IPN", $emailtext . "\n\n" . $req);
}

1 个答案:

答案 0 :(得分:0)

Paypal IPN返回状态的第一个字符是大写。

因此Completed不等于completed。试试这个

curl_close($ch);

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

$token = $_POST['invoice'];
$item= $_POST['invoice'];

$conn=new PDO("mysql:host=SERVER;dbname=MYDATABASE","NAME","PASS");
if ($_POST['payment_status'] == 'Completed')
{
   $sql="UPDATE `tbl_products` SET `id_status` = 3 WHERE `id_product`=:idproduct";
   $stmt=$conn->prepare($sql);
   $stmt->bindParam(':idproduct',$item);
   $stmt->execute();
}
if ($_POST['payment_status'] == 'Pending')
{
    $sql="UPDATE `tbl_products` SET `id_status` = 2 WHERE `id_product`=:idproduct";
    $stmt=$conn->prepare($sql);
    $stmt->bindValue(':idproduct',$item);
    $stmt->execute();
}
foreach ($_POST as $key => $value)
{
    $emailtext .= $key . " = " .$value ."\n\n";
}
mail("MYEMAIL", "Live-VALID IPN", $emailtext . "\n\n" . $req);
}
else if (strcmp ($res, "INVALID") == 0)
{
// log for manual investigation
foreach ($_POST as $key => $value)
{
    $emailtext .= $key . " = " .$value ."\n\n";
}
mail("MYEMAIL", "Live-INVALID IPN", $emailtext . "\n\n" . $req);
}

最好将状态存储在变量中,并使用php strtolower函数将它们设置为小写。

$paypalStatus = strtolower($_POST['payment_status']);

比这样检查

if($paypalStatus == 'pending')