Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Cart & Paypal? 3

Status
Not open for further replies.

WilliamMute

Programmer
Jan 4, 2006
117
Hi everybody,

Am kinda really stock here. I developed a shopping cart which basically itemise all the products in it. i.e

Product 1 CD Case Price: £5.99 Qt: 1
Product 2 Football Price: 9.99 Qt:1

These work fine on my website. the problem is when I transfer the the items to Paypal for customers to pay for item. I cant itemise the item. The paypal cart just shows the name of my business and the total amount of the order.

Am sure most of the guru's here have done a shopping cart before, can somebody please help me to the code of having everything sent to paypal so they can be itemised in paypal?

I know this might sound really dumb, but how do I know that when paypal credits my account for say £10. How do I knwo who its from if there were five orders of £10 value but the customers all ordered different products?

I will really appreciate it if someone can shed a little bit of light please. Thank you ever sooooo much in advance.
 
is not that the normal behaviour for paypal? it's a payments service and not an invoicing service. you render the invoice to the customer and the customer pays paypal for you. in the same way that you would not expect to see the breakdown of a supermarket purchase on your bank statement and/or credit card bill.

as for the inverse: if paypal remits you a monthly/weekly account in a single payment they will also provide you with a statement of how that account is made up. it is then for you to reconcile this with your bank statement and invoice ledger.
 
Hi,

Thanks for the reply,

I know what you are saying and yes it does make sense but, typically, when I purchased anything online, I usually see all my items on paypal. its the transfer process am worried about. I'll try and get screen shots to explain what I mean.
 
Paypal has it's own cart that you can use instead of your own for this sort of thing.

However, since you already have your own cart (which I guess you're happy with) you could have a loop which copies from your cart to their cart when they go to checkout.

The calls to it are something like

Code:
<form name="add_basket" target="paypal" action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="add" value="1">
<input type="hidden" name="cmd" value="_cart">

<input type="hidden" name="business" value="my_paypal_addr@email.com">
<input type="hidden" name="item_name" value="item1">
<input type="hidden" name="item_number" value="my_sku_code">
<input type="hidden" name="quantity" value="7">
<input type="hidden" name="shipping" value="1.99">
<input type="hidden" name="shipping2" value="0">
<input type="hidden" name="amount" value="1.99">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
</form>


Hope that helps

Steve
 
PERFECT STEVE! SO ALMOST PERFECT!

Just one thing,I thought about using other people's cart i.e paypal's very own but am just worried about the complexity of it in terms of integrating it to my site. They always look soo over complex.

Now 'loop' does this now mean when someone click 'proceed to check out' how can I do a loop for each item on the cart to be submitted to paypal? am a little lost here. I understand everything else but that.

THANK YOU!
 
For the 'loop' point - I guess you must be holding the cart contents somewhere local like your database.

The Paypal cart opens in its own browser window, so when someone clicks 'Checkout' you can loop round the list of stuff in your cart and add it to the PayPal cart.

Here's some (very) rough code to show what I mean, following on from the code above
Code:
while ($row = yourdbfetchstatement() ) {
  with (add_basket) {
    item_name.value = $row["description"];
    item_number.value  = $row["sku_code"];
    quantity.value  = $row["qty"];
    amount.value = $row["cost"];
    submit();
  }
}

This may not be plain sailing - what happens if your customer presses checkout twice ? You don't want to double up the Paypal cart contents so you'd need to find a way of emptying it.

Paypal have extensive documentation and examples on their site. Have you had a look at the Developer Tools section of the Paypal Help pages ?

Best of luck [thumbsup]

Stev3e

 
Thanks Steve once more!

I'll have a crack at it now. Even though I havent done any coding in a while now.

Much appreciated!
 
i don't think that the with() construct exists in php.
 
Oh..., I guess that shows how novice I am. jpadie, how will I go about it then without the with() statement?

Secondly

the above while statement, does it goes in my Paypal form i.e before the submite part so I will basically embed the php while staement in the form?

<form action=" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="me@mybusiness.net">
<input type="hidden" name="item_name" value="="<?php echo $item_name ; ?> ">
<input type="hidden" name="amount" value="<?php echo $amount ; ?>">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="bn" value="PP-BuyNowBF">

while ($row = yourdbfetchstatement() ) {
with (add_basket) {
item_name.value = $row["description"];
item_number.value = $row["sku_code"];
quantity.value = $row["qty"];
amount.value = $row["cost"];
submit();
}
}
</form>

It that it? or am I simply living in a dream world??
 
Apologies, William - that was me thinking javascript and PHP at the same time.

If you have the cart contents in a database then PHP could read the cart from the DB as your checkout screen opens and store it in (say) javascript arrays.

Your screen's onLoad() event could then call a javascript function to go through the arrays, filling in the line items and submitting the form.

As soon as you start using the paypal cart, paypal will open a new browser window for you showing the contents, therefore the text on your checkout screen should direct the customer to review the paypal cart and proced to pay.

That's the simplest way I could think of describing a solution. I'm sure there may be a more elegant way.

Steve

 
i think paypal works ok with an incremented suffix. assuming you are using website payments standard as the product then this link should help you

to create the increment something like this would work
Code:
<form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<!--
<?php
$result = mysql_query (someselectquerytogettheshoppingcart);
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
echo <<<EOL
	<input type="hidden" name="item_name_$i" value="{$row['item_name']}"/>
	<input type="hidden" name="amount_$i" value="{$row['item_amount']}" />
	//etc
EOL;
	$i++;
}
?>
</form>
the variables you need to pass are in the link that i posted above too. but i am not sure that you are able to do this using XClick. you may have to use a different paypal product.
 
That looks like the correct way of doing it , jpadie.

