1

我将 ipn 脚本改编为“立即购买”按钮,之后我添加了“添加到购物车”按钮。

This is the buy now button:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="return" value="http://www.company.develway.com/company_user?id=    <?php echo $id; ?>">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="custom" value="<?php echo $id; ?>"/>
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="item_name" value="<?php echo 'Premium Content for ' . $name; ?>">
<input type="hidden" name="hosted_button_id" value="TV4SWL86EHYYQ">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>


This is add to cart button:
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" alt="Make payments with PayPal - it'sfast, free and secure!">
<input type="hidden" name="add" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="finciucsergiu@gmail.com">
<input type="hidden" name="custom" value="<?php echo $id; ?>"/>
<input type="hidden" name="item_name" value="<?php echo 'Premium Content for ' . $name; ?>">
<input name="notify_url" value="http://www.company.develway.com/ipn.php" type="hidden">
<input type="hidden" name="item_number" value="Wid-001">
<input type="hidden" name="amount" value="0.01">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input name="notify_url" value="http://www.company.develway.com/ipn.php" type="hidden">
</form>

这是 ipn 文件的主要部分:

 if (strcmp ($res, "VERIFIED") == 0) {

$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$custom = $_POST['custom'];
$txn_type = $_POST['txn_type'];
$number_of_items = $_POST['num_cart_items'];

$connection = mysql_connect("hhh", "fff", "lll");
$db_select = mysql_select_db("company", $connection);

mysql_query("INSERT INTO paid_companies(users_email, companies_id)
 VALUES('$payer_email', '$custom') ") or die(mysql_error());

}

AL 适用于立即购买和添加到购物车(如果它是一件商品),但如果我在数据库中插入多个商品,则仅包含最后一件商品。我做错了什么?

4

1 回答 1

1

当您在购物车中有多个商品时,您必须遍历 IPN 中包含的所有商品以获取数据库中的所有商品。

看一下这个...

// Cart Items
$num_cart_items = isset($_POST['num_cart_items']) ? $_POST['num_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 的数组,其中包含您的所有项目。您可以将数据库插入添加到我提供的循环中,或者您可以使用它将购物车项目收集到 $cart_items 中,然后使用它来工作。

无论哪种方式,您都需要遍历这些项目。

于 2013-02-17T02:15:26.643 回答