...

View Full Version : paypal shopping cart wont load.



tim967
08-20-2011, 07:06 PM
hi all, been folllowing the phpacademy mini shopping cart tutorial and i've almost finished, gone to test it and it just takes me to a paypal page rather than the paypal cart page. here is my code, hope someone can help.


<?php
session_start();
$page = 'index.php' ;

mysql_connect('localhost','tjwebsol_cart','HIDDEN') or die(mysql_error());
mysql_select_db('tjwebsol_cart') or die(mysql_error());

if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
$_SESSION['cart_'.(int)$_GET['add']]+='1';
}

}
header('Location: '.$page);

}

if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header('Location: '.$page);
}
if (isset($_GET['delete'])) {
$_SESSION['cart_'.(int)$_GET['delete']]='0';
header('Location: '.$page);
}



function products() {
$get = mysql_query('SELECT * FROM products WHERE quantity > 0 ORDER BY id DESC');
if (mysql_num_rows($get)==0 ) {
echo "There are no products to display.";
}
else
{
while ($get_row = mysql_fetch_assoc($get)) {
echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />&pound;'.number_format($get_row['price'] ,2).' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
}
}

}

function paypal_items() {
$num = 0;
foreach($_SESSION as $name => $value) {
if($value!=0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, strlen($name)-5);
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$num++;
echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
echo '<input type="hidden" name="shipping_'.$num.'" value="2.50">';
echo '<input type="hidden" name="shipping2_'.$num.'" value="1.00">';
echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
}
}
}
}
}

function cart() {
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).'<a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />';
}
}
$total += $sub;
}
}
if ($total==0) {
echo "Your cart is empty.";
}
else {
echo '<p>Total: &pound;'.number_format($total, 2).'</p>';
?>
<p>
<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="timtoday967@hotmail.co.uk">
<?php paypal_items(); ?>
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="amount" value="<?php echo $total; ?>">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</p>
<?php
}
}


?>

mlseim
08-20-2011, 09:33 PM
Not sure what the post means ...
What's the difference between "paypal page", and the "paypal cart page" ...
Are those different scripts? ??

tim967
08-20-2011, 09:57 PM
Not sure what the post means ...
What's the difference between "paypal page", and the "paypal cart page" ...
Are those different scripts? ??

Sorry I probably should have explained my problem a bit better. Basicly what should happen is once the user has added some items to there cart and clicked the paypal checkout button it should open a page on the paypal website that shows the users cart and details about all the items e.g name, quantity price etc and then they should be able to pay from there by entering their details. But at the moment all it does is show the web page https://www.paypal.com/cgi-bin/webscr rather than the cart with the users order.

JamesOxford
08-21-2011, 04:16 AM
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

Without knowing more about which PayPal API you are using, and spending a bit more time looking at PayPal and your script, The "action" attribute in form is what determines where the script goes. If the page that is loading is wrong, change the action attribute to the page you want.

What is possibly happening though, is that it is not taking the person to the wrong page at all. It is taking them to the correct page, but they are not logged in so it redirects them to the login page. You aren't trying to checkout though at this point are you? Just trying to add to a PayPal shopping cart. This is not really likely what is happening. I haven't really used that API before. I have used Website Payments Pro. Sorry I can't be of more help.

tim967
08-21-2011, 09:05 AM
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

Without knowing more about which PayPal API you are using, and spending a bit more time looking at PayPal and your script, The "action" attribute in form is what determines where the script goes. If the page that is loading is wrong, change the action attribute to the page you want.

What is possibly happening though, is that it is not taking the person to the wrong page at all. It is taking them to the correct page, but they are not logged in so it redirects them to the login page. You aren't trying to checkout though at this point are you? Just trying to add to a PayPal shopping cart. This is not really likely what is happening. I haven't really used that API before. I have used Website Payments Pro. Sorry I can't be of more help.

I'll try and do some more research on the paypal api today, not really sure which one it uses as I was following a video tutorial. I'll keep trying and hopefully something will fix it.

mlseim
08-21-2011, 06:37 PM
Tim,
Be sure to create a free Sandbox account to set up a fake seller and fake buyer.
This will allow you to test your site/scripts in a real PayPal environment. You simulate
a complete transaction using a test credit card. Once you get out all of the bugs,
you simply redirect your API and PayPal account to the REAL PayPal site.

https://developer.paypal.com/

They also have many script examples and documentation.


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum