Recording Sales Information

Purchase approval and customer billing information is returned from the credit card company to the page identified in the ReturnURL of the submitted form. This is our ordercapture.php page. The information is sent as hidden fields and is available to this page through the $_POST[] Collection using the following names:

$_POST[Approval]true (order accepted) or false (order rejected)
$_POST[Amount]$ Order total
$_POST[CustomerID]Customer order number
$_POST[Name]Customer billing name
$_POST[Address]Customer billing address
$_POST[City]Customer billing city
$_POST[State]Customer billing state
$_POST[Zip]Customer billing zip
$_POST[Phone]Customer phone number
$_POST[Email]Customer email address

The ordercapture.php page is a pure PHP page without any XHTML coding. This page is not viewable by the customer, but exists, instead, to peform background processing of the returned information prior to redirecting the customer to the salesorder.php page.

One major piece of work that needs to be done on this page is to record the sales order so that a permanent record exists of the sale. This record can be used to recreate the order if need be, and to provide information to interface with an accounting and inventory systems (if they existed, which they don't here).

Order Header and Detail Records

The traditional manner of keeping track of sales orders is through the creation of two types of sales records. An order header record contains order identification information such as order number, order date, and customer billing information. There is one order header record per order. An order detail record contains details about the items purchased--the item numbers, quantities, and prices. There is one order detail record for each item purchased. Such information is maintained in these files that the original order can be recreated.

The order header and order detail files are database tables maintained in the eCommerce.mdb database. Their formats are shown below.

OrderHeader (table)
Field Name Field Type Field Size
OrderNo Text 10
OrderDate Date/Time  
CustomerName Text 50
CustomerAddress Text 50
CustomerCity Text 50
CustomerState Text 2
CustomerZip Text 10
CustomerPhone Text 15
CustomerEmail Text 50

OrderDetail (table)
Field Name Field Type Field Size
OrderNo Text 10
ItemNumber Text 6
ItemTitle Text 50
ItemPrice Currency 2-decimals  
ItemQuantity Numeric long integer  

Gathering Order Information

The first portion of the script on the ordercapture.php page gathers the information sent from the credit card company through the $_POST[] Collection and assigns it to variables for ease of handling.

<?php
'-- Capture information from credit card return
$Approval = $_POST[Approval];
$Amount = $_POST[Amount];
$OrderNo = $_POST[CustomerID];
$Name = $_POST[Name];
$Address = $_POST[Address];
$City = $_POST[City];
$State = $_POST[State];
$Zip = $_POST[Zip];
$Phone = $_POST[Phone];
$Email = $_POST[Email]; ...

The credit card company sends an Approval flag set to "True" or "False" depending on whether the order was approved or not. If the order was approved, then we need to store the order information in the OrderHeader and OrderDetail tables; if the order was not approved, then we don't keep this information since a sale was not made. In either case, however, we need to empty the shopping cart of this order. There is not, at this point, any pending order.

The general logic for the script to handle these processing tasks is illustrated below.

if ($Approval) {

  '-- Create OrderHeader record

  '-- Create OrderDetail record

}

'-- Clear ShopCart table

Creating the OrderHeader Record

We begin the script by opening a connection to the eCommerce database. This connection is needed for clearing the shopping cart whether or not order records are created. Then, if an approval was received from the credit card company, the script creates an OrderHeader record.

$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\PHPTutorial\Ecommerce\databases\ecommerce.mdb','','');
if ($Approval)
{
$sqlInsert = "INSERT INTO OrderHeader (OrderNo,OrderDate,CustomerName,CustomerAddress,CustomerCity,CustomerState,CustomerZip,CustomerPhone,CustomerEmail) Values ('$OrderNo','$Date','$Name','$Address','$City','$State','$Zip','$Phone','$Email')";
$rsInsert = odbc_exec($conn,$sqlInsert);
}

This processing is a simple matter of creating a new record in the OrderHeader table and copying fields sent from the credit card company into the new record. The OrderDate field is filled with the current system date. After the record is populated and updated, the recordset is closed.

Creating the OrderDetail Records

Next, the script creates one or more records in the OrderDetail table. The information for this table, however, doesn't come from the credit card company. Parts of it are in the ShopCart table and parts in the Products table. We are, basically, transferring the details of the current order to a similar set of OrderDetail records. We begin the scripting by creating the three recordsets needed for this processing: the $RSDetail recordset for accessing the OrderDetail table, the $RSShopCart recordset for accessing the ShopCart table, and the $RSProducts recordset for accessing the Products table.

  '-- Create OrderDetail record
$sqlDetail ="SELECT * FROM OrderDetail WHERE NULL";
$rsDetail = odbc_exec($conn,$sqlDetail);

$sqlShopCart ="SELECT * FROM ShopCart WHERE OrderNo ='$OrderNo'";
$rsShopCart = odbc_exec($conn,$sqlShopCart);

  while ($row = odbc_fetch_array($rsShopCart))
  
  { 
  
  $sqlProd ="SELECT ItemTitle,ItemPrice FROM Products WHERE ItemNumber ='$row[OrderItem]'";
  $rsProd = odbc_exec($conn,$sqlProd);
  
  $ProdTitle = odbc_result($rsProd,ItemTitle);
  $ProdPrice = odbc_result($rsProd,ItemPrice);
  
	$sqlInsertDetail = "INSERT INTO OrderDetail (OrderNo,ItemNumber,ItemQuantity,ItemTitle,ItemPrice) Values ('$row[OrderNo]','$row[OrderItem]','$row[OrderQuantity]','$ProdTitle','$ProdPrice')";
	
	$rsInsertDetail = odbc_exec($conn,$sqlInsertDetail);
  
  }

The script iterates through the shopping cart records for the matching OrderNos, copying the OrderNo, OrderItem, and OrderQuantity fields to the associated fields in the OrderDetail record. Then, the OrderItem field is used to retrieve the ItemTitle and ItemPrice for this product from the Products table, copying them to the ItemTitle and ItemPrice fields of the OrderDetail record. The completed record is updated to the OrderDetail table and the next shopping cart record is processed. In the end, there is an OrderDetail record for each of the shopping cart records.

Clearing the Shopping Cart

The final portion of the script clears the shopping cart of the purchased items. These records are identified by having an OrderNo that matches the OrderNo of the approved, or disapproved, order.

'-- Clear ShopCart table
$sqlDelete = "DELETE FROM ShopCart WHERE OrderNo='$OrderNo'";
$rsDelete = odbc_exec($conn,$sqlDelete);


header("Location:salesorder.php?Approval=$Approval")

When this "background" processing is completed (recall, there is no XHTML code on this page), there is immediate redirection to the salesorder.php page. The approval notification is passed along to this page to govern the processing that takes place there.