php - Paypal IPN : Reading multiple item_name and quantities and saving to a database

标签 php paypal

下面是我的 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."')"); 
        } 

    } 

最佳答案

看起来您在 //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/

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

关于php - Paypal IPN : Reading multiple item_name and quantities and saving to a database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692984/

相关文章:

php - 如何使用 Linkedin API 获取 Linkedin 推荐?

php - 带回调的 JSONP 和 GET - 需要帮助纠正错误

javascript - 使用 AngularJS 的 Paypal Express Checkout

ruby-on-rails-3 - ActionController::TestCase 模拟 PayPal 购买

api - PayPal 自适应支付 - 错误 520009 - 账户受限

javascript - 如何在不输入的情况下发送用户名和密码?

php - Laravel5 Eloquent 从带有 where 子句的多个表中选择

php - 这段更新 Paypal 按钮的代码有什么问题?我得到 "buttontype invalid error"

php - 插入两个表(同一个数据库)如何从一个表中获取唯一 ID?

php - PHP 支付错误代码 13113 中的 Paypal API Express Checkout