0

全て。セッション変数を読み取るためのソリューションを取得できないように見える PayPal IPN 統合に問題があります。

基本的に、私のショップ モジュール スクリプトでは、PayPal から提供された顧客の詳細を注文テーブルに保存します。ただし、トランザクションで注文した商品を、注文 ID でリンクされた別のテーブルに保存したいと考えています。

ただし、機能していないのはスクリプトの 2 番目の部分です。ここでは、セッション内の製品をループ処理して、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');
}

そして、ここにあるようにPHP用のPayPal IPNクラスを使用しています: 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;
    }
}
4

2 に答える 2

1

正しく思い出せば、PayPalIPNはスクリプトに直接通知を送信します。通知はPayPal(注文した顧客ではなく)から送信されるため、このコンテキストではセッションは存在しません。したがって、すべてのカートデータがセッションに存在するわけではありません。

過去にIPNを使用したことがある場合、それらの順序に関するすべてをデータベースに保存し、TransactionIDを生成しました。これは、残りの注文とともにPayPalに送信できるパススルー変数の1つであり、PayPalはそれを返します。PayPalからIPNを受け取ったら、TransactionIDに基づいて注文を再水和し、従わなければならないビジネスルール(メールの送信、パスワードの作成など)を続行しました。

于 2010-05-02T22:48:18.583 に答える
0

私は現在これに取り組んでいます -- セッションの user_id をトランザクション テーブルに挿入しようとしているとします。注文と一緒に user_id を Paypal に送信するのが最善かもしれません。PaypalにリダイレクトされたらすぐにDBに書き込むと、実際に注文を完了したという保証がないからです. さらに、PayPal は商品/注文情報をコンマで区切った文字列で送り返すと思います。そのため、販売した一意の商品の数を調べて、item_id1、item_id2、item_id3 を動的に取得する必要があります。クエリは常に変化しています。私が間違っている場合は修正してください。これを実装しようとしていますが、クラスを使用していません。

于 2012-03-01T17:36:07.220 に答える