Link to home
Start Free TrialLog in
Avatar of sciwriter
sciwriter

asked on

Paypal with PHP and database access

The Paypal interface has been poorly handled here before, so I want something useful to me and others.  The basic <FORM> I will post below.   I am storing the data in sessions, which came from a database.

Paypal says to send your order in a form, which they present for payment.  Not interested in giving them all the details of an order, instead, we are going to use Paypal's first option --

    Method 1. Passing the Aggregate Cart Amount to PayPal
(https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/howto_checkout)

I need help formatting the "aggregate Cart" with PHP and database fields, or in my case, session data.  We need to make the Item (their t-shirt example) read :  

"Your order of <date> for <X> Items from <site>, Total Order Cost <$amount>.

To do this:  

1.  The <date> needs to be gotten from the server and presented like --  May 16, 2005
2.  The site could be hard coded, but I would like to extract  "www.thissite.com" from the URL itself.
3.  The <X> quantity is the number of items in the PHP session, which is the "key" in this statement --  
                 foreach($_SESSION['divs'] as $key => $val)
In that loop we will:
(a) add the number of items, which will .$_SESSION['divs'][$key]['qty']., convert to a character
(b) add up the <$amount> from each item, $_SESSION['divs'][$key]['price'] times the qty above
(c) total and convert $amount to 2 decimal places
(d) take the value of shipping returned from a javascript function Calc_ship(), say call it 'totship'
(e) add to total price in B above, to get order total, but split each into the form going to Paypal.

Take your time with this code, it has not been done this way before in this section, AFAIK.
Avatar of sciwriter
sciwriter

ASKER

Here is the PayPal "basic" form layout --

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="you@youremail.com">
<input type="hidden" name="item_name" value="Item Name">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="amount" value="0.00">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
Welcome again sciwriter :-)
Will go point by point, because I'm not sure if I get the whole idea yet.

Points 1) and 2)

<?php

   $orderDate = date("M d, Y");
   $siteName = $_SERVER["HTTP_HOST"];
   $noOfItems = 10;
   $orderTotal = "$100.00";

   $orderDesc = "Your order of ".$orderDate." for ".$noOfItems." Items from ".$siteName.", Total Order Cost ".$orderTotal;

   echo $orderDesc;
?>
I don't know how you read shipping cost from your JS code? I'm not sure what do you need next? Display the form?

<?php
   $orderDate = date("M d, Y"); // get current date
   $siteName = $_SERVER["HTTP_HOST"]; // get site name

   $noOfItems = 0; // initialize number of items
   $orderTotal = 0; // initialize total amount
   $totShip = 12.30; // shipping cost

   foreach($_SESSION['divs'] as $key => $val)
   {
      $noOfItems  += $_SESSION['divs'][$key]['qty'];
      $orderTotal += $_SESSION['divs'][$key]['price']*$_SESSION['divs'][$key]['qty'];
   }

   $orderTotal += $totShip; // add shipping cost to total amount
   $orderTotal = sprintf("%01.2f", $orderTotal); // format total amount with trailing zeros


   // display results
   $orderDesc = "Your order of ".$orderDate." for ".$noOfItems." Items from ".$siteName.", Total Order Cost ".$orderTotal;

   echo $orderDesc;

?>
ASKER CERTIFIED SOLUTION
Avatar of peyox
peyox
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK give me a bit to get this code into a new page, and I will get back with you, prob. tomorrow, OK?
Peyox, are you still there?
Basically peyox, your code worked great, you make it look so simple  ....

The only thing that is not working, is when I add the \n to the output, there are no line breaks --
$orderDesc = "Your order of ".$orderDate."\n for ".$noOfItems." Items from ".$siteName.",\n Total Order Cost ".$orderTotal;

And the other thing is, nothing would work until I put all the "ini_set" code prior to the Session_start().
Even though I had already done this on the previous page that called this page, I still HAD to declare the same stuff on this page.  I guess this is the one thing still confusing me about PHP sessions --

WHy you have to re-declare all the same stuff each time a PHP page runs, why you can't just call the session_start() for the session already opened, from the prior page just moments before???

Can you explain this, I know HTTP is stateless, but it seems to me the _SESSION ID is already known by the server, since it comes from a certain IP number, I don't quite get why each page must redundantly state all the same information all over again??

Thanks for your help./....
>> The only thing that is not working, is when I add the \n to the output, there are no line breaks
I just tried that. \n, \r\n or <br> don't work. I think Paypal doesn't allow to break lines in order description.

>>And the other thing is, nothing would work until I put all the "ini_set" code prior to the Session_start().
Readers, see: http:Q_21389576.html

Session ID is transported to web server (php) either via cookie or in url string. There's no direct connection with client's IP number. This is why you can hijack somebody's else session ID and connect from different computer unless session is still active (did not expired).

In order to tell the php that you want to read session variables you have to explicity use session_start(). This will look for session in a cookie (http headers) or URL string. Then session becames available to you.

The other way is to set  session.auto_start in php.ini (or thru ini_set, which doesn't make sens in your case, because you still have to call something to load a session). Since you don't have access to php.ini, you have to keep calling session_start at the beginning of each script.

--
peyox
Thanks Peyox, I'm still not 100% certain why I need to keep calling 5 lines of ini_set() code, but since this is the last page that will call it, I won't worry about it.  Cheerio, watch for any other Qs I post, you help was A++.

Sci