<<

. 83
( 132 .)



>>

charge_card() function, which sends a transaction (over an SSL link, naturally) to
an authorization center. Here is that code:

function charge_card()
{
if (!$this->validate_card())
{
return FALSE;
}

$this->calculate_total();
$total_charged = $this->total_price + $this->shipping_cost;

// pass it off to one of the Charge subclasses
return AuthorizeNet::sale($this);
// return IONGATE::sale($this);
}
492 Part IV: Not So Simple Applications

Notice that near the end, the return value is forced TRUE. This is for testing pur-
poses. In reality, your authorization house returns a value for auth_result that
determines whether the function returns TRUE or FALSE.

Scripts
These are the pages called by URLs and the includes. You will probably notice that
not much is involved here. Almost all the work is done in the classes. These scripts
are concerned more with presentation of data and ways of manipulating them ” as
they should be, because such a design makes it more likely that you™ll be able to
reuse the classes somewhere else someday.

DISPLAY.PHP
This prints out either a list of categories or a specific product:

<?php
require_once(˜header.php™);

$product_id = (int)array_key_value($_REQUEST,™product_id™,0);
$submit = (string)array_key_value($_POST, ˜submit™);
$again = (string)array_key_value($_POST, ˜again™);

$p = new Product(array(˜product_id™=>$product_id));

if (empty($p->product_id))
{
$p->description = “Here at Bag™O™Stuff, we™ve got so much stuff
we don™t know what to do with it. So buy some and help us clear out
some space.”;
$p->product = “What™ve We Got?”;
}
else
{
$p->fetch_from_db();
}

// begin constructing the page title with a link to the main page
// of the catalog

// add a link back to the product level to the page title,
// followed by the name of this product, and print out the
// top of the page

$page_title = anchor_tag(˜index.php™, “Bag™O™Stuff”);
foreach ($p->parents() as $t)
{
Chapter 14: Shopping Cart 493

$page_title .= ˜&nbsp;&gt;&nbsp;™.anchor_tag(
˜display.php?product_id=™.$t->product_id
, $t->product
);
}
if ($p->product_id)
{
$page_title .= ˜&nbsp;&gt;&nbsp;™.anchor_tag(
˜display.php?product_id=™.$p->product_id
, $p->product
);
}
else
{
$page_title .= ˜<br><br>™.$p->product;
}
print start_page($page_title);

if ($submit == ˜Add to Cart™ || $again == ˜please™)
{
include(˜cart.php™);
}
else
{
print print_product($p);
}
// print out the bottom of the page
print end_page();

?>

It doesn™t get a whole lot more basic than this: If this page is to display a cate-
gory (not a product), a category is loaded and then printed. The same happens for a
product, if appropriate. If you remember the display.php page from Chapter 12, you
might notice that the only real difference is that the objects instantiated here are
created from the classes new to this application. That gives you access to the new
print methods, which were designed to work with this application.

CART.PHP
Here™s the page that creates your shopping cart:

<?php
$o = get_session_order();

$href = regular_url($_SERVER[˜PHP_SELF™]);
494 Part IV: Not So Simple Applications

// display the contents of the shopping cart
print start_form();
print hidden_field(˜again™,™please™);
print ˜<h3>Shopping Cart</h3>™;
print ˜<h5>(Change quantity to 0 to remove an item.)</h5>™;
print start_table(array(
˜border™=>0
, ˜width™=>™80%™
, ˜cellpadding™ => 5
));
print table_row(
table_header_cell(˜Item™)
, table_header_cell(array(˜align™=>™right™,
˜value™=>™Quantity™))
, table_header_cell(array(˜align™=>™right™, ˜value™=>™Price™))
, table_header_cell(array(˜align™=>™right™, ˜value™=>™Total™))
);
print table_row(˜<hr>™,™<hr>™,™<hr>™,™<hr>™);
$total_price = 0;
$total_quantity = 0;
foreach ($o->items() as $item)
{
$tprice = $item->price * $item->quantity;
$qfield = text_field(array(
˜name™=>”quantity[{$item->item_name}]”
, ˜value™=>$item->quantity
, ˜size™=>4
));
print table_row(
anchor_tag($href.™?product_id=™.$item->product_id, $item-
>item_name)
, table_cell(array(˜align™=>™right™, ˜value™=>$qfield))
, table_cell(array(˜align™=>™right™, ˜value™=>money($item-
>price)))
, table_cell(array(˜align™=>™right™,
˜value™=>money($tprice)))
);

// keep a running total of the quantity and price of items
// in the cart.
$total_price = $total_price + $tprice;
$total_quantity = $total_quantity + $item->quantity;
}
$o->total_price = $total_price;
Chapter 14: Shopping Cart 495

$o->total_quantity = $total_quantity;

// print out totals
print table_row(˜<hr>™,™<hr>™,™<hr>™,™<hr>™);
print table_row(
table_header_cell(array(˜align™=>™right™, ˜value™=>™Grand
Total™))
, table_header_cell(array(˜align™=>™right™,
˜value™=>$total_quantity))
, ˜&nbsp;™
, table_header_cell(array(˜align™=>™right™,
˜value™=>money($total_price)))
);

print table_row(
table_cell(array(
˜colspan™ => 4
, ˜value™ => submit_field(˜Recalculate Order™)
))
);

$url = ˜display.php™;
if (isset($p))
{
$url .= ˜?product_id=™.$p->product_id;
}
print table_row(
table_cell(array(
˜align™ => ˜right™
, ˜colspan™ => 4
, ˜value™ => ˜<b>™
. anchor_tag(regular_url($url), ˜Continue Shopping™)
. ˜</b>™
))
);

print end_table();
print end_form();
save_session_order($o);
?>

CHECKOUT.PHP
Now, finally, it™s time to check out. Note that this is really the only file that needs
to be on the secure server. There™s no need for the catalog portions or even the cart
496 Part IV: Not So Simple Applications

page to be on a secure server, because they don™t contain information that needs to be
protected. However, on this page you™re going to be accepting credit-card information.
Extensive comments are contained within the script to help you get through the
page™s logic.

<?php
require_once(˜header.php™);

// get the session variables for the shopping cart, the current
order,
// and the user™s email address
$order = get_session_order();

// if a value for ˜email™ was posted to the script from a form, use
that
// in preference to the session variable
$_SESSION[˜email™] = array_key_value(
$_POST
, ˜email™
, array_key_value($_SESSION, ˜email™)
);
$order->email = $_SESSION[˜email™];

// set up variables defining the values of the buttons of the form
// (defining the values once helps avoid errors caused by spelling
problems.)
$order_button = ˜ORDER NOW!™;

// load data from form into order
$order->build($_POST);

$submit = array_key_value($_POST, ˜submit™);
if ($submit == $order_button)
{
// the user hit the big ORDER button. validate their credit
// card and charge it, using the ValidateCard() and ChargeCard()
// functions of the Order class.

if (!$order->charge_card())
{
print “could not charge card\n”;
}
else
{
Chapter 14: Shopping Cart 497

// the charge went through - write the order to the
// database using the SaveOrder() method of the Order class.
$order->write_to_db();

// redirect the user to the receipt page for a receipt
// they can print or save to a file, and exit the script.
// pass on the ID value of the new order record and
// the session ID that was passed in to this script.

$url = ˜receipt.php?order_id=™
. $order->order_id
. ˜&sessid=™
. session_id()
;
// header(˜Location: $url™);
print paragraph(anchor_tag($url, $url));
// exit;
}
}

<<

. 83
( 132 .)



>>