Monday, March 16, 2009

Adding PayPal Checkout to 3rd-Party Shopping Cart

The PayPal site already offers extensive explanation on the code, so this blog post shall contain only the codes to attach to your HTML/PHP file.

<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="you@youremail.com">
<input type="hidden" name="currency_code" value="USD">

<? php
$sql = "SELECT * FROM Shopping_Cart WHERE sessionId = '$session_id';
$result = mysql_query($sql);

$i = 1;
while ($row = mysql_fetch_assoc($result)) {

$itemName = $row['itemName'];
$itemPrice = $row['price'];

<input type="hidden" name="item_name_'.$i.'" value="'.$itemName.'">
<input type="hidden" name="amount_'.$i.'" value="'.$itemPrice. '">
$i++;

}
?>
<input type="submit" value="Checkout Cart">
</form>

The PHP code retrieves the items from the Shopping Cart table and "posts" them into the PayPal shopping cart so that they can be processed.

No comments:

Post a Comment