简体   繁体   English

Paypal购物车IPN处理程序

[英]Paypal IPN Handler for Cart

I'm using PayPal 'buy it now' buttons on my website to sell products. 我在网站上使用PayPal的“立即购买”按钮来销售产品。 Because I keep track of the number of units in stock for each product in a MySQL database and I'd like the inventory tracking in the system to be automated, I am using PayPal's Instant Payment Notification functionality to let me know when a purchase has been completed. 因为我跟踪MySQL数据库中每种产品的库存数量,并且希望系统中的库存跟踪是自动化的,所以我使用PayPal的即时付款通知功能来通知我何时购买完成。 When Paypal notifies my handler that a valid purchase has been made, the script updates my MySQL database by subtracting '1' from the inventory of the product purchased. 当贝宝(Paypal)通知我的经理已经进行了有效的购买时,该脚本通过从购买的产品清单中减去“ 1”来更新我的MySQL数据库。

I've attached my IPN PHP code below that works successfully with Paypal buy it now buttons (one purchase at a time). 我已在下面附加了我的IPN PHP代码,该代码可与Paypal的立即购买按钮(一次购买一次)一起成功使用。

My Question is: I would like to substitute 'buy it now' buttons with PayPal's 'add to cart' buttons so that customers can purchase more than one product at a time. 我的问题是:我想用PayPal的“添加到购物车”按钮代替“立即购买”按钮,以便客户一次可以购买多个产品。 I'm unsure how I have to alter my code below to let it loop through all items purchased and update my database accordingly. 我不确定如何更改下面的代码以使其遍历所有已购买的物品并相应地更新数据库。 Any help would be greatly appreciated! 任何帮助将不胜感激!

The Code: 编码:

    // Paypal POSTs HTML FORM variables to this page
// we must post all the variables back to paypal exactly unchanged and add an extra parameter cmd with value _notify-validate

// initialise a variable with the requried cmd parameter
$req = 'cmd=_notify-validate';

// go through each of the POSTed vars and add them to the variable
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

// In a live application send it back to www.paypal.com
// but during development you will want to uswe the paypal sandbox

// comment out one of the following lines

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
//$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

// or use port 443 for an SSL connection
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);


if (!$fp) {
// HTTP ERROR
}
else
{
  fputs ($fp, $header . $req);
  while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {


      $item_name = stripslashes($_POST['item_name']);
      $item_number = $_POST['item_number'];
      $item_id = $_POST['custom'];  
      $payment_status = $_POST['payment_status'];
      $payment_amount = $_POST['mc_gross'];         //full amount of payment. payment_gross in US
      $payment_currency = $_POST['mc_currency'];
      $txn_id = $_POST['txn_id'];                   //unique transaction id
      $receiver_email = $_POST['receiver_email'];
      $payer_email = $_POST['payer_email'];
      $size = $_POST['option_selection1'];
      $item_id  = $_POST['item_id'];
      $business = $_POST['business'];



      if ($payment_status == 'Completed')  {  
// UPDATE THE DATABASE      


      }

It would probably be easier for you to collect all item-ID's into an order-ID, then send that order-ID with your POST to PayPal. 您可能会更容易将所有项目ID收集到一个订单ID中,然后将该POST ID与您的POST一起发送到PayPal。 Once the IPN comes back with your details, check and calculate that the total sum of all items in the order-ID, matches the sum the IPN said was paid. IPN返回您的详细信息后,请检查并计算订单ID中所有项目的总和,与IPN所说的已支付的总和相匹配。

Query your database for the order-ID and get all the item-ID's connected to it, then decrease the stock number for each of the item-ID's. 在数据库中查询订单ID,并获取所有与该ID相关的商品ID,然后减少每个ID的库存数量。

Hope it helps. 希望能帮助到你。 It's just one way of doing it! 这只是一种实现方式!

It is not clear what you are asking. 目前尚不清楚您在问什么。 I wrote a PayPal IPN listener for Website Payment Pro. 我为Website Payment Pro编写了PayPal IPN侦听器。 I started by looking here PayPal Documentation: IPN Sample Code and then changing the code as I needed. 我首先在这里查看PayPal文档:IPN示例代码 ,然后根据需要更改代码。 Please add some more details to your question. 请为您的问题添加更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM