IPN模拟器成功但不运行数据库操作

时间:2013-12-17 20:05:04

标签: php database curl paypal paypal-ipn

由于某种原因,我有更多的麻烦,那么我应该...我有一个IPP的Paypal列表,IPN模拟器说每次成功和使用不同的方法,但我不能得到它然后操纵基于数据库成功的回应。

任何想法?

<?php

//INCLUDE CONNECTION STRING
include('connect.php');

// STEP 1: read POST data

// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}  
foreach ($myPost as $key => $value) {        
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
    $value = urlencode(stripslashes($value)); 
} else {
    $value = urlencode($value);
}
$req .= "&$key=$value";
}


// STEP 2: POST IPN data back to PayPal to validate

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

if( !($res = curl_exec($ch)) ) {

error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

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

// 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($payment_status=="Completed"){

    $selectuser = mssql_query("select statsmemberid from statsmembers where email='$payer_email'");

            if(mssql_num_rows($selectuser) != 0){
            $row = mssql_fetch_row($selectuser);
            $statsmemberid = $row[0];

            $getCredits = mssql_query("select creditsbought from statsmuplayers where statsmemberid='$statsmemberid'");

            $row = mssql_fetch_row($getCredits);
            $totalCredits = $row[0]+11;             

            $updatemu = mssql_query("update statsmuplayers set creditsbought='$totalCredits' where statsmemberid='$statsmemberid'");
            echo "Credits Applyed";
            }else{
                echo "Invalid Email";
            }




}


} else if (strcmp ($res, "INVALID") == 0) {
// IPN invalid, log for manual investigation
echo "The response from IPN was: <b>" .$res ."</b>";
}
?>

1 个答案:

答案 0 :(得分:0)

我建议你将所有这些代码封装到一些对象中。这将真正帮助你弄清楚出了什么问题。

你想要一个处理与paypal进行IPN通信的对象(你不需要从头开始编写这是我在谷歌搜索中找到的第一个PHP实现https://github.com/dodev34/paypal-ipn-response-client

然后,您需要一个处理数据库连接的基础对象。最后,您需要一个statsmembers对象来扩展您的数据库连接对象并强制执行您的业务逻辑。您可能只需提取一些PHP ORM代码,就像您在此处看到的那样http://www.phpactiverecord.org/projects/main/wiki/Quick_Start

这将允许您与实际的paypal IPN通信分开测试更新功能。好消息是,您不必依赖于编写系统文件进行调试,就像上面评论中建议的那样。

我一眼就看不到代码中的具体错误,您确定通过paypal正确传递了payer_email吗?