doujiaohuo1096
doujiaohuo1096
2015-08-20 09:45

PayPal IPN响应无法插入数据库

已采纳

So recently I built a little IPN system based of others and it worked great. I could via sandbox purchase this item and it would send information via IPN to my listener and would insert in to a database. But now when I tried it yesterday, after no change on the files, it won't work. It does send me the IPN and I can store it in a .txt - but that is as much it lets me to do. If I then manually take the response and change the string to that value (from the .txt) it all works. I don't understand why this is and I've tried numerous ways to fix this.

This below is the response I get from paypal (same problem with my adaptive IPN, which worked before)

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

This is my 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条回答

  • doucan1996 doucan1996 6年前

    nvm, I had blocked all IPs that weren't mine - so that's the problem. It's solved.

    点赞 评论 复制链接分享

相关推荐