Page 1 of 1

PHP Shooping Cart - Paypal Auto return Problem

Posted: Fri Sep 07, 2007 8:05 am
by dream2rule
Hello All,

I have coded for the shopping cart using PHP and Mysql and now am stuck in the process of integrating it with a paypal account.

Here's the checkout_pay.php page which is used to checkout the selected cart items to paypal

Code: Select all

<?php
session_start();
ob_start();
$sid = session_id();
include("includes/config.php");
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
</head>

<body>

  <table width="95%" border="0" align="center">
    <tr>
      <td colspan="3" align="center"><?php require("includes/header.php");?></td>
    </tr>
    
    <tr>
      <td width="18%" valign="top"><table width="91%" height="84" border="0">
        <tr>
          <td align="left" valign="top">
		  		<?php 
					require("includes/bar.php");
					echo "<hr>";
					if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE)
					{
						echo "Logged in as <br><strong>" . $_SESSION['SESS_USERNAME']. "</strong> <br>[<a href='" . $config_basedir
								. "/logout.php'>logout</a>]";
					}
					else
					{
						echo "<a href='". $config_basedir . "/login.php'>Login</a>";
					}
				?>          </td>
        </tr>
        
      </table>
      </td>
      <td width="66%" valign="top">
      
      <!-- CONTENT STARTS HERE -->
      
      <table width="99%" height="88" border="0" align="center">
        <tr>
          <td align="left" valign="top"><?php
					
					require("includes/db.php");
					require("functions.php");
					
					if($_POST['paypalsubmit'])
					{
						$upsql = "UPDATE orders SET status = 2, payment_type = 1 WHERE id = " . $_SESSION['SESS_ORDERNUM'];
						$upres = mysql_query($upsql);
						$itemssql = "SELECT o.total,ot.product_id FROM orders o, order_items ot 
						WHERE o.id = " . $_SESSION['SESS_ORDERNUM']. " AND o.id = ot.order_id ";
						
						$itemsres = mysql_query($itemssql);
						$num_rows = @mysql_num_rows($itemsres);
		
						//echo $itemssql."<br>".$itemsres."<br>".$num_rows."<br>";
		
						if($num_rows > 0)
						{
							$row = mysql_fetch_assoc($itemsres);
										
							if($_SESSION['SESS_LOGGEDIN'])
							{
								unset($_SESSION['SESS_ORDERNUM']);
							}
							else
							{
								session_register("SESS_CHANGEID");
								$_SESSION['SESS_CHANGEID'] = 1;
							}
						
							header("Location: 
							https://www.paypal.com/cgi-bin/webscr?c ... item_name=". 
							urlencode($_POST['item_name']). "+Order&item_number=PROD" . $row['product_id']."&amount=" . urlencode(
							sprintf('%.2f',$row['total'])) . "&no_note=1&currency_code=USD&lc=US&submit.x=41&submit.y=15");
							//exit;						
						}
						else
						{
							echo ("<br><b>ERROR:</b> Your session has expired. Please try again.");
						}
					}
					
					else if($_POST['chequesubmit'])
					{
						$upsql = "UPDATE orders SET status = 2,	payment_type = 2 WHERE id = ". $_SESSION['SESS_ORDERNUM'];
						$upres = mysql_query($upsql);
						$upres = mysql_query($upsql);
						if($_SESSION['SESS_LOGGEDIN'])
						{
							unset($_SESSION['SESS_ORDERNUM']);
						}
						else
						{
							session_register("SESS_CHANGEID");
							$_SESSION['SESS_CHANGEID'] = 1;
						}
						
					
				?>
            <h1>Paying by cheque</h1>
						Please make your cheque payable to
						<strong><?php echo $config_sitename; ?></strong>.
						<p>	
						Send the cheque to:	
						<p>
						<?php echo $config_sitename; ?><br>
									street 1,<br>
									address1,<br>
									address 2,<br>
									city.<br>
					<?php
					}
					else
					{
						echo "<h1>Payment</h1>";
						showcart();
					?>
					<h2>Select a payment method</h2>
					<form action='checkout_pay.php' method='POST'>
					<!-- <form action='https://www.paypal.com/cgi-bin/webscr' method='POST'> -->
					<table cellspacing=10>
					<tr>
                        <td><h3>PayPal</h3></td>
                        <td>
                        This site uses PayPal to accept
                        Switch/Visa/Mastercard cards. No PayPal account
                        is required - you simply fill in your credit
                        card details
                        and the correct payment will be taken from your account.                        </td>
						<td><input type="submit" name="paypalsubmit" value="Pay with PayPal"></td>
                        <!-- <input type="hidden" name="business" value="payments@xyzshopping.com" /> -->
						<input type="hidden" name="cmd" value="_xclick" />
						<input type="hidden" name="business" value="drishti.w@seguesol.com" />
						<input type="hidden" name="quantity" value="<?=$_SESSION["quantity"]?>" />
						<input type="hidden" name="item_name" value="Product(s) at xyzshopping.com" />
						<input type="hidden" name="amount" value="<?=$_SESSION["total"]?>" />
						<input type="hidden" name="shipping" value="<?=$shipping?>">
						<input type="hidden" name="return" value="http://localhost/shopping/success.php" />
						<!-- <input type="hidden" name="return" value="http://localhost/shopping/success.php" /> -->
						<input type="hidden" name="cancel_return" value="http://localhost/shopping/checkout_pay.php" />
						<!-- image_url gives the 150x50 logo location -->
						<input type="hidden" name="image_url" value="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" />
						<input type="hidden" name="currency_code" value="USD" />
						<input type="hidden" name="lc" value="US" />
					</tr>
					
					<tr>
                        <td><h3>Cheque</h3></td>
                        <td>
                        If you would like to pay by cheque, you
                        can post the cheque for the final
                        amount to the office.                        </td>
                        <td><input type="submit" name="chequesubmit" value="Pay by Cheque"></td>
					</tr>
					</table>
					</form> 
                   <?php
						}   
					?>         </td>
        </tr>
      </table>
      
      <!-- CONTENT ENDS HERE -->
      
      </td>
      <td width="15%">&nbsp;</td>
    </tr>
    
    <tr>
      <td colspan="3" align="center">
	  		<?php require("includes/footer.php");?>   
     </td>
    </tr>
  </table>

</body>
</html>
I would want this code to work and get redirected to the page success.php where i am displaying a message like Transaction Successful .

I am stuck.. Can you please help me out with this one.

Regards,

Posted: Fri Sep 07, 2007 12:17 pm
by Christopher
I did not wade through all your code, but with Paypal (like other payment processors) you provide a return URL when you send the user over to their checkout. Upon completion they will be sent back your your "success" page with a success/fail status.