[英]PayPal IPN - having trouble accessing session data?
all. 所有。 I'm having issues with PayPal IPN integration where it seems I cannot get my solution to read session variables.
我在使用PayPal IPN集成时遇到问题,似乎无法获得读取会话变量的解决方案。
Basically, in my shop module script, I store the customer's details as provided by PayPal to an orders table. 基本上,在我的商店模块脚本中,我将PayPal提供的客户详细信息存储到订单表中。 However, I also wish to save products ordered in a transaction to a separate table linked by the order ID.
但是,我也希望将交易中订购的产品保存到由订单ID链接的单独表格中。
However, it's the second part of the script that's not working, where I loop through the products in the session and then save them to the orders_products table. 但是,这是脚本的第二部分不起作用,我在其中循环浏览会话中的产品,然后将其保存到orders_products表中。
Is there a reason why the session data not being read? 是否有原因无法读取会话数据?
The code within shop.php is as follows: 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');
}
And I'm using the PayPal IPN class for PHP as found here: http://www.micahcarrick.com/04-19-2005/php-paypal-ipn-integration-class.html , but the contents of the validate_ipn()
method is as follows: 而且我正在使用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;
}
}
If I recall correctly, PayPal IPN sends a notification to your script directly. 如果我没记错的话,贝宝IPN会直接向您的脚本发送通知。 Since the notification is coming from PayPal - NOT the customer that placed the order - their session doesn't exist in this context.
由于通知来自PayPal,而不是发出订单的客户,因此在这种情况下,他们的会话不存在。 Therefore, all their cart data doesn't exist in the session.
因此,会话中不存在其所有购物车数据。
When I've used IPN in the past, I stored everything about their order in the database and generated a TransactionID. 过去使用IPN时,我将有关其订单的所有信息存储在数据库中并生成了TransactionID。 This is one of the pass-through variables that I can send off with the rest of the order to PayPal and they will pass it back.
这是我可以随订单的其余部分发送给PayPal的传递变量之一,它们会将其传递回去。 Once I received the IPN from PayPal I re-hydrated their order based on the TransactionID and proceeded with whatever business rules I had to follow - send an email, create passwords, etc.
从贝宝(PayPal)收到IPN后,我会根据TransactionID重新为他们的订单补水,并按照我必须遵循的任何业务规则进行处理-发送电子邮件,创建密码等。
I am currently working on this -- say you are trying to get the session user_id in insert into your transaction tables, it may be best to sent the user_id to Paypal along with the order. 我目前正在为此工作-假设您正在尝试将会话user_id插入交易表中,最好将user_id与订单一起发送给Paypal。 Because if you write to the DB as soon as they are redirected to Paypal you have no guarantee they actually completed the order.
因为如果您将数据库重定向到Paypal后立即写入数据库,则不能保证他们实际完成了订单。 In addition, I think PayPal send back the product/order information in a string delimited by a comma, so you would want to look through the count of however many unique items you sold, and dynamically get item_id1, item_id2, item_id3 ...so your query is always changing.
此外,我认为PayPal会以逗号分隔的字符串形式发送产品/订单信息,因此您需要查看出售的许多独特商品的计数,并动态获取item_id1,item_id2,item_id3 ...如此您的查询总是在变化。 Correct me if I'm wrong, I'm about to start implementing this but without using classes.
如果我错了,请纠正我,我将开始执行此操作,但不使用类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.