Jump to content

I want to pass multiple items from my shopping cart to paypal


countrydj

Recommended Posts

Hi Guys...

I'm stuck again with my shopping cart.

I have everything working now, except for payments via Paypal.

 

I have a Paypal link at the bottom of my cart.php which goes to paypal.php

Here is paypal.php:

                       foreach ($contents as $id=>$qty) {  
            $sql = 'SELECT * FROM products WHERE id = '.$id;  
            $result = $db->query($sql);  
            $row = $result->fetch();  
            extract($row);  

$trade = number_format($trade, 2);
$subtotal = $trade*$qty;
$subtotal = number_format($subtotal, 2);

//echo "<br><br>Quantity = ".$qty."<br>Product = ".$product."<br> Trade = ".$trade."<br><br>";

		$output[] = '<form id="paypal_form" class="paypal" action="../../../paypal/payments.php" method="post">';
		$output[] = '<input name="cmd" type="hidden" value="_cart" />';
		$output[] = '<input type="hidden" name="upload" value="1">';
		$output[] = '<input name="no_note" type="hidden" value="1" />';
		$output[] = '<input name="lc" type="hidden" value="UK" />';
		$output[] = '<input name="currency_code" type="hidden" value="GBP" />';
//			$output[] = '<input name="bn" type="hidden" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />';
		$output[] = '<input name="first_name" type="hidden" value="First" />';
		$output[] = '<input name="last_name" type="hidden" value="Second" />';
		$output[] = '<input name="payer_email" type="hidden" value="an@email.co.uk" />';
//########################### products ################################
		$output[] = '<input type="hidden" name="item_number_1" value="1">';
		$output[] = '<input type="hidden" name="item_name_1" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_1" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_2" value="2">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_3" value="3">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_4" value="4">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_3" value="5">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_4" value="6">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_3" value="7">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

		$output[] = '<input type="hidden" name="item_number_4" value="8">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';
}


			$output[] = '<div align=center><button type="submit" style="background-color:#eae0a8; border:0; width:140px; "><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px; border="0""></div>';
		$output[] = '</form>';


			$output[] = '</td>';
		$output[] = '</tr>';
		$output[] = '</table>';


//############################## END OF FORM ################################

    } else {  
        $output[] = '<p><b><font size=4 color=#ff0000><center>Your shopping cart is empty.</font></b></center></p>';  
    }  
    return join('',$output);

If I fill in the variables manually, it works fine.

e.g.

			$output[] = '<input type="hidden" name="item_number_1" value="1">';
		$output[] = '<input type="hidden" name="item_name_1" value="[color="Red"]Cuddly Toy[/color]">';
		$output[] = '<input type="hidden" name="amount_1" value="[color="red"]10.95[/color]">';

 

However, if I send the top form above to paypal, it only passes 2 items and they are both the same except for the item number and they are both from the last item in the cart.

 

THe form is passed to payments.php:

<?php
// Database variables
$host = "localhost"; //database location
$user = "db_user"; //database username
$pass = "db_passwd"; //database password
$db_name = "db_name"; //database name

// PayPal settings
$paypal_email = 'sandbox_paypal_email  ';
$return_url = 'http://iso2000.co.uk/payment-successful.htm'; //This is a test site
$cancel_url = 'http://iso2000.co.uk/payment-cancelled.htm';
$notify_url = 'http://iso2000.co.uk/paypal/payments.php';


// Include Functions
include("functions.php");

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

// Firstly Append paypal account to querystring
$querystring = "?business=".urlencode($paypal_email)."&";

// Append amount& currency (?) to quersytring so it cannot be edited in html

//The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
$querystring .= "item_name=".urlencode($item_name)."&";
$querystring .= "amount=".urlencode($item_amount)."&";

//loop for posted values and append to querystring
foreach($_POST as $key => $value){
	$value = urlencode(stripslashes($value));
	$querystring .= "$key=$value&";
}

// Append paypal return addresses
$querystring .= "return=".urlencode(stripslashes($return_url))."&";
$querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
$querystring .= "notify_url=".urlencode($notify_url);

// Append querystring with custom field
//$querystring .= "&custom=".USERID;

// Redirect to paypal IPN
header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
exit();

}else{
// Response from PayPal
}
?>

I can't find anything helpful by Googling. I've been testing and trying for a couple of days.

 

Can anybody advise me to get my cart items passed to Paypal.

Link to comment
Share on other sites

If you look you will realize you only labeled out all information for 2 items.. You mislabeled the numbers for the rest of these pieces.. See bold below.

 

$output[] = '<input type="hidden" name="item_number_3" value="3">';

$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';

$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

 

$output[] = '<input type="hidden" name="item_number_4" value="4">';

$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';

$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

 

$output[] = '<input type="hidden" name="item_number_3" value="5">';

$output[] = '<input type="hidden" name=item_name_2" value="'.$product.'">';

$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

 

$output[] = '<input type="hidden" name="item_number_4" value="6">';

$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';

$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

 

$output[] = '<input type="hidden" name="item_number_3" value="7">';

$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';

$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';

Link to comment
Share on other sites

Hi kts...

Many thanks for replying to my post.

I feel pretty stupid now.

Here is my updated products part of the script:

			$output[] = '<input type="hidden" name="item_number_1" value="1">';
		$output[] = '<input type="hidden" name="item_name_1" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_1" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_1" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_2" value="2">';
		$output[] = '<input type="hidden" name="item_name_2" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_2" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_2" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_3" value="3">';
		$output[] = '<input type="hidden" name="item_name_3" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_3" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_3" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_4" value="4">';
		$output[] = '<input type="hidden" name="item_name_4" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_4" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_4" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_5" value="5">';
		$output[] = '<input type="hidden" name="item_name_5" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_5" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_5" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_6" value="6">';
		$output[] = '<input type="hidden" name="item_name_6" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_6" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_6" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_7" value="7">';
		$output[] = '<input type="hidden" name="item_name_7" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_7" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_7" value="'.$qty.'">';

		$output[] = '<input type="hidden" name="item_number_8" value="8">';
		$output[] = '<input type="hidden" name="item_name_8" value="'.$product.'">';
		$output[] = '<input type="hidden" name="amount_8" value="'.$trade.'">';
		$output[] = '<input type="hidden" name="quantity_8" value="'.$qty.'">';

I have also included a quantity in the item now.

 

However, it still doesn't work.

 

I have tested this with 1 item in the cart and it sends 8 items, all the same, to paypal.

I have tested this with 2 items in the cart and it sends 8 items, all the same, to paypal.

But this time it picks out the LAST item in the cart.

I have tested this with 5 items in the cart and it sends 8 items, all the same, to paypal.

Once again it only picks out the last item.

 

I don't know how to do it, but it seems to me that the script needs to loop one item at a time and then list them as item 1, item 2 etc in the paypal form being sent to paypal.

 

Hopefully somebody will be able to advise me.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.