The OrderCapture.aspx page is linked from the credit card processing site (locally, the OrderForm.aspx page). The purpose of this page is to capture returned order approval and customer information sent through a submitted form.
An eCommerce site requires information about customer purchase transactions in order to keep its own books in order. For instance, it needs to know if the purchase was approved; it needs to know the shipping address of the customer to send the merchandise; and it needs to know the dollar amount of the approved purchase to update its accounting system. Ancilliary processing that can take place includes updating the inventory system to reduce the quantities of items on hand by the quantities purchased. Information to initiate this processing is produced or gathered by the credit card processing service and packaged into a return form sent to the eCommerce site.
There are a whole host of business processing events initiated by a single purchase. The focus here, though, is not on all possible transaction events, only on some common technical tasks surrounding on-line commerce. One of these common tasks is generation of email confirmations to customers, shown below in the format produced by the OrderCapture.aspx page.
One thing to note about the OrderCapture.aspx pageit produces no page display; it is comprised entirely of script with no XHTML tags or server controls. It is a non-visual transition step to producing a final sales order page that does visually summarize the completed order.
Capturing Order Information
The first part of the script on this page captures order information from the HTML form sent from the CreditCheck.aspx page. The System.Data.OleDb namespace is required for access to the ShopCart table; the System.Net.Mail namespace is required to produce an email message.
<%@ Page Language="vb" Debug="True" %> <%@ Import Namespace="System.Data.OleDb" %> <%@ Import Namespace="System.Net.Mail" %> <SCRIPT Runat="Server"> Sub Page_Load '-- Capture information from credit check return Dim Approved As String = Request.Form("ReturnApproved") Dim MerchantID As String = Request.Form("ReturnMerchantID") Dim CustomerID As String = Request.Form("ReturnCustomerID") Dim OrderAmount As String = Request.Form("ReturnOrderAmount") Dim Name As String = Request.Form("ReturnName") Dim Address As String = Request.Form("ReturnAddress") Dim City As String = Request.Form("ReturnCity") Dim State As String = Request.Form("ReturnState") Dim Zip As String = Request.Form("ReturnZip") Dim EmailAddress As String = Request.Form("ReturnEmail") If Approved = "Cancel" Then Response.Redirect("ShopCart.aspx") End If ... End Sub </SCRIPT>
When the page loads, all form values are assigned to variables which are used in the continuing script to produce an email message. If, however, the customer had decided to abandon order processing, then the Request.Form("Approved") field contains the string "Cancel." In this case, no email is sent and the customer is redirected to the shopping cart page.
Composing an Order Confirmation Email
Email is sent from a Web server through its SMTP (Simple Mail Transfer Protocol) service. As described under "Sending Email," the ASP.NET MailMessage class is used to compose and send emails through an SmtpClient.
The following code is a continuation of the Page_Load subprogram, and begins the email production process by created a new MailMessage object (Email) and setting its To, From, and Subject properties. Notice that the values supplied for the To and Subject properties are the EmailAddress and CustomerID variables that are captured from the returned form. The From property is a fictitious email address.
'-- Send email confirmation of order If EmailAddress <> "" Then Dim Email = New MailMessage Dim Client = New SmtpClient("localhost") Dim FromAddress = New MailAddress("orders@webWarehouse.com") Email.From = FromAddress Dim ToAddress = New MailAddress(EmailAddress) Email.To.Add(ToAddress) Email.Subject = "Order " & CustomerID & " Confirmation" ...
The major effort is in composing the body of the message. This is done by creating an EmailBody variable and concatenating lines of text to it. The IsBodyHtml property of this message will be set to True, so XHTML code can be part of the message body. The following lines prepare the beginning lines of the message, including the header for the table within which shopping cart items are displayed.
... Dim EmailBody As String = "" EmailBody &= "<html>" EmailBody &= "<body>" EmailBody &= "<span style=""font-size:14pt; font-weight:bold"">" EmailBody &= " webWarehouse.com" EmailBody &= "</span><br/>" EmailBody &= "<span style=""font-size:12pt; font-weight:bold"">" EmailBody &= " Order Confirmation" EmailBody &= "</span><br/><br/>" EmailBody &= "Date: " & Today & "<br/>" EmailBody &= "Order No.: " & CustomerID & "<br/>" EmailBody &= "<br>" EmailBody &= Name & "<br/>" EmailBody &= Address & "<br/>" EmailBody &= City & ", " & State & " " & Zip & "<br/>" EmailBody &= "<p>Thank you for your order. We appreciate your shopping at " EmailBody &= "webWarehouse.com. If you have any questions about your order, " EmailBody &= "please email us at <a href=""mailto:orders@webWarehouse.com"">" EmailBody &= "orders@webWarehouse.com</a> and reference the order number " EmailBody &= "listed above.</p>" EmailBody &= "<table border=""1"" cellpadding=""3""" Emailbody &= "style=""border-collapse:collapse"">" EmailBody &= "<tr style=""background-color:#F0F0F0"">" EmailBody &= " <th>ID</th>" EmailBody &= " <th>Title</th>" EmailBody &= " <th>Qty</th>" EmailBody &= " <th>Price</th>" EmailBody &= " <th>Amount</th>" EmailBody &= "</tr>" ...
Note how variables are interspersed with text strings. Also, be aware that quotation marks that are part of the message must be entered as double quotes ("") since the enclosing text string is itself quoted.
Next, items from the shopping cart for this customer are included as rows within the display table. This retrieval requires a connection to the database and selection from the ShopCart table of all records for this customer. The returned recordset is iterated and a table row is formatted for each record. During this iteration, BookAmounts are calculated and the OrderAmount is summed.
... Dim BookAmount As Decimal Dim OrderAmount As Decimal = 0.00 Dim DBConnection As OleDbConnection Dim DBCommand As OleDbCommand Dim DBReader As OleDbDataReader Dim SQLString As String DBConnection = New OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("../Databases/BooksDB.mdb")) DBConnection.Open() SQLString = "SELECT * FROM ShopCart " & _ "WHERE OrderNumber = '" & Session("OrderNumber") & "' " & _ "ORDER BY BookID" DBCommand = New OleDbCommand(SQLString, DBConnection) DBReader = DBCommand.ExecuteReader() While DBReader.Read() BookAmount = DBReader("BookPrice") * DBReader("BookQty") OrderAmount += BookAmount EmailBody &= "<tr>" EmailBody &= " <td>" & DBReader("BookID") & "</td>" EmailBody &= " <td>" & DBReader("BookTitle") & "</td>" EmailBody &= " <td style=""text-align:right"">" & _ String.Format("{0:D}", DBReader("BookQty")) & _ " </td>" EmailBody &= " <td style=""text-align:right"">" & _ String.Format("{0:N}", DBReader("BookPrice")) & _ " </td>" EmailBody &= " <td style=""text-align:right"">" & _ String.Format("{0:N}", BookAmount) & "</td>" EmailBody &= " </td>" EmailBody &= "</tr>" End While DBReader.Close() DBConnection.Close() ...
Finally, the shipping cost is calculated and the total lines are added to the table.
... Dim OrderShipping As Decimal = OrderAmount * Application("Shipping") OrderAmount += OrderShipping EmailBody &= "<tr>" EmailBody &= " <td colspan=""4"" style=""align:right"">" & _ String.Format("{0:N}", Shipping) & "</td>" EmailBody &= " <td style=""align:right"">" & _ String.Format("{0:N}", OrderShipping) & "</td>" EmailBody &= "</tr>" EmailBody &= "<tr>" EmailBody &= " <td style=""align:right"">" & _ String.Format("{0:C}", OrderAmount) & "</td>" EmailBody &= "</tr>" EmailBody &= "</table>" EmailBody &= "" EmailBody &= "</body>" EmailBody &= "</html>" Email.Body = EmailBody Email.IsBodyHtml = True Client.Send(Email) End If Response.Redirect("SalesOrder.aspx")
After the EmailBody variable accumulates all the appropriate text, database values, and surrounding XHTML code, it is assigned to the Body property of the Email object. The object's format is set for HTML content by setting its IsBodyHtml="True" property. Then the email is sent through SMTP services. Finally, redirection takes place to the SalesOrder.aspx page for the customer to view a summary of this order.
This Page_Load code is all that appears on the OrderCapture.aspx page. It does not produce a page display nor is the customer even aware that the page is loaded. Visually, the customer returns from the credit card processing page directly to the SalesOrder.aspx page. As mentioned above, the OrderCapture.aspx page would be the appropriate place to also take care of other business processing tasks associated with the order, but these other tasks are not part of this example site.