From a quick test, it also seems to overwrite the paypal cart rather than adding to it, which was one of my earlier concerns.

Steve
 
Ok guys,
thank you for all your VALUABLE efforts, let me pretend I totally understand whats going on here and try it out. Ps, I hope my computer dosnt blow up! with my interpretation of what I think your saying.

Would let you know how I get on. let me try it now.

Thanks
 
Hi Guys Again...

I've just tried out all of your suggetions, my recent coding now looks like this

Code:
<form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="cmd" value="_xclick">


<?php
"
SELECT products.product_name, products.product_price, products.product_dimension, products.image, products.product_id, springrisecart.quantity, products.id
FROM springrisecart
INNER JOIN
  products
  ON
    products.id = springrisecart.product_id
WHERE
   springrisecart.`session`='$cart_id'";
$result = mysql_query($sql, $Connection) or die (mysql_error());

$i = 0;
while ($row = mysql_fetch_assoc($result)) {
echo <<<EOL
    <input type="hidden" name="item_name" value="{$row['product_name']}"/>
	<input type="hidden" name="business" value="sales@mysite.org">
	<input type="hidden" name="amount" value="{$row['product_price']}"/>
	<input type="hidden" name="no_note" value="1">
	<input type="hidden" name="currency_code" value="GBP">
	<input type="hidden" name="lc" value="GB">
	
	
    //etc
EOL;
    $i++;
}
?>
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="[URL unfurl="true"]https://www.paypal.com/en_US/i/btn/x-click-but23.gif"[/URL] border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="[URL unfurl="true"]https://www.paypal.com/en_GB/i/scr/pixel.gif"[/URL] width="1" height="1">
</form>


The problem is that, the loop is not working. on Paypal, the product name etc show and the price but its only 1 product even though I had 3 items on my cart in my site.

Any suggestions please?

Thank you so much
 
Here's the sample code I used to try out jpadie's suggestion (just insert your own paypal email address).

Note that paypal expects the unit price in amount_x, not the line total

Code:
<html>
<head>
<title>Paypal test</title>
</head>
<body onLoad=basket.submit()>

<form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post" name=basket target='paypal'>
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="me@email.com">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">

<?
    $rownum = 1;

    echo "<input type='hidden' name='item_name_".$rownum."' value='Grommets'>";
    echo "<input type='hidden' name='item_number_".$rownum."' value='ABC123'>";
    echo "<input type='hidden' name='quantity_".$rownum."' value='1000'>";
    echo "<input type='hidden' name='amount_".$rownum."' value='0.75'>";

    $rownum++;
    echo "<input type='hidden' name='item_name_".$rownum."' value='Widgets'>";
    echo "<input type='hidden' name='item_number_".$rownum."' value='DEF456'>";
    echo "<input type='hidden' name='quantity_".$rownum."' value='40'>";
    echo "<input type='hidden' name='amount_".$rownum."' value='2.5'>";

    $rownum++;
    echo "<input type='hidden' name='item_name_".$rownum."' value='Washers'>";
    echo "<input type='hidden' name='item_number_".$rownum."' value='XYZ789'>";
    echo "<input type='hidden' name='quantity_".$rownum."' value='400'>";
    echo "<input type='hidden' name='amount_".$rownum."' value='0.5'>";
?>
</form>
</body>
</html>

Steve
 
combining the two posts (steve and William) you need something like the following. Note that this does not deal with tax and you may need to include a certificate. read the paypal instruction manual

Code:
<form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="cmd" value="_xclick">
<!--setup for paypal integration -->
<input type="hidden" name="business" value="sales@mysite.org">    
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="lc" value="GB">
<!-- the following line is needed in place of xclick-->
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1">

<?php
"
SELECT 
		products.product_name as product_name, 
		products.product_price as product_price, 
		products.product_dimension as product_dimension, 
		products.image as image, 
		springrisecart.product_id as product_id, 
		springrisecart.quantity as quantity	  
FROM 
	springrisecart
	INNER JOIN
  		products
  		ON
    	products.id = springrisecart.product_id
WHERE
	springrisecart.`session`='$cart_id'
	";
$result = mysql_query(	$sql, 
						$Connection) 
			or die (mysql_error());
?>
 	

<?php

$i = 1;
while ($row = mysql_fetch_assoc($result)) {
	echo <<<EOL
	<!-- ITEM $i DATA -->
    <input type="hidden" name="item_number_$i" value="{$row['product_id']}"/>
    <input type="hidden" name="item_name_$i" value="{$row['product_name']}"/>
    <input type="hidden" name="quantity_$i" value="{$row['quantity']}"/>
    <input type="hidden" name="amount_$i" value="{$row['product_price']}"/>
	<!-- END ITEM $i DATA -->
EOL;
    $i++;
}
?>
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="[URL unfurl="true"]https://www.paypal.com/en_US/i/btn/x-click-but23.gif"[/URL] border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="[URL unfurl="true"]https://www.paypal.com/en_GB/i/scr/pixel.gif"[/URL] width="1" height="1">
</form>
 
Steve, Jpadie, what can I say... Thank You a Zillion it works works works! One thing though which am not sure if thats normal or not. When the you choose buy now on my site, it goes to paypal with just the total amount of the shopping cart, option to pay and a small link saying 'view cart' It is only when you click on the link thats when you see all the items in the cart. is this 'normal' as I've not experience this before when paying for stuff on paypal myself? Other than that, it works like a magic!

Am grateful! Thank you for your support the great genious of PHP.
 
i'm not sure what is normal for paypal. have a read of the technical interface specification in that link i provided above - that should give you the answer. and do remember that the code posted does not handle tax and Paypal do make it your responsibility to get the tax right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top