PayPal IPN响应无法插入数据库

时间:2015-08-20 09:45:33

标签: php string paypal paypal-ipn

所以最近我建立了一个基于其他人的小型IPN系统,它运行良好。我可以通过沙箱购买此项目,它将通过IPN发送信息给我的监听器,并将插入到数据库中。但是现在当我昨天尝试它时,在没有对文件进行任何更改之后,它将无法正常工作。它确实向我发送了IPN,我可以将它存储在.txt中 - 但这也是我可以做到的。如果我然后手动接收响应并将字符串更改为该值(来自.txt),则一切正常。我不明白为什么会这样,我已经尝试了很多方法来解决这个问题。

以下是我从paypal获得的回复(我的自适应IPN的问题,之前有效)

VERIFIED -- cmd=_notify-validate&mc_gross=15.00&protection_eligibility=Eligible&address_status=confirmed&payer_id=UXU2LGHMZ8FQC&tax=0.00&address_street=1+Main+St&payment_date=02%3A36%3A31+Aug+20%2C+2015+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Purple&mc_fee=0.74&address_country_code=US&address_name=Purple+Heart%27s+Test+Store&notify_version=3.8&custom=3-2-FRT&payer_status=verified&business=viktor**-facilitator%40yahoo.com&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=ARlhq6hLAVqXSD5s.6w.I67yISfcA-u87czB5qI9QnD2Y2gKnJ3b.tHb&payer_email=terleto%40yahoo.com&txn_id=3NX1120866247313A&payment_type=instant&payer_business_name=Purple+Heart%27s+Test+Store&last_name=Heart&address_state=CA&receiver_email=viktor**-facilitator%40yahoo.com&payment_fee=0.74&receiver_id=FWCDTECER3V24&txn_type=web_accept&item_name=Featured+Status+for+Umper+Dumpar&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=3-2-FRT&payment_gross=15.00&shipping=0.00&ipn_track_id=dfee171e44f44

这是我的IPN

        <?php
class Paypal_IPN
{
    /** @var string $_url The paypal url to go to through cURL
    private $_url;
    /**
    * @param string $mode 'live' or 'sandbox' 
    */
    public function __construct($mode = 'sandbox')
    {
        if ($mode == 'live')
            $this->_url = 'https://www.paypal.com/cgi-bin/webscr';
        else
            $this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    }
    public function run()
    {
        $postFields = 'cmd=_notify-validate&' . file_get_contents("php://input");
        $ch         = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $this->_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $postFields
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        /* $fh = fopen('result.txt', 'w');
        fwrite($fh, $result . ' -- ' . $postFields);
        fclose($fh); */
        function decodePayPalIPN($postFields)
        {
            if (empty($postFields)) {
                return array();
            }
            $post  = array();
            $pairs = explode('&', $postFields);
            foreach ($pairs as $pair) {
                list($key, $value) = explode('=', $pair, 2);
                $key   = urldecode($key);
                $value = urldecode($value);
                preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts);
                switch (count($key_parts)) {
                    case 4:
                        if (!isset($post[$key_parts[1]])) {
                            $post[$key_parts[1]] = array(
                                $key_parts[2] => array(
                                    $key_parts[3] => $value
                                )
                            );
                        } else if (!isset($post[$key_parts[1]][$key_parts[2]])) {
                            $post[$key_parts[1]][$key_parts[2]] = array(
                                $key_parts[3] => $value
                            );
                        } else {
                            $post[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value;
                        }
                        break;
                    case 3:
                        if (!isset($post[$key_parts[1]])) {
                            $post[$key_parts[1]] = array();
                        }
                        $post[$key_parts[1]][$key_parts[2]] = $value;
                        break;
                    default:
                        $post[$key] = $value;
                        break;
                }
            }
            return $post;
        }
        $raw_post_parsed = decodePayPalIPN($postFields); // Now we are decoding it
        /*echo "<pre>";
        print_r($raw_post_parsed);
        echo "</pre>";
        $raw_post_parsed_Text = var_export($raw_post_parsed, true);
        $fh = fopen('result2.txt', 'w');
        fwrite($fh, $result . ' -- ' . $raw_post_parsed_Text);
        fclose($fh); */
        if ($raw_post_parsed['payment_status'] === "Completed") {
            if ($raw_post_parsed['payment_gross'] === "15.00") {
                include(dirname(__FILE__) . "/../../includes/config.php");
                include(dirname(__FILE__) . "/../../includes/session.php");
                $verify_sign                 = $raw_post_parsed['verify_sign'];
                $payer_email                 = $raw_post_parsed['payer_email'];
                $custom                      = $raw_post_parsed['custom'];
                $custom_exploded             = explode("-", $custom); // Exploding the item into arrays
                $custom_exploded_sound_id    = $custom_exploded[0]; //sound type
                $custom_exploded_purchase_id = $custom_exploded[1]; //sound ID
                $sql                         = "INSERT INTO as_sound_featured (sound_id, purchaser_id, purchaser_email, verify_sign) VALUES ('$custom_exploded_sound_id', '$custom_exploded_purchase_id', '$payer_email', '$verify_sign')";
                if ($con->query($sql) === TRUE) {
                    $sql2                      = "UPDATE as_sound SET extra='1' WHERE id=$custom_exploded_sound_id";
                    $website_url               = "http://" . $_SERVER['SERVER_NAME'] . "/";
                    $comment_desc_notification = "Your sound is now featured";
                    $comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
                    $sql_not                   = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
                    mysqli_query($con, $sql_not);
                    $con->query($sql2);
                } else {
                    $website_url               = "http://" . $_SERVER['SERVER_NAME'] . "/";
                    $comment_desc_notification = "Featured status for your sound failed";
                    $comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
                    $sql_not                   = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
                    mysqli_query($con, $sql_not);
                }
            } else {
                $website_url               = "http://" . $_SERVER['SERVER_NAME'] . "/";
                $comment_desc_notification = "Featured status for your sound failed";
                $comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
                $sql_not                   = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
                mysqli_query($con, $sql_not);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

nvm,我已经阻止了所有不属于我的IP - 所以这就是问题所在。它已经解决了。