home *** CD-ROM | disk | FTP | other *** search
Wrap
<SCRIPT runat="server" language="JScript"> // ***************************************************************************** // // include/mscommerce.runtime5.asp // // Dynamic Link runtime support for Microsoft Commerce Server // // COPYRIGHT (c) 1999-2000 Adobe Systems Incorporated. All rights reserved. // // These routines are designed to work stand-alone or within existing commerce // sites. The following globals will be used if found: // // Application("SiteName") // Application("DefaultConnectionString") // // Application("MSCSSite") // Application("MSCSQueryMap") // Application("MSCSMessageManager") // Application("MSCSShopperManager") // Application("MSCSSIDURLKey") // Application("MSCSDataFunctions") // // mscsPage // mscsShopperID // mscsOrderFormStorage // mscsOrderForm // mscsPipeContext // if (typeof(mscsPage) == "undefined") { var mscsPage; } if (typeof(mscsShopperID) == "undefined") { var mscsShopperID; } if (typeof(mscsOrderFormStorage) == "undefined") { var mscsOrderFormStorage; } if (typeof(mscsOrderForm) == "undefined") { var mscsOrderForm; } if (typeof(mscsPipeContext) == "undefined") { var mscsPipeContext; } var mscsOrderFormIsNew = false; var fakeOrderForm; if (typeof(Session("FakeShoppingBasket")) == "undefined") { Session("FakeShoppingBasket") = ""; } // ----------------------------------------------------------------------------- // Place a global variable into the application object. function AddToApplication(globalName) { Application.Lock(); Application(globalName) = eval(globalName); Application.Unlock(); } // ----------------------------------------------------------------------------- // Read the site config file into the global MSCSSite dictionary. function ReadSiteDictionary() { var cscPath = Request("APPL_PHYSICAL_PATH") + "/config/site.csc"; var fso = Server.CreateObject("Scripting.FileSystemObject"); if (fso.FileExists(cscPath)) { var FD = Server.CreateObject("Commerce.FileDocument"); FD.ReadDictionaryFromFile(cscPath, "IISProperties", MSCSSite); } else { MSCSSite.CloseRedirectURL = ""; MSCSSite.DefaultConnectionString = ConnectString(Application("DefaultDatabase")); MSCSSite.DisableHTTPS = 1; MSCSSite.DisplayName = Application("SiteName"); MSCSSite.NonsecureHostName = Request("SERVER_NAME") + ":" + Request("SERVER_PORT"); MSCSSite.SecureHostName = ""; MSCSSite.Status = "Open"; } } // ----------------------------------------------------------------------------- // Load some default messages into the global MSCSMessageManager. function LoadMessages() { MSCSMessageManager.AddMessage("pur_out_of_stock", "At least one item is out of stock."); MSCSMessageManager.AddMessage("pur_badsku", "Products in your basket were deleted because they don't exist in this store."); MSCSMessageManager.AddMessage("pur_badplacedprice", "Prices of products in your basket have been updated."); MSCSMessageManager.AddMessage("pur_noitems", "An order must have at least one item."); MSCSMessageManager.AddMessage("pur_badshipping", "Unable to complete order. Cannot compute shipping cost."); MSCSMessageManager.AddMessage("pur_badtax", "Unable to complete order. Cannot compute tax."); MSCSMessageManager.AddMessage("pur_badhandling", "Unable to complete order. Cannot compute handling cost."); MSCSMessageManager.AddMessage("pur_badverify", "Changes to the data require your review. Please review and resubmit."); MSCSMessageManager.AddMessage("pur_badpayment", "There was a problem authorizing your credit. Please verify your payment information or use a different card."); MSCSMessageManager.AddMessage("pur_badcc", "Bad Credit Card Number."); } // ----------------------------------------------------------------------------- // Makes sure all the global-scope MSCS objects are defined. function AssertGlobals() { if (typeof(MSCSSite) == "undefined") { MSCSSite = Application("MSCSSite"); } if (typeof(MSCSSite) == "undefined") { MSCSSite = Server.CreateObject("Commerce.Dictionary"); ReadSiteDictionary(); } if (typeof(MSCSShopperManager) == "undefined") { MSCSShopperManager = Application("MSCSShopperManager"); } if (typeof(MSCSShopperManager) == "undefined") { MSCSShopperManager = Server.CreateObject("Commerce.StandardSManager"); MSCSShopperManager.InitManager(Application("SiteName"), "url"); AddToApplication("MSCSShopperManager"); } if (typeof(MSCSSIDURLKey) == "undefined") { MSCSSIDURLKey = Application("MSCSSIDURLKey"); } if (typeof(MSCSSIDURLKey) == "undefined") { MSCSSIDURLKey = "mscssid"; AddToApplication("MSCSSIDURLKey"); } if (typeof(MSCSQueryMap) == "undefined") { MSCSQueryMap = Application("MSCSQueryMap"); } if (typeof(MSCSQueryMap) == "undefined") { MSCSQueryMap = Server.CreateObject("Commerce.Dictionary"); MSCSQueryMap.productpl = Server.CreateObject("Commerce.Dictionary"); MSCSQueryMap.productpl.SQLCommand = "SELECT sku, name, list_price FROM Products WHERE sku = ?"; AddToApplication("MSCSQueryMap"); } if (typeof(MSCSMessageManager) == "undefined") { MSCSMessageManager = Application("MSCSMessageManager"); } if (typeof(MSCSMessageManager) == "undefined") { MSCSMessageManager = Server.CreateObject("Commerce.MessageManager"); MSCSMessageManager.AddLanguage("usa", 0x409); MSCSMessageManager.defaultLanguage = "usa"; LoadMessages(); AddToApplication("MSCSMessageManager"); } if (typeof(MSCSDataFunctions) == "undefined") { MSCSDataFunctions = Application("MSCSDataFunctions"); } if (typeof(MSCSDataFunctions) == "undefined") { MSCSDataFunctions = Server.CreateObject("Commerce.DataFunctions"); MSCSDataFunctions.Locale = 0x409; AddToApplication("MSCSDataFunctions"); } } // ----------------------------------------------------------------------------- // Add any necessary shopper arguments to a dynamic URL. function mscs_URLArgs() { return queryStringToArray(mscsPage.URLShopperArgs()); } // ----------------------------------------------------------------------------- // Makes sure the page- and session-scope MSCS objects are defined. function AssertPage() { if (typeof(mscsPage) == "undefined") { mscsPage = Server.CreateObject("Commerce.Page"); } if (typeof(mscsShopperID) == "undefined") { mscsShopperID = mscsPage.GetShopperID(); } if (!mscsShopperID) { mscsShopperID = MSCSShopperManager.CreateShopperID(); mscsPage.PutShopperID(mscsShopperID); } } // ----------------------------------------------------------------------------- // Makes sure that an MSCS orderForm exists. function AssertOrderForm() { if (typeof(mscsOrderFormStorage) == "undefined") { mscsOrderFormStorage = Server.CreateObject("Commerce.DBStorage"); mscsOrderFormStorage.InitStorage( MSCSSite.DefaultConnectionString, "ShoppingBaskets", // table that holds data "shopper_id", // table key "Commerce.OrderForm", // object that maps onto stored data "marshalled_order", // field containing opaque data "date_changed" // field containing date of last change ); } if (typeof(mscsOrderForm) == "undefined") { mscsOrderForm = mscsOrderFormStorage.GetData(null, mscsShopperID); if (!mscsOrderForm) { mscsOrderForm = Server.CreateObject("Commerce.OrderForm"); mscsOrderForm.shopper_id = mscsShopperID; mscsOrderFormIsNew = true; } } } // ----------------------------------------------------------------------------- // Commit any changes made to the order form to the database. function UpdateOrderForm() { if (mscsOrderFormIsNew) { mscsOrderFormStorage.InsertData(null, mscsOrderForm); mscsOrderFormIsNew = false; } else { mscsOrderFormStorage.CommitData(null, mscsOrderForm); } } // ----------------------------------------------------------------------------- // Run the order processing pipeline on an order form. function RunPlan() { if (typeof(mscsPipeContext) == "undefined") { mscsPipeContext = Server.CreateObject("Commerce.Dictionary"); mscsPipeContext.MessageManager = MSCSMessageManager; mscsPipeContext.DataFunctions = MSCSDataFunctions; mscsPipeContext.QueryMap = MSCSQueryMap; mscsPipeContext.ConnectionStringMap = MSCSSite.ConnectionStringMap; mscsPipeContext.SiteName = MSCSSite.DisplayName; mscsPipeContext.DefaultConnectionString = MSCSSite.DefaultConnectionString; mscsPipeContext.Language = "usa"; } var pipeline = Server.CreateObject("Commerce.MtsPipeline"); // Comment in the next line for debugging // pipeline.SetLogFile(Request("APPL_PHYSICAL_PATH") + "/config/plan.pcf.log"); pipeline.LoadPipe(Request("APPL_PHYSICAL_PATH") + "/config/plan.pcf"); pipeline.Execute(1, mscsOrderForm, mscsPipeContext, 0); UpdateOrderForm(); } // ----------------------------------------------------------------------------- // Returns the current OrderForm wrapped as a Dynamic Link content source. function GetOrderForm(db) { if (UseFakeCommerce()) { fakeOrderForm = new CSWFakeOrderForm(db, "Products", "SKU", "list_price"); return fakeOrderForm; } AssertOrderForm(); RunPlan(); return new CSWOrderForm(mscsOrderForm); } // ***************************************************************************** // CONTENT SOURCE WRAPPERS: ORDERFORM and ORDERFORM ITEMS // // Items are accessed through the wrappers as follows: // // set orderForm = WrapOrderForm(mscsOrderForm) // orderForm.Value("ship_to_name") // orderForm.items.MoveFirst // orderForm.items.Value("SKU") // ----------------------------------------------------------------------------- // Content source wrapper for OrderForm. function WrapOrderForm(mscsOrderForm) { return new CSWOrderForm(mscsOrderForm); } function CSWOrderForm(mscsOrderForm) { // Method table: this.Move = CSW_NOP; this.MoveFirst = CSW_NOP; this.MoveNext = CSW_NOP; this.Value = CSWOrderForm_Value; this.Set = CSWOrderForm_Set; this.UpdateBatch = UpdateOrderForm; this.Data = mscsOrderForm; this.items = new CSWDictionaryList(mscsOrderForm.items); this.items.Key = CSWOrderFormItems_Key; this.items.UpdateBatch = UpdateOrderForm; this._Basket_Errors = new CSWVariantList(mscsOrderForm._Basket_Errors); this._Basket_Errors.UpdateBatch = UpdateOrderForm; this._Purchase_Errors = new CSWVariantList(mscsOrderForm._Purchase_Errors); this._Purchase_Errors.UpdateBatch = UpdateOrderForm; this.RecordCount = 1; this.AbsolutePosition = 1; this.EOF = true; } // ----------------------------------------------------------------------------- // Data access: function CSWOrderForm_Value(fieldName) { return this.Data.Value( fieldName ); } function CSWOrderForm_Set(fieldName, value) { this.Data.Value(fieldName) = value; } // Most of the functionality of the orderForm's item list is provided by the // DictionaryList CSW. We add these two methods to automate forms processing // using the SKU as the key. function CSWOrderFormItems_Key() { return "SKU=" + this.Value("SKU"); } // ***************************************************************************** // CONTENT SOURCE WRAPPERS: SIMPLELIST AND DICTIONARY // ----------------------------------------------------------------------------- // Content source wrapper for a Commerce.SimpleList of Commerce.Dictionary's. // We don't wrap SimpleLists directly 'cause then we'd have to wrap each field // in the dictionary. function WrapDictionaryList(mscsList) { return new CSWDictionaryList(mscsList); } function CSWDictionaryList(mscsList) { // Method table: this.Move = CSW_Move; this.MoveFirst = CSW_MoveFirst; this.MoveNext = CSW_MoveNext; this.Value = CSWDictionaryList_Value; this.Set = CSWDictionaryList_Set; this.UpdateBatch = CSW_NOP; this.Data = mscsList; this.RecordCount = this.Data.Count; this.MoveFirst(); } // ----------------------------------------------------------------------------- // Data access: function CSWDictionaryList_Value(fieldName) { return this.Data( this.AbsolutePosition - 1 ).Value( fieldName ); } function CSWDictionaryList_Set(fieldName, value) { this.Data(this.AbsolutePosition-1).Value(fieldName) = value; } // ----------------------------------------------------------------------------- // Content source wrapper for a Commerce.SimpleList of Variant's. function WrapVariantList(mscsList) { return new CSWVariantList(mscsList); } function CSWVariantList(mscsList) { // Method table: this.Move = CSW_Move; this.MoveFirst = CSW_MoveFirst; this.MoveNext = CSW_MoveNext; this.Value = CSWVariantList_Value; this.Set = CSWVariantList_Set; this.UpdateBatch = CSW_NOP; this.Data = mscsList; this.RecordCount = this.Data.Count; this.MoveFirst(); } // ----------------------------------------------------------------------------- // Data access: function CSWVariantList_Value() { return this.Data( this.AbsolutePosition - 1 ); } function CSWVariantList_Set(value) { this.Data(this.AbsolutePosition-1) = value; } // ***************************************************************************** // CONTENT SOURCE WRAPPERS: FAKE ECOMMERCE // // Simulate a commerce server. Shopping basket information is stored in // the session object. Prices and other item information are pulled // from a database. function UseFakeCommerce() { return typeof(Application("UseMSCommerceServer")) == "undefined" || Application("UseMSCommerceServer") == false; } function CSWFakeOrderForm(db, table, sku, price) { this.Init = CSWFakeOrderForm_Init; this.GetProductInfo = CSWFakeOrderForm_GetProductInfo; this.Init(db, table, sku, price); // wrapper API methods this.Move = CSW_NOP; this.MoveFirst = CSW_NOP; this.MoveNext = CSW_NOP; this.Value = CSWFakeOrderForm_Value; this.Set = CSW_NOP; this.UpdateBatch = CSWFakeOrderForm_Save; this.RecordCount = 1; this.AbsolutePosition = 1; this.EOF = true; // wrapper API methods this.items.Move = CSW_Move; this.items.MoveFirst = CSW_MoveFirst; this.items.MoveNext = CSW_MoveNext; this.items.Value = CSWFakeItemList_Value; this.items.Set = CSW_NOP; this.items.UpdateBatch = CSWFakeItemList_Save; this.items.Key = CSWOrderFormItems_Key; this.items.RecordCount = this.items.data.length; this.items.MoveFirst(); } // ----------------------------------------------------------------------------- // Persistence for fake shopping baskets. Basket information is stored in // the session data as a string of the form "sku1,qty1,...,skuN,qtyN". In the // running object it is stored as a list of lists. function CSWFakeOrderForm_Init(db, table, sku, price) { var storedData = Session("FakeShoppingBasket").split(","); var itemList = new Array(); for (var i = 0; i < Math.floor(storedData.length / 2); i++) { itemList[i] = new Object(); itemList[i].SKU = storedData[2*i]; itemList[i].Quantity = storedData[2*i + 1]; } this.items = new Object(); this.items.data = itemList; this.GetProductInfo(db, table, sku, price); this._total_total = 0; for (var i = 0; i < this.items.data.length; i++) { this.items.data[i]._iadjust_currentprice = this.items.data[i]._product_list_price; this.items.data[i]._oadjust_adjustedprice = this.items.data[i].Quantity * this.items.data[i]._iadjust_currentprice; this._total_total += this.items.data[i]._oadjust_adjustedprice; } } function CSWFakeOrderForm_Save() { this.items.UpdateBatch(); } function CSWFakeItemList_Save() { var itemList = this.data; var storedData = new Array(); var j = 0; for (var i = 0; i < itemList.length; i++) { if (!itemList[i].deleted) { storedData[2*j] = itemList[i].SKU; storedData[2*j + 1] = itemList[i].Quantity; j++; } } Session("FakeShoppingBasket") = storedData.join(","); } // ----------------------------------------------------------------------------- // Get product information from the database. function CSWFakeOrderForm_GetProductInfo(db, table, sku, price) { if ((this.items.data.length == 0) || (String(db).length == 0) || (String(table).length == 0) || (String(sku).length == 0) || (String(price).length == 0)) { return; } var recordSet = new ActiveXObject("ADODB.RecordSet"); recordSet.Open("select * from " + table, ConnectString(db)); for (var i = 0; i < this.items.data.length; i++) { recordSet.MoveFirst(); while (!recordSet.EOF) { if (recordSet(sku) != this.items.data[i].SKU) { recordSet.MoveNext(); continue; } for (var j = 0; j < recordSet.Fields.Count; j++) { var name = recordSet.Fields(j).Name; if (name == price) name = "list_price"; this.items.data[i]["_product_" + name] = recordSet.Fields(j).Value; } break; } } } // ----------------------------------------------------------------------------- // Retrieve values: function CSWFakeOrderForm_Value(fieldName) { return this[fieldName]; } function CSWFakeItemList_Value(fieldName) { return this.data[this.AbsolutePosition - 1][fieldName]; } // ***************************************************************************** // INITIALIZATION function initMSCS() { if (UseFakeCommerce()) { return; } AssertGlobals(); AssertPage(); RegisterURLArgsFunction(mscs_URLArgs); } // Note: since we don't know the source page language, the following must // be language neutral: </SCRIPT> <% initMSCS() %>