【问题标题】:paypal ipn won't return item_name and quantitypaypal ipn 不会返回 item_name 和数量
【发布时间】:2017-07-29 16:58:21
【问题描述】:

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

这是 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_namequantity。因此它返回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) {

}
?>

【问题讨论】:

  • 请发布您的IPN代码。
  • IPN 正在编辑中。
  • 您是否尝试过$item_name_1 = $_POST['item_name1'];,因为您的购物车中有多个商品?

标签: php paypal paypal-ipn


【解决方案1】:

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

所以你必须这样做:

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

// and so on

【讨论】:

  • hmm 我认为 paypal 会单独处理每次购买,无论它在购物车中,因为它与每个元素的 $_POST['mc_gross'] 配合得很好。但我会在我尝试后立即通知您。
  • 无论您的交易中有多少商品,PayPal 都会在每笔交易中发送一个 IPN。另外,mc_gross 是您购物车的总价,而不是您商品的价格/这可能对您有所帮助:developer.paypal.com/docs/classic/ipn/integration-guide/…
  • 该死的,我瞎了,该死的,先生,您是对的。谢谢你。因此,我如何处理 ipn 侦听器,以便它将 paypal 发送的价格与我的 myqsli 数据库中列出的价格进行比较?
  • 您使用$_POST['item_name1'] 获取您的mysql 项目,然后将其价格与$_POST['mc_gross_1'] 进行比较。
猜你喜欢
  • 1970-01-01
  • 2011-06-21
  • 2020-04-28
  • 2018-01-28
  • 2015-08-16
  • 1970-01-01
  • 2011-10-26
  • 2015-10-14
相关资源
最近更新 更多