OKPAY Callback API更新数据库

时间:2015-02-17 01:30:57

标签: php

好的我正在使用OKPAY在付款完成时启动IPN回调,我已经习惯了测试工具发送成功付款的请求,但它没有更新我的信息

    $market = isset($_GET['ok_txn_currency']) ? $_GET['ok_txn_currency'] : '';
    $amount = isset($_GET['ok_txn_net']) ? $_GET['ok_txn_net'] : '';
    $username = isset($_GET['username']) ? $_GET['username'] : '';
    $status = isset($_GET['ok_txn_status']) ? $_GET['ok_txn_status'] : '';
    $txid = isset($_GET['ok_txn_id']) ? $_GET['ok_txn_id'] : '';


    // Read the post from OKPAY and add 'ok_verify' 
    $req = 'ok_verify=true'; 
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    // Post back to OKPAY to validate 
    $header .= "POST /ipn-verify.html HTTP/1.0\r\n"; 
    $header .= "Host: www.okpay.com\r\n"; 
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 
    $fp = fsockopen ('www.okpay.com', 80, $errno, $errstr, 30); 

    if (!$fp)
    {
        exit('Error postback');
    }
    fputs ($fp, $header . $req); 
    while (!feof($fp))
        $res = fgets ($fp, 1024); 
    fclose ($fp);
    if ($res != "VERIFIED")
    {
        exit('Not verified');
    }

    if($_POST['ok_txn_status'] !== 'completed')
        exit('Invalid ok_txn_status');


        $checktx = $this->db->prepare("SELECT * FROM transactions WHERE txid=? LIMIT 1");
        $checktx->execute(array($txid));
        $result = $checktx->fetch();
        if(!$result):
            if(isset($market) && isset($amount)):
            //insert into transactions and update balance
            $transaction = $this->db->prepare("INSERT INTO transactions(market,amount,
            username,txid,transaction,status) VALUES(?,?,?,?,?,?)");
            $transaction->execute(array($market,$amount,$username,$invoice,'deposit','1'));
            if($transaction):
            echo 'debug 1';
            echo $market;
                //get the user and their balance
                $getuserbalance = $this->db->prepare("SELECT * FROM user WHERE username=?");
                $getuserbalance->execute(array($username));
                $userbalance = $getuserbalance->fetch();
                //this is their new balance
                $newbalance = $amount + $userbalance->{$market};
                $updateuser = $this->db->prepare("UPDATE user SET ".htmlentities($market)."=? WHERE username=?");
                $updateuser->execute(array($newbalance,$username));
            endif;
        endif;
    endif;

它给了我200OK的回报,但没有给我提供数据库。对不起长代码

1 个答案:

答案 0 :(得分:0)

if ($res != "VERIFIED")
{
    exit('Not verified');
}

这是你的错误。当您使用“IPN测试工具”时,状态将为“TEST”而不是“VERIFIED”