paypal ipn不会returnitem_name和数量

paypal ipn won't return item_name and quantity

我正在尝试使用我的自定义购物车进行在线购物。

这是 HTML 中的代码:

<form name="paypalSubmit" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="value">
    <input type="hidden" name="currency_code" value="EUR">
    <input type="hidden" name="cancel_return" value="link">
    <input type="hidden" name="return" value="link">

                <input type="hidden" name="item_name_1" value='Stiklines is ledo'>
            <input type="hidden" name="amount_1" value=0.56>
            <input type="hidden" name="quantity1" value=3>

                       <input type="hidden" name="item_name_2" value='RGB lempute su pultu'>
            <input type="hidden" name="amount_2" value=0.68>
            <input type="hidden" name="quantity2" value=1>


    <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

一切正常。付款完成后,ipn 命中我的 ipn_listener,然后更新数据库。虽然它不会 return item_name 也不会 quantity。因此它 returns payment_status, mc_gross, mc_currency, txn_id, payer_email, payment_date.

任何人都可以帮我弄清楚我错过了什么?非常感谢。

编辑: ipn_listener.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
// STEP 1: Read POST data

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}


// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_quantity = $_POST['quantity'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $payer_email = $_POST['payer_email'];
    $payment_date = $_POST['payment_date'];

    // <---- HERE you can do your INSERT to the database    
    $db_handle->insert("INSERT INTO buy_history(payment_method, item_name, item_quantity, payment_status, payment_amount, payment_currency, txn_id, payer_email, payment_date) VALUES ('paypal', '" . $item_name . "', " . $item_quantity . ", '" . $payment_status . "', '" . $payment_amount . "', '" . $payment_currency . "', '" . $txn_id . "', '" . $payer_email . "', '" . $payment_date . "')");


} else if (strcmp ($res, "INVALID") == 0) {

}
?>

由于您的购物车中有多个项目,因此您在 IPN 中收到多个项目。

所以你必须这样做:

$item_name1 = $_POST['item_name1'];
$item_quantity1 = $_POST['quantity1'];

// and so on