Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 9 pptx

70 357 0
Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 9 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER 14 ■ ORDER PIPELINE 539 public void SetAuthCodeAndReference(string authCode, string reference) { // call static method CommerceLibAccess.SetOrderAuthCodeAndReference(OrderID, authCode, reference); // update fields AuthCode = authCode; Reference = reference; } public void SetDateShipped() { // call static method CommerceLibAccess.SetOrderDateShipped(OrderID); // update field DateShipped = DateTime.Now.ToString(); } Summary You’ve started to build the backbone of the application and prepared it for the lion’s share of the order pipeline processing functionality, which you’ll implement in Chapter 15. Specifically, we’ve covered the following: • The basic framework for your order pipeline • The database additions for auditing data and storing additional required data in the Orders table • How to put orders into the pipeline when they are placed in BalloonShop In the next chapter, you’ll go on to implement the order pipeline. Darie-Watson_4681C14.fm Page 539 Friday, September 16, 2005 5:26 AM Darie-Watson_4681C14.fm Page 540 Friday, September 16, 2005 5:26 AM 541 ■ ■ ■ CHAPTER 15 Implementing the Pipeline In the last chapter, you completed the basic functionality of the OrderProcessor component, which is responsible for moving orders through pipeline stages. You’ve seen a quick demon- stration of this using a dummy pipeline section, but you haven’t yet implemented the pipeline discussed at the beginning of the last chapter. In this chapter, you’ll add the required pipeline sections so that you can process orders from start to finish, although you won’t be adding full credit card transaction functionality until the next chapter. We’ll also look at the web administration of orders by modifying the order admin pages added earlier in the book to take into account the new order-processing system. Considering the Code for the Pipeline Sections The OrderProcessor code is complete, except for one important section—the pipeline stage selection. Rather than forcing the processor to use PSDummy, you actually want to select one of the pipeline stages outlined in Chapter 14, depending on the status of the order. Before you do this, let’s run through the code for each of the pipeline sections in turn, and some new utility code, which will take you to the point where the order pipeline is complete apart from actual credit card authorization. Business Tier Modifications The first thing we’ll look at in this section is some modifications to the OrderProcessorMailer class that are required for pipeline sections to send mail to customers and suppliers. After that, we’ll move on to the pipeline sections; each section requires a new class in the App_Code/ CommerceLib folder. (Remember that this code is available in the Source Code area of the Apress web site at http://www.apress.com). By the time you get to the next “Exercise” section, you should have eight new classes with the following names (they all start with PS, short for Pipe- line Section): • PSInitialNotification • PSCheckFunds • PSCheckStock • PSStockOK Darie-Watson_4681C15.fm Page 541 Friday, September 16, 2005 10:36 AM 542 CHAPTER 15 ■ IMPLEMENTING THE PIPELINE • PSTakePayment • PSShipGoods • PSShipOK • PSFinalNotification We’ll discuss the classes you are creating as you go. OrderProcessorMailer Modifications The OrderProcessorMailer class needs two new methods, MailCustomer and MailSupplier. The new methods to add to OrderProcessorMailer are as follows: public static void MailCustomer(MembershipUser customer, string subject, string body) { // Send mail to customer string to = customer.Email; string from = BalloonShopConfiguration.CustomerServiceEmail; Utilities.SendMail(from, to, subject, body); } public static void MailSupplier(string subject, string body) { // Send mail to supplier string to = BalloonShopConfiguration.SupplierEmail; string from = BalloonShopConfiguration.OrderProcessorEmail; Utilities.SendMail(from, to, subject, body); } These methods use properties from BalloonShopConfiguration and the Utilities. SendMail method to send mail to customers and suppliers. OrderProcessor Modifications As with MailAdmin, we’ll provide some new methods in OrderProcessor for mailing so that pipe- line sections don’t use OrderProcessorMailer directly. The code for this is simply two new methods as follows: public void MailCustomer(string subject, string message) { OrderProcessorMailer.MailCustomer(Order.Customer, subject, message); } public void MailSupplier(string subject, string message) { OrderProcessorMailer.MailSupplier(subject, message); } Darie-Watson_4681C15.fm Page 542 Friday, September 16, 2005 10:36 AM CHAPTER 15 ■ IMPLEMENTING THE PIPELINE 543 Doing this is really according to personal taste. It wouldn’t really matter if order pipeline sections used OrderProcessorMailer methods, although in the case of MailCustomer it does simplify the syntax slightly. ThePSInitialNotification Class PSInitialNotification is the first pipeline stage and is responsible for sending an email to the customer confirming that the order has been placed. The code for this class starts off in what will soon become a very familiar fashion (the autogenerated using statements aren’t shown here for brevity): using System.Text; namespace CommerceLib { /// <summary> /// 1st pipeline stage - used to send a notification email to /// the customer, confirming that the order has been received /// </summary> public class PSInitialNotification : IPipelineSection { private OrderProcessor orderProcessor; public void Process(OrderProcessor processor) { // set processor reference orderProcessor = processor; // audit orderProcessor.CreateAudit("PSInitialNotification started.", 20000); The code contains one additional using statement; the class itself, which implements the IPipelineSection interface; a private field for storing a reference to the order processor; and then the IPipelineSection.Process method implementation. This method starts by storing the reference to OrderProcessor, which all the pipeline sections will do because using the members it exposes (either in the Process method or in other methods) is essential. An audit entry is also added using the numbering scheme introduced earlier (the initial 2 signifies that it’s coming from a pipeline section, the next 00 means that it’s the first pipeline section, and the final 00 shows that it’s the start message for the pipeline section). The remainder of the Process method sends the email, using the MailCustomer method of OrderProcessor. A private method, GetMailBody(), is used to build a message body, which we’ll look at shortly: try { // send mail to customer orderProcessor.MailCustomer("BalloonShop order received.", GetMailBody()); Darie-Watson_4681C15.fm Page 543 Friday, September 16, 2005 10:36 AM 544 CHAPTER 15 ■ IMPLEMENTING THE PIPELINE After the mail is sent, you add an audit message to change the status of the order and tell the order processor that it’s okay to move straight on to the next pipeline section: // audit orderProcessor.CreateAudit( "Notification e-mail sent to customer.", 20002); // update order status orderProcessor.Order.UpdateStatus(1); // continue processing orderProcessor.ContinueNow = true; } If an error occurs, an OrderProcessor exception is thrown: catch { // mail sending failure throw new OrderProcessorException( "Unable to send e-mail to customer.", 0); } If all goes according to plan, the Process method finishes by adding a final audit entry: // audit processor.CreateAudit("PSInitialNotification finished.", 20001); } The GetMailBody method is used to build up an email body to send to the customer using a StringBuilder object for efficiency. The text uses customer and order data, but follows a generally accepted e-commerce email format: private string GetMailBody() { // construct message body StringBuilder sb = new StringBuilder(); sb.Append("Thank you for your order! The products you have " + "ordered are as follows:\n\n"); sb.Append(orderProcessor.Order.OrderAsString); sb.Append("\n\nYour order will be shipped to:\n\n"); sb.Append(orderProcessor.Order.CustomerAddressAsString); sb.Append("\n\nOrder reference number:\n\n"); sb.Append(orderProcessor.Order.OrderID.ToString()); sb.Append( "\n\nYou will receive a confirmation e-mail when this " + "order has been dispatched. Thank you for shopping " + "at BalloonShop!"); return sb.ToString(); } } } Darie-Watson_4681C15.fm Page 544 Friday, September 16, 2005 10:36 AM CHAPTER 15 ■ IMPLEMENTING THE PIPELINE 545 When this pipeline stage finishes, processing moves straight on to PSCheckFunds. The PSCheckFunds Class The PSCheckFunds pipeline stage is responsible for making sure that the customer has the required funds available on a credit card. For now, you’ll provide a dummy implementation of this, and just assume that these funds are available. The code starts in the same way as PSInitialNotification, although without the reference to the System.Text namespace: namespace CommerceLib { /// <summary> /// 2nd pipeline stage - used to check that the customer /// has the required funds available for purchase /// </summary> public class PSCheckFunds : IPipelineSection { private OrderProcessor orderProcessor; public void Process(OrderProcessor processor) { // set processor reference orderProcessor = processor; // audit orderProcessor.CreateAudit("PSCheckFunds started.", 20100); Even though you aren’t actually performing a check, you set the authorization and reference codes for the transaction to make sure that the code in OrderProcessor works properly: try { // check customer funds // assume they exist for now // set order authorization code and reference orderProcessor.Order.SetAuthCodeAndReference("AuthCode", "Reference"); You finish up with some auditing, the code required for continuation, and error checking: // audit orderProcessor.CreateAudit("Funds available for purchase.", 20102); // update order status orderProcessor.Order.UpdateStatus(2); // continue processing orderProcessor.ContinueNow = true; } Darie-Watson_4681C15.fm Page 545 Friday, September 16, 2005 10:36 AM 8213592a117456a340854d18cee57603 546 CHAPTER 15 ■ IMPLEMENTING THE PIPELINE catch { // fund checking failure throw new OrderProcessorException( "Error occured while checking funds.", 1); } // audit processor.CreateAudit("PSCheckFunds finished.", 20101); } } } When this pipeline stage finishes, processing moves straight on to PSCheckStock. The PSCheckStock Class The PSCheckStock pipeline stage sends an email instructing the supplier to check stock availability: using System.Text; namespace CommerceLib { /// <summary> /// 3rd pipeline stage – used to send a notification email to /// the supplier, asking whether goods are available /// </summary> public class PSCheckStock : IPipelineSection { private OrderProcessor orderProcessor; public void Process(OrderProcessor processor) { // set processor reference orderProcessor = processor; // audit orderProcessor.CreateAudit("PSCheckStock started.", 20200); Mail is sent in a similar way to PSInitialNotification, using a private method to build up the body. This time, however, we use MailSupplier: try { // send mail to supplier orderProcessor.MailSupplier("BalloonShop stock check.", GetMailBody()); As before, you finish by auditing and updating the status, although this time you don’t tell the order processor to continue straight away: Darie-Watson_4681C15.fm Page 546 Friday, September 16, 2005 10:36 AM CHAPTER 15 ■ IMPLEMENTING THE PIPELINE 547 // audit orderProcessor.CreateAudit( "Notification e-mail sent to supplier.", 20202); // update order status orderProcessor.Order.UpdateStatus(3); } catch { // mail sending failure throw new OrderProcessorException( "Unable to send e-mail to supplier.", 2); } // audit processor.CreateAudit("PSCheckStock finished.", 20201); } The code for building the message body is simple; it just lists the items in the order and tells the supplier to confirm via the BalloonShop web site (using the order administration page OrdersAdmin.aspx, which you’ll modify later): private string GetMailBody() { // construct message body StringBuilder sb = new StringBuilder(); sb.Append("The following goods have been ordered:\n\n"); sb.Append(orderProcessor.Order.OrderAsString); sb.Append("\n\nPlease check availability and confirm via "); sb.Append("http://balloonshop.apress.com/OrdersAdmin.aspx"); sb.Append("\n\nOrder reference number:\n\n"); sb.Append(orderProcessor.Order.OrderID.ToString()); return sb.ToString(); } } } Note that the URL used here isn’t a real one—you should replace it with a URL of your own. When this pipeline stage finishes, processing pauses. Later, when the supplier confirms that stock is available, processing moves on to PSStockOK. The PSStockOK Class The PSStockOK pipeline section doesn’t do much at all. It just confirms that the supplier has the product in stock and moves on. Its real purpose is to look for orders that have a status corre- sponding to this pipeline section and know that they are currently awaiting stock confirmation. Darie-Watson_4681C15.fm Page 547 Friday, September 16, 2005 10:36 AM 548 CHAPTER 15 ■ IMPLEMENTING THE PIPELINE namespace CommerceLib { /// <summary> /// Summary description for PSStockOK /// </summary> public class PSStockOK : IPipelineSection { private OrderProcessor orderProcessor; public void Process(OrderProcessor processor) { // set processor reference orderProcessor = processor; // audit orderProcessor.CreateAudit("PSStockOK started.", 20300); // the method is called when the supplier confirms that stock is // available, so we don't have to do anything here except audit orderProcessor.CreateAudit("Stock confirmed by supplier.", 20302); // update order status orderProcessor.Order.UpdateStatus(4); // continue processing orderProcessor.ContinueNow = true; // audit processor.CreateAudit("PSStockOK finished.", 20301); } } } When this pipeline stage finishes, processing moves straight on to PSTakePayment. The PSTakePayment Class The PSTakePayment pipeline section completes the transaction started by PSCheckFunds. As with that section, you only provide a dummy implementation here. namespace CommerceLib { /// <summary> /// 5th pipeline stage – takes funds from customer /// </summary> public class PSTakePayment : IPipelineSection { private OrderProcessor orderProcessor; Darie-Watson_4681C15.fm Page 548 Friday, September 16, 2005 10:36 AM [...]... method on the order being viewed The other modifications to the code were to enable or disable the processing button depending on whether an order is being viewed One interesting point to note is what happens if you continue to try to process an order after it has been completed The result of clicking the processing button again is shown in Figure 15-2 8213 592 a117456a340854d18cee57603 5 59 Darie-Watson_4681C15.fm... OrderTest.aspx page by clicking the button again, calling OrderProcessor.Process for the second time 8 Check your mail for the ship goods email: Payment has been received for the following goods: 1 Love Rose, $12 .99 each, total cost $12 .99 1 Love Cascade Hearts, $12 .99 each, total cost $12 .99 Shipping: By air (10 days, $35) Total order cost: $60 .98 Please ship to: Charles Darwin 2nd Hut On The Left The Beach... stock check email (as shown next) The following goods have been ordered: 1 Love Rose, $12 .99 each, total cost $12 .99 1 Love Cascade Hearts, $12 .99 each, total cost $12 .99 Shipping: By air (10 days, $35) Total order cost: $60 .98 Please check availability and confirm via http://balloonshop.apress.com/OrdersAdmin.aspx Order reference number: 11 7 Continue processing on the OrderTest.aspx page by clicking... September 16, 2005 10:36 AM CHAPTER 15 ■ IMPLEMENTING THE PIPELINE Business Tier Modifications The modifications in this section apply mainly to the CommerceLibAccess and CommerceLib➥ OrderInfo classes However, to give easy access to audit trail info, you also need to add a new class, called (surprisingly enough) CommerceLibAuditInfo Apart from allowing access to your new data, you have one more thing to add,... You need to provide a human-readable version of order statuses, which you’ll use later to display information in the order admin page Adding Human-Readable Status Information You can store order status strings in any number of places One option is to store them in the database, perhaps in a table called Status You could then link to the Orders table by matching the existing Orders.Status field to, say,... DBNull.Value && orderRow["ShippingType"] != DBNull.Value && orderRow["ShippingCost"] != DBNull.Value) { shipping.ShippingID = Int32.Parse(orderRow["ShippingID"].ToString()); shipping.ShippingType = orderRow["ShippingType"].ToString(); shipping.ShippingCost = double.Parse(orderRow["ShippingCost"].ToString()); } else { shipping.ShippingID = -1; } // Get Tax Data if (orderRow["TaxID"] != DBNull.Value && orderRow["TaxType"]... new stored procedures Moving on to the business tier, you’ll need to update several methods and add several more to give access to the new stored procedures and allow the presentation tier to obtain and update data Finally, in the presentation tier, you’ll need to update the files responsible for order administration Note that your new data structures mean that customer-related information, including... The following products have been shipped: 1 Love Rose, $12 .99 each, total cost $12 .99 1 Love Cascade Hearts, $12 .99 each, total cost $12 .99 Shipping: By air (10 days, $35) Total order cost: $60 .98 Your order has been shipped to: Charles Darwin 2nd Hut On The Left The Beach The Island Santa Cruz The Galapagos Islands Order reference number: 11 Thank you for shopping at BalloonShop! 11 Examine the new... the customer name, email address, and postal address, are no longer editable by administrators It would be possible to do this, but because customers can edit that information themselves, we won’t implement it here In fact, at some point, it might be nice to add a customer administration page, usable by administrators, to check on customer activity and edit customer accounts We’ll leave this task to you... existing order by its ID, and then click on the button to process the first phase of the order 5 Check your (customer account) mail for the customer notification email An example is shown here: Thank you for your order! The products you have ordered are as follows: 1 Love Rose, $12 .99 each, total cost $12 .99 1 Love Cascade Hearts, $12 .99 each, total cost $12 .99 Shipping: By air (10 days, $35) Total . $ 12. 99 each, total cost $ 12. 99 1 Love Cascade Hearts, $ 12. 99 each, total cost $ 12. 99 Shipping: By air ( 10 days, $35) Total order cost: $ 60. 98 Your order will be shipped to: Charles Darwin 2nd. following goods: 1 Love Rose, $ 12. 99 each, total cost $ 12. 99 1 Love Cascade Hearts, $ 12. 99 each, total cost $ 12. 99 Shipping: By air ( 10 days, $35) Total order cost: $ 60. 98 Please ship to: Charles. following goods have been ordered: 1 Love Rose, $ 12. 99 each, total cost $ 12. 99 1 Love Cascade Hearts, $ 12. 99 each, total cost $ 12. 99 Shipping: By air ( 10 days, $35) Total order cost: $ 60. 98 Please

Ngày đăng: 09/08/2014, 14:20

Từ khóa liên quan

Mục lục

  • Beginning ASP.NET 2.0 E-Commerce in C# 2005: From Novice to Professional

    • Chapter 15 Implementing the Pipeline

    • Chapter 16 Credit Card Transactions

Tài liệu cùng người dùng

Tài liệu liên quan