如何将多行 paypal ipn 变量插入 mysql 数据库

How to insert multiple rows of paypal ipn variables to mysql database

我正在创建一个带有贝宝结账功能的购物车。我希望 ipn 在交易后将行插入 mysql。

现在,它只是这样做,但即使购物车中有超过 3 件不同的商品,它也只与一行相交。 我想我需要一个 foreach 循环,以便购物车中的每件商品在交易后都有自己的数据库行。

我试过了,但无法正常工作。 这是我记录到文件的结果示例

 for IPN payload: cmd=_notify-validate&mc_gross=7.00&protection_eligibility=Eligible&address_status=confirmed&item_number1=&tax=0.00&item_number2=&payer_id=SLDWABVPR4W3N&address_street=1+Main+St&payment_date=01%3A56%3A35+Jul+30%2C+2015+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&mc_shipping=0.00&mc_handling=0.00&first_name=test&mc_fee=0.50&address_country_code=US&address_name=test+buyer&notify_version=3.8&custom=5-1%2C3-1%2C&payer_status=verified&business=ericel123-facilitator%40gmail.com&address_country=United+States&num_cart_items=2&mc_handling1=0.00&mc_handling2=0.00&address_city=San+Jose&verify_sign=Agvo6..302dNn6xS6bIzJdaQQfJ7AnGucvHS6CgM0lgL0w5Q5Oz8VCI-&payer_email=ericel123-buyer%40gmail.com&mc_shipping1=0.00&mc_shipping2=0.00&tax1=0.00&tax2=0.00&txn_id=8YJ42821LM735725U&payment_type=instant&last_name=buyer&address_state=CA&item_name1=white+cup&receiver_email=ericel123-facilitator%40gmail.com&item_name2=Men+handy+bags&payment_fee=0.50&quantity1=1&quantity2=1&receiver_id=KET4YMZ2YCVY8&txn_type=cart&mc_gross_1=2.00&mc_currency=USD&mc_gross_2=5.00&residence_country=US&test_ipn=1&transaction_subject=5-1%2C3-1%2C&payment_gross=7.00&ipn_track_id=8b6b5bb9131 

以上来自这个 foreach 循环。

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

如何为我需要的选定变量创建一个数组,并能够基于 item_name 或 item_number.

创建或插入记录

对于大多数 IPN 数据,您只需使用 $_POST['varname'] 来提取值。对于订单商品,您可以使用以下命令将所有商品解析为 $cart_items.

$i = 1;
$cart_items = array();   
while(isset($_POST['item_number' . $i]))   
{   
    $item_number = isset($_POST['item_number' . $i]) ? $_POST['item_number' . $i] : '';   
    $item_name = isset($_POST['item_name' . $i]) ? $_POST['item_name' . $i] : '';   
    $quantity = isset($_POST['quantity' . $i]) ? $_POST['quantity' . $i] : '';  
    $mc_gross = isset($_POST['mc_gross_' . $i]) ? $_POST['mc_gross_' . $i] : 0;
    $mc_handling = isset($_POST['mc_handling' . $i]) ? $_POST['mc_handling' . $i] : 0;
    $mc_shipping = isset($_POST['mc_shipping' . $i]) ? $_POST['mc_shipping' . $i] : 0;
    $custom = isset($_POST['custom' . $i]) ? $_POST['custom' . $i] : '';   
    $option_name1 = isset($_POST['option_name1_' . $i]) ? $_POST['option_name1_' . $i] : '';   
    $option_selection1 = isset($_POST['option_selection1_' . $i]) ? $_POST['option_selection1_' . $i] : '';   
    $option_name2 = isset($_POST['option_name2_' . $i]) ? $_POST['option_name2_' . $i] : '';   
    $option_selection2 = isset($_POST['option_selection2_' . $i]) ? $_POST['option_selection2_' . $i] : '';
    $option_name3 = isset($_POST['option_name3_' . $i]) ? $_POST['option_name3_' . $i] : '';   
    $option_selection3 = isset($_POST['option_selection3_' . $i]) ? $_POST['option_selection3_' . $i] : '';
    $option_name4 = isset($_POST['option_name4_' . $i]) ? $_POST['option_name4_' . $i] : '';   
    $option_selection4 = isset($_POST['option_selection4_' . $i]) ? $_POST['option_selection4_' . $i] : '';
    $option_name5 = isset($_POST['option_name5_' . $i]) ? $_POST['option_name5_' . $i] : '';   
    $option_selection5 = isset($_POST['option_selection5_' . $i]) ? $_POST['option_selection5_' . $i] : '';
    $option_name6 = isset($_POST['option_name6_' . $i]) ? $_POST['option_name6_' . $i] : '';   
    $option_selection6 = isset($_POST['option_selection6_' . $i]) ? $_POST['option_selection6_' . $i] : '';
    $option_name7 = isset($_POST['option_name7_' . $i]) ? $_POST['option_name7_' . $i] : '';   
    $option_selection7 = isset($_POST['option_selection7_' . $i]) ? $_POST['option_selection7_' . $i] : '';
    $option_name8 = isset($_POST['option_name8_' . $i]) ? $_POST['option_name8_' . $i] : '';   
    $option_selection8 = isset($_POST['option_selection8_' . $i]) ? $_POST['option_selection8_' . $i] : '';
    $option_name9 = isset($_POST['option_name9_' . $i]) ? $_POST['option_name9_' . $i] : '';   
    $option_selection9 = isset($_POST['option_selection9_' . $i]) ? $_POST['option_selection9_' . $i] : '';

    $btn_id = isset($_POST['btn_id' . $i]) ? $_POST['btn_id' . $i] : '';

    $current_item = array(   
                           'item_number' => $item_number,   
                           'item_name' => $item_name,   
                           'quantity' => $quantity, 
                           'mc_gross' => $mc_gross, 
                           'mc_handling' => $mc_handling, 
                           'mc_shipping' => $mc_shipping, 
                           'custom' => $custom,   
                           'option_name1' => $option_name1,   
                           'option_selection1' => $option_selection1,   
                           'option_name2' => $option_name2,   
                           'option_selection2' => $option_selection2, 
                           'option_name3' => $option_name3, 
                           'option_selection3' => $option_selection3, 
                           'option_name4' => $option_name4, 
                           'option_selection4' => $option_selection4, 
                           'option_name5' => $option_name5, 
                           'option_selection5' => $option_selection5, 
                           'option_name6' => $option_name6, 
                           'option_selection6' => $option_selection6, 
                           'option_name7' => $option_name7, 
                           'option_selection7' => $option_selection7, 
                           'option_name8' => $option_name8, 
                           'option_selection8' => $option_selection8, 
                           'option_name9' => $option_name9, 
                           'option_selection9' => $option_selection9, 
                           'btn_id' => $btn_id
                          );   

    array_push($cart_items, $current_item);   
    $i++;   
}

然后您可以遍历 $cart_items 以相应地处理每个单独的项目。这是您可以执行的操作的示例。

foreach($cart_items as $item)
{
    $db->prepare("INSERT INTO s_itms_trans(item_number, item_name, mc_gross) VALUES('".$item['item_number']."', '".$item['item_name']."', ".$item['mc_gross'].")")->execute();
}