2

I am trying to set up a dynamic PayPal form for multiple items. However, whenever I try to dynamically set the item_name, item_number, amount and quantity for each item, the cart on the PayPal website breaks, and I cannot display any items:

In my foreach loop, I want to add the ID variable (rendered with PHP) to each hidden input field:

$paypal_form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="merchant@XXXXXX.com">
<input type="hidden" name="return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="cancel_return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="notify_url" value="http://XXXXXX.com/confirmation.php">';

foreach ($_SESSION["shopping_cart"] as $each_item) { 

$item_id = $each_item['item_id'];
$item_name = $each_item['item_name'];
$product_id = $each_item['product_id'];
$price = $each_item['price'];

$paypal_form .='<input type="hidden" name="item_name_'.$item_id.'" value="'.$item_name.'">
<input type="hidden" name="item_number_'.$item_id.'" value="'.$product_id.'">
<input type="hidden" name="amount_'.$item_id.'" value="'.$price.'">
<input type="hidden" name="quantity_'.$item_id.'" value="1">';

} // end foreach

$paypal_form .= '<input type="hidden" name="currency_code" value="EUR">
<input type="submit" value="Pay with PayPal" class="pay_button"/>
</form>';

It seems that PayPal does not accept the dynamic $item_id amendment to the hidden input names?

When I leave the input names untouched (like below), it works, but then I cannot dynamically render the input fields for multiple items:

<input type="hidden" name="item_name_1" value="'.$item_name.'">
<input type="hidden" name="item_number_1" value="'.$product_id.'">
<input type="hidden" name="amount_1" value="'.$price.'">
<input type="hidden" name="quantity_1" value="1">
rainerbrunotte
  • 907
  • 1
  • 17
  • 37

2 Answers2

4

I managed to get it working. It seems that PayPal does not accept dynamically generated "random" numbers for the input field names, they only work if they are in proper order (item_name_1, item_name_2, item_name_3, ...), eg. an item name of item_name_204 for the first item does not work.

By adding the loop index, the following code made it work for me:

$paypal_form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="merchant@XXXXXX.com">
<input type="hidden" name="return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="cancel_return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="notify_url" value="http://XXXXXX.com/confirmation.php">';

$i = 0; 
foreach ($_SESSION["shopping_cart"] as $each_item) { 
$i++;
$item_id = $each_item['item_id'];
$item_name = $each_item['item_name'];
$product_id = $each_item['product_id'];
$price = $each_item['price'];

$paypal_form .='<input type="hidden" name="item_name_'.$i.'" value="'.$item_name.'">
<input type="hidden" name="item_number_'.$i.'" value="'.$product_id.'">
<input type="hidden" name="amount_'.$i.'" value="'.$price.'">
<input type="hidden" name="quantity_'.$i.'" value="1">';

} // end foreach

$paypal_form .= '<input type="hidden" name="currency_code" value="EUR">
<input type="submit" value="Pay with PayPal" class="pay_button"/>
</form>';
rainerbrunotte
  • 907
  • 1
  • 17
  • 37
0

I don't know will help or not. Try to add next field:

<input type="hidden" name="tax_cart" value="0"> 
Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
  • Or try to use sandbox mode and simple form with 2 items. May be in foreach exist empty/bad value. Example form here: http://stackoverflow.com/questions/3308898/paying-for-multiple-items-at-once-via-paypal or http://stackoverflow.com/questions/1761803/submitting-multiple-items-to-paypal-cart-at-once-with-php – Danila Ganchar Jun 01 '15 at 10:42
  • Compare your form after foreach with form which working. I think problem in fields names/values. – Danila Ganchar Jun 01 '15 at 10:57
  • Managed to get it working. See my posted solution above. Thanks for your effort however! – rainerbrunotte Jun 01 '15 at 11:12