【问题标题】:PayPal IPN: Reading multiple item_name and quantities and saving to a databasePayPal IPN:读取多个 item_name 和数量并保存到数据库
【发布时间】:2020-04-28 18:30:26
【问题描述】:

下面是我的 IPN 代码。我的问题是我无法在表payments_paypal 中添加多个项目名称或数量。事实上 IPN 没有监听 payer_id,quantity,item_name(multiple items)。

我们如何在这里 $_POST item_name1 或 item_name2 等,以便在验证时我可以在其他页面中获取它,这将是成功页面。虽然我已经通过其他方法实现了这一点,但这不是正确的方法。所以我想知道为什么这些变量不是以字符串的形式来自 ipn,只有这些变量以字符串 txn、smt、st、currency 的形式出现,其余的都丢失了。如果我们必须在这里使用循环,我们如何发送值以从这里 ipn.php 插入循环。

任何帮助将不胜感激。

    $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]); 

    }

    $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"; 
    } 

    /* 
     * Post IPN data back to PayPal to validate the IPN data is genuine 
     * Without this step anyone can fake IPN data 
     */ 
    $paypalURL = PAYPAL_URL; 
    $ch = curl_init($paypalURL); 
    if ($ch == FALSE) { 
        return FALSE; 
    } 
    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_SSLVERSION, 6); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 

    // Set TCP timeout to 30 seconds 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: company-name')); 
    $res = curl_exec($ch); 

    /* 
     * Inspect IPN validation result and act accordingly 
     * Split response headers and payload, a better way for strcmp 
     */  
    $tokens = explode("\r\n\r\n", trim($res)); 
    $res = trim(end($tokens)); 
    if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) { 

        // Retrieve transaction info from PayPal 
        $item_number    = $_POST['item_number']; 
        $txn_id         = $_POST['txn_id']; 
        $payment_gross     = $_POST['mc_gross']; 
        $currency_code     = $_POST['mc_currency']; 
        $payment_status = $_POST['payment_status']; 
         $quantity   = $_POST['quantity']; 

        // Check if transaction data exists with the same TXN ID 
        $prevPayment = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'"); 
        if($prevPayment->num_rows > 0){ 
            exit(); 
        }else{ 
            // Insert transaction data into the database 
            $insert = $db->query("INSERT INTO payments_paypal(item_number,txn_id,payment_gross,currency_code,payment_status) VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."')"); 
        } 

    } 

【问题讨论】:

    标签: php paypal


    【解决方案1】:

    看来您在// Retrieve transaction info from PayPal 部分中使用的代码非常简单,并不是为了查找多个项目而编写的,例如编号变量,如item_name1item_name2 等。

    您应该记录您正在接收但未正确处理的 IPN 的文本,或从您的 PayPal 帐户历史记录中获取它们的文本:https://www.paypal.com/us/smarthelp/article/where-can-i-access-my-ipn-history-faq3692

    此外,请查看多个项目的变量名称文档:https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/

    然后,您可以修改代码以读取和处理这些多个编号的变量,并将它们存储在您想要的任何数据库架构中,这可能需要额外的列或其他任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2018-01-28
      • 2012-05-14
      • 1970-01-01
      • 2015-10-21
      • 2015-01-31
      • 2014-05-18
      相关资源
      最近更新 更多