全部。我遇到了PayPal IPN集成問題,看起來我無法通過我的解決方案來讀取會話變量。PayPal IPN - 無法訪問會話數據?
基本上,在我的店鋪模塊腳本中,我將貝寶提供的客戶詳細信息存儲到訂單表中。不過,我也希望將訂單中訂購的產品保存到由訂單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('[email protected]', 'IPN result', 'IPN was successful on wrestling-wear.com');
} else {
mail('[email protected]', 'IPN result', 'IPN failed on wrestling-wear.com');
}
而且我使用PHP的貝寶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;
}
}
所以基本上,會話存儲在數據庫而不是一個tmp文件?乾杯。 – 2010-05-02 22:54:22
@Martin我想你可以在任何你喜歡的地方離開會話,但你需要將交易數據複製到永久的地方,以便將它與PayPal的響應進行匹配。如果session_unset()被意外調用(丟失cookie,或許?),事務不一定會消失。 – cbednarski 2010-05-03 02:27:54