I l @ ve RuBoard |
With the beans in place to record orders and authorize credit cards, you can write the JSP to run the credit card and complete the order (see Listing 13.9). Listing 13.9 authorizePurchase.jsp<%@ include file="/jsp/cust/AutoLogin.jsp" %> <%@ page import="com.bfg.product.Product" %> <%@ page import="com.bfg.product.Order" %> <%@ page import="java.util.Iterator" %> <%@ page import="com.bfg.cart.CartItem" %> <jsp:useBean id="orderaddr" class="com.bfg.customer.Address" scope="session"/> <jsp:useBean id="ordercredit" class="com.bfg.customer.CreditCard" scope="session"/> <jsp:useBean id="cart" class="com.bfg.cart.Cart" scope="session"/> <jsp:useBean id="customer" class="com.bfg.customer.Customer" scope="session"/> <% if (!cart.authorizeCharge(ordercredit)) { response.sendRedirect("authorizationFailed.jsp"); return; } Order order = new Order(customer, orderaddr, ordercredit, cart); order.recordOrder(); order.emailReceipt(customer.getEmail()); cart.clear(); pageContext.setAttribute("orderaddr", null, PageContext.SESSION_SCOPE); pageContext.setAttribute("ordercredit", null, PageContext.SESSION_SCOPE); %> <HEAD> <TITLE>Order Receipt</TITLE> </HEAD> <BODY> <%@ include file="/jsp/includes/bfgheader.jsp" %> <CENTER><H1>Order Receipt</H1></CENTER> Thank you for your order, your credit card has been charged <%= order.getOrderTotalString() %>. Your order number is <%= order.getOrderNumber() %>. Please write down this order number in case you need to refer to the order at a future date. You will also be receiving a copy of your receipt via e-Mail. <%@ include file="/jsp/includes/bfgfooter.jsp" %> After the normal heading items, the page runs the authorization call to see if a successful result is returned. If not, it redirects to a "We Had a Problem with Your Order" page (which is so much more polite than "You're a deadbeat, get lost"). Assuming that your customer has money in the bank, a new order object is created from all of its component elements and is recorded. Then you can have the page send the nice customer a receipt via e-mail. Finally, you need to have the page clear out the cart contents and make sure that the order information isn't left in the session, where it might possibly end up bleeding through somewhere that it shouldn't. This last step might seem overcautious, but I've seen real-life examples of platform failures that caused one person's order information to leak into another session. It's just good common sense to purge the cart as soon as it is no longer needed. If you go to the site and give final confirmation to an order now, you'll see the screen shown in Figure 13.1. Figure 13.1. The order receipt page.
Confirmation will also result in the following e-mail being sent: Return-Path: <turner@blackbear.com> Received: from JAMES (james [192.168.1.10]) by linux.blackbear.com (8.11.6/8.11.6) with ESMTP id fAJ4n0H19225 for <turner@blackbear.com>; Sun, 18 Nov 2001 23:49:01 -0500 Date: Sun, 18 Nov 2001 23:49:01 -0500 Message-ID: <4544380.1006145340846.JavaMail.james@JAMES> From: turner@blackbear.com To: turner@blackbear.com Subject: Receipt for order 8 Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit Status: Thank you for shopping at Books for Geeks. Here is the receipt for your order 8 placed on 2001-11-18 CODE TITLE QUANT PRICE TOTAL ================================================================ 672320959 Java 2 Micro Edition (J2ME) Ap 4 .99 9.96 PALMCASE Genuine Nagahyde PDA Carrying 1 .00 $ 0.00 672321173 Oracle and Java Development 5 .99 9.95
|
I l @ ve RuBoard |