2012-01-03 161 views
2

这里是从PayPal的一个样本响应后,我使用沙盒如何获取paypal IPN中的item_number?

Array 
(
    [address_city] => San Jose 
    [address_country] => United States 
    [address_country_code] => US 
    [address_name] => Test User 
    [address_state] => CA 
    [address_status] => confirmed 
    [address_street] => 1 Main St 
    [address_zip] => 95131 
    [business] => [email protected] 
    [charset] => windows-1252 
    [custom] => 
    [first_name] => Test 
    [form_charset] => UTF-8 
    [item_name1] => iPod Touch 
    [item_name2] => Giant Hamburger, the world's biggest 
    [item_number1] => 2 
    [item_number2] => 1 
    [last_name] => User 
    [mc_currency] => USD 
    [mc_fee] => 0.79 
    [mc_gross] => 17.00 
    [mc_gross_1] => 7.00 
    [mc_gross_2] => 10.00 
    [mc_handling] => 0.00 
    [mc_handling1] => 0.00 
    [mc_handling2] => 0.00 
    [mc_shipping] => 2.00 
    [mc_shipping1] => 2.00 
    [mc_shipping2] => 0.00 
    [merchant_return_link] => click here 
    [notify_version] => 3.4 
    [num_cart_items] => 2 
    [payer_email] => [email protected] 
    [payer_id] => TRCLJTHLNCJ7Q 
    [payer_status] => verified 
    [payment_date] => 23:14:45 Jan 02, 2012 PST 
    [payment_fee] => 0.79 
    [payment_gross] => 17.00 
    [payment_status] => Completed 
    [payment_type] => instant 
    [protection_eligibility] => Eligible 
    [quantity1] => 1 
    [quantity2] => 1 
    [receiver_email] => [email protected] 
    [receiver_id] => 74PV23B8KSK84 
    [residence_country] => US 
    [tax] => 0.00 
    [tax1] => 0.00 
    [tax2] => 0.00 
    [test_ipn] => 1 
    [transaction_subject] => Shopping CartiPod TouchGiant Hamburger, the world's biggest 
    [txn_id] => 4BK63214AG342352Y 
    [txn_type] => cart 
    [verify_sign] => A0Jk1FY.7aHd-VRk44a4grBP7kE.A0Azn34Obs6QiSJKuCvAWSWyItkH 
) 

你可以看到,有item_name1和item_name2怎么去完成支付车的这些物品的价值,让我们说,例如,我有付10件物品?这将是像item_name1到item_name10

我生成项目的编号是通过我的PHP代码,这样

<?php 
    $i = 1; 
    foreach($_SESSION['cart'] as $id => $qty): 
    $product = find_product($id); 
?> 

<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product['ProductDescription']; ?>"> 
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product['ProductID']; ?>"> 
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product['ProductOverridePrice']; ?>"> 
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $qty; ?>"> 

<?php 
    $i++; 
    endforeach; 
?> 

,如果你看一下样品贝宝的响应,有mc_gross_1,mc_gross_2 ... tax1,tax2之类的..所以我怎样才能得到那些增加数字的值并将其保存在数据库中?

回答

5

不过你也有[num_cart_items] = 2。 所以只是做一个循环...

for ($i = 1; $i <= $_POST['num_cart_items']; $i++) { 
    $name = $_POST['item_name' . $i]; 
    $number = $_POST['item_number' . $i]; 
    $quantity = $_POST['quantity' . $i]; 
    //etc... 

    $query = "INSERT INTO payments (product_name, product_id, product_quantity) 
      VALUES ('" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($number) . "', '" . mysql_real_escape_string($quantity) . "')"; 
    mysql_query($query); 
} 
+1

一个小小的警告:在'num_cart_items'参数不是由PayPal的IPN模拟器提供。只是想我会提到,以防有人使用它进行测试。 – 2013-08-23 20:27:47

+0

如何以PayPal按钮形式发送多个项目 – Karthik 2013-09-28 06:43:20

+0

您可以使用'.'连接PHP。 – Aborted 2013-11-21 01:00:01