paypal ipn不会返回item_name和数量

时间:2017-07-29 16:58:21

标签: php paypal paypal-ipn

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

以下是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然后更新数据库。虽然它不会返回item_name也不会返回quantity。因此,它返回payment_statusmc_grossmc_currencytxn_idpayer_emailpayment_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) {

}
?>

1 个答案:

答案 0 :(得分:0)

由于购物车中有多件商品,因此您会在IPN中收到多件商品。

所以你必须这样做:

x