Paypal IPN握手未经验证

时间:2016-12-21 15:55:28

标签: php paypal paypal-ipn

我无法让IPN正常工作。我已经多年了。希望你们中的一个可以帮助我。大部分代码都是从http://www.evoluted.net/thinktank/web-development/paypal-php-integration复制来帮助我开始,然后我在php中添加了我需要的代码。我没有使用functions.php。

<?php

// Database variables
$host = "hidden"; //database location
$user = "hidden"; //database username
$pass = "hidden"; //database password
$db_name = "hidden"; //database name

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// assign posted variables to local variables
$data['item_name']          = $_POST['item_name'];
$data['item_number']        = $_POST['item_number'];
$data['payment_status']     = $_POST['payment_status'];
$data['payment_amount']     = $_POST['mc_gross'];
$data['payment_currency']   = $_POST['mc_currency'];
$data['txn_id']             = $_POST['txn_id'];
$data['receiver_email']     = $_POST['receiver_email'];
$data['payer_email']        = $_POST['payer_email'];
$data['playersname']        = $_POST['playersname'];
$data['item']        = $_POST['item'];
$data['price']        = $_POST['price'];
$item_name = $data['item'];
$item_amount = $data['price'];
$player_name = $data['playersname'];


// Create connection
$conn = new mysqli($host, $user, $pass, $db_name);
// Check connection
 if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

    $sql = "INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                '".$data['payment_currency']."' ,
                '".$data['price']."' ,
                '".$data['playersname']."' ,
                '".$data['item'] ."' ,
                '".date("Y-m-d H:i:s")."'
                )";

if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();



// PayPal settings
$paypal_email = 'thomasboyd2013-facilitator@hotmail.co.uk';
$return_url = 'https://28days.000webhostapp.com/Successful.html';
$cancel_url = 'https://28days.000webhostapp.com/Cancelled.html';
$notify_url = 'https://28days.000webhostapp.com/ipn.php';

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
    $querystring = '';
    $data['item']        = $_POST['item'];
    $data['price']        = $_POST['price'];
    $item_name = $data['item'];
    // so I can see who bought what:
    if ($item_name == "500g") {
        $item_amount = 1.00;
    }
    if ($item_name == "2000g") {
        $item_amount = 3.00;
    }
    if ($item_name == "25level") {
        $item_amount = 5.00;
    }
    if ($item_name == "35level") {
        $item_amount = 8.00;
    }
    // Firstly Append paypal account to querystring
    $querystring .= "?business=".urlencode($paypal_email)."&";

    // Append amount& currency (£) to quersytring so it cannot be edited in html
    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }
    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
    $querystring .= "notify_url=".urlencode($notify_url);
    // Append querystring with custom field///////////////////
    $querystring .= "&custom=".$player_name;
    // Redirect to paypal IPN
    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
    exit();
}
    //Database Connection
    $link = mysql_connect($host, $user, $pass);
    mysql_select_db($db_name);

    // Response from Paypal
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
        $value = urlencode($value);
        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
        $req .= "&$key=$value";
    }

    // assign posted variables to local variables
    $data['item_name']          = $_POST['item_name'];
    $data['item_number']        = $_POST['item_number'];
    $data['payment_status']     = $_POST['payment_status'];
    $data['payment_amount']     = $_POST['mc_gross'];
    $data['payment_currency']   = $_POST['mc_currency'];
    $data['txn_id']             = $_POST['txn_id'];
    $data['receiver_email']     = $_POST['receiver_email'];
    $data['payer_email']        = $_POST['payer_email'];
    $data['custom']             = $_POST['custom'];
    $pitem = $data['item_name'] ;
    $pname = $data['custom'];

    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

    if (!$fp) {
    } else {
        fputs($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp($res, "VERIFIED") == 0) {

                // Validate payment (Check unique txnid & correct price)
                $valid_txnid = check_txnid($data['txn_id']);
                $valid_price = check_price($data['payment_amount'], $data['item_number']);
                // PAYMENT VALIDATED & VERIFIED!
                if ($valid_txnid && $valid_price) {
                    $orderid = updatePayments($data);
                    // My code
                        $conn = new mysqli($host, $user, $pass, $db_name);

                        $sql = "SELECT * FROM players";

                        $result = $conn->query($sql);
                        if ($result->num_rows > 0) {
                            // output data

                            while($row = $result->fetch_assoc()) {
                                if($row["Username"]==$pname) {

                                    if ($pitem == "500g") {
                                        $newgold = 500 + $row["Gold"];
                                        $sql = "UPDATE players SET Gold=".$newgold." WHERE Username='".$name."'";
                                    }
                                    if ($pitem == "2000g") {
                                        $newgold = 2000 + $row["Gold"];
                                        $sql = "UPDATE players SET Gold=".$newgold." WHERE Username='".$name."'";
                                    }
                                    if ($pitem == "25level") {
                                        $sql = "UPDATE `players` SET `HeadArmor` = '25', `BodyArmor` = '21', `FootArmor` = '13', `GloveArmor` = '4', `Bow` = '6', `Arrows` = '4', `Xbow` = '5', `Bolts` = '2', `Shield` = '15', `OneH` = '16', `TwoH` = '16', `XP` = '19410', `Level` = '25' WHERE Username='".$name."'; ";
                                    }
                                    if ($pitem == "35level") {
                                        $sql = "UPDATE `players` SET `HeadArmor` = '33', `BodyArmor` = '27', `FootArmor` = '17', `GloveArmor` = '5', `Bow` = '7', `Arrows` = '5', `Xbow` = '6', `Shield` = '19', `OneH` = '21', `TwoH` = '21', `XP` = '54872', `Level` = '35' WHERE Username='".$name."'; ";
                                    }
                                    if ($conn->query($sql) === TRUE) {
                                        echo "Record updated successfully";
                                    } else {
                                        echo "Error updating record: " . $conn->error;
                                    }
                                }
                            }
                        }
                        $conn->close();

                    if ($orderid) {
                        // Payment has been made & successfully inserted into the Database
                    } else {
                        // Error inserting into DB
                        // E-mail admin or alert user
                        // mail('user@domain.com', 'PAYPAL POST - INSERT INTO DB WENT WRONG', print_r($data, true));
                    }
                } else {
                    // Payment made but data has been changed
                    // E-mail admin or alert user
                }

            } else if (strcmp ($res, "INVALID") == 0) {

                // PAYMENT INVALID & INVESTIGATE MANUALY!
                // E-mail admin or alert user
                // Used for debugging
            }
        }
    fclose ($fp);
    }
?>

这可能与sql有关。

0 个答案:

没有答案