代码之家  ›  专栏  ›  技术社区  ›  Martin Bean

PayPal IPN——访问会话数据有问题吗?

  •  0
  • Martin Bean  · 技术社区  · 14 年前

    所有。我在PayPal IPN集成方面遇到了一些问题,我似乎无法获得解决会话变量的方法。

    基本上,在我的商店模块脚本中,我将客户的详细信息存储在Paypal提供的订单表中。但是,我也希望将事务中订购的产品保存到由订单id链接的单独表中。

    但是,脚本的第二部分不起作用,我在会话中循环遍历产品,然后将它们保存到orders_products表中。

    没有读取会话数据的原因吗?

    shop.php中的代码如下:

    if ($paypal->validate_ipn()) {
        $name = $paypal->ipn_data['address_name'];
        $street_1 = $paypal->ipn_data['address_street'];
        $street_2 = "";
        $city = $paypal->ipn_data['address_city'];
        $state = $paypal->ipn_data['address_state'];
        $zip = $paypal->ipn_data['address_zip'];
        $country = $paypal->ipn_data['address_country'];
        $txn_id = $paypal->ipn_data['txn_id'];
        $sql = "INSERT INTO orders (name, street_1, street_2, city, state, zip, country, txn_id)
                VALUES (:name, :street_1, :street_2, :city, :state, :zip, :country, :txn_id)";
        $smt = $this->pdo->prepare($sql);
        $smt->bindParam(':name', $name, PDO::PARAM_STR);
        $smt->bindParam(':street_1', $street_1, PDO::PARAM_STR);
        $smt->bindParam(':street_2', $street_2, PDO::PARAM_STR);
        $smt->bindParam(':city', $city, PDO::PARAM_STR);
        $smt->bindParam(':state', $state, PDO::PARAM_STR);
        $smt->bindParam(':zip', $zip, PDO::PARAM_STR);
        $smt->bindParam(':country', $country, PDO::PARAM_STR);
        $smt->bindParam(':txn_id', $txn_id, PDO::PARAM_INT);
        $smt->execute();
        // save products to orders relationship
        $order_id = $this->pdo->lastInsertId();
        // $cart = $this->session->get('cart');
        $cart = $this->session->get('cart');
        foreach ($cart as $product_id => $item) {
            $quantity = $item['quantity'];
            $sql = "INSERT INTO orders_products (order_id, product_id, quantity) VALUES ('$order_id', '$product_id', '$quantity')";
            $res = $this->pdo->query($sql);
        }
        $this->session->del('cart');
        mail('martin@mcbwebdesign.co.uk', 'IPN result', 'IPN was successful on wrestling-wear.com');
    } else {
        mail('martin@mcbwebdesign.co.uk', 'IPN result', 'IPN failed on wrestling-wear.com');
    }
    

    我使用PayPal IPN类作为PHP在这里找到: http://www.micahcarrick.com/04-19-2005/php-paypal-ipn-integration-class.html ,但是 validate_ipn() 方法如下:

    public function validate_ipn()
    {
        $url_parsed = parse_url($this->paypal_url);
        $post_string = '';
        foreach ($_POST as $field => $value) {
            $this->ipn_data[$field] = $value;
            $post_string.= $field.'='.urlencode(stripslashes($value)).'&';
        }
        $post_string.= "cmd=_notify-validate"; // append IPN command
        // open the connection to PayPal
        $fp = fsockopen($url_parsed[host], "80", $err_num, $err_str, 30);
        if (!$fp) {
            // could not open the connection. If logging is on, the error message will be in the log
            $this->last_error = "fsockopen error no. $errnum: $errstr";
            $this->log_ipn_results(false);
            return false;
        } else {
            // post the data back to PayPal
            fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
            fputs($fp, "Host: $url_parsed[host]\r\n");
            fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $post_string . "\r\n\r\n");
            // loop through the response from the server and append to variable
            while (!feof($fp)) {
                $this->ipn_response.= fgets($fp, 1024);
            }
            fclose($fp); // close connection
        }
        if (eregi("VERIFIED", $this->ipn_response)) {
            // valid IPN transaction
            $this->log_ipn_results(true);
            return true;
        } else {
            // invalid IPN transaction; check the log for details
            $this->last_error = 'IPN Validation Failed.';
            $this->log_ipn_results(false);
            return false;
        }
    }
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   BradBrening    14 年前

    如果我记得正确,PayPal IPN直接向您的脚本发送通知。由于通知来自PayPal,而不是放置订单的客户——他们的会话在此上下文中不存在。因此,他们所有的购物车数据都不存在于会话中。

    当我在过去使用ipn时,我将关于订单的所有信息存储在数据库中,并生成一个transactionid。这是一个传递变量,我可以把其余的订单发送给PayPal,他们会把它传回来。一旦我从PayPal收到了IPN,我就根据TraseNoID重新整理他们的订单,然后按照我必须遵循的任何业务规则进行发送电子邮件、创建密码等。

        2
  •  0
  •   chsnmyrhoades    12 年前

    我目前正在研究这一点,比如说,您试图将会话USELID插入到您的事务表中,最好将USER ID发送到PayPal以及订单。因为如果你一到DayPal就给DB写信,你就不能保证他们真的完成了订单。此外,我认为PayPal在一个由逗号分隔的字符串中发送产品/订单信息,因此您希望查看您所销售的许多独特项目的数量,并动态地获取ITEMIDID1、ITEMIDID2、ITEMIDID3……因此您的查询总是在变化。如果我错了,请纠正我,我将开始实现它,但不使用类。