asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Credit Card Validation with Site Server Commerce
By Dina Berry
Rating: 2.7 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Site Server Commerce

    The Site Server Commerce product is a set of tools and code that allows you to sell products or services on the Web. Site Server Commerce is not an out-of-the-box solution. Even though it will build a site for you in 14 easy steps, you will probably want to alter the code to fit your needs. This article explains how to add simple credit card validation to your site using the tools provided with the Site Server Commerce software.

    Requirements

    To use the code in this article you need a copy of Site Server Commerce and the Site Server Commerce SDK version 1.1. The SDK is available at http://backoffice.microsoft.com/downtrial/moreinfo/siteserversdk.asp. I’ll explain all concepts that are related to Site Server Commerce above and beyond the normal ASP/IIS 4.0 skills.

    Understanding the Orderform Object

    During a customer’s visit to a commerce site, you will want to keep an orderform of who they are, what they have purchased, how they are going to pay for it, etc. This information is kept in a standard Dictionary object (introduced in IIS 4.0) referred to as the OrderForm (progid of "Commerce.OrderForm"). A dictionary object can be thought of as a two-dimensional array of name/value pairs. The Dictionary object can be treated as a collection so that the .item and .count properties are available. The value can be any object, including another Dictionary object. For more information on dictionary objects, please refer to WebBuilder’s article “Make Dictionaries Work For You”, October 1998.

    The customer information and the product information are treated differently in the orderform. The customer information is a regular name/value pair such as “ship_to_name” with a value of “Dina Berry”. Since there can be many products, the object in the orderform for the “items” is a Simple List. A Simple List is a one-dimensional array of Dictionary objects. Each array item is a Dictionary object that contains all the information about the product. To validate a credit card, the code needs to create an orderform but does not need to create the Simple List or subsequent Dictionary objects for the products.

    Most of the code to run a pipeline is available when you create a site using the Site Creation Wizard. To run a pipeline you need the global.asa and the i_util.asp files that are in the root of your commerce site. The global.asa creates most of the commerce objects you will need. The i_util.asp has all the functions you need in order to use a pipeline.

    The validatecc_test.asp file is a short form to enter the credit card type, number, expiration month, and expiration year. Once your have filled out the information and submitted the HTTP form, the orderform object is created. The orderform object is then passed to the pipeline. The result of the pipeline is either an error that the card couldn’t be validated or no error at all. If there is an error, the text of the error is displayed at the top of the page.

    Figure 1: The Web Form for Credit Cards

    The code to produce this page is in Listing 1. The page contains a form and the call to the function “ValidateCreditCard” to validate the form information. This function creates an orderform and executes the pipeline. But first we need to create the pipeline file itself.

    Listing 1 - Credit card validation form

    
    <%@ Language=VBScript %>
    <!-- #include file="i_util.asp" -->
    <%
    
    Dim sCardType
    Dim sCardNumber
    Dim sExpMonth
    Dim sExpYear
    
    sCardType = Request("cctype")
    sCardNumber = Request("ccnumber") 
    sExpMonth = Request("ccexpmonth") 
    sExpYear = Request("ccexpyear")
    
    if Request("submit") <> "" then
    	sMessage = ValidateCreditCard()
    end if
    
    %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    </HEAD>
    <BODY>
    Message = "<% = sMessage %>"
    <p>
    
    <form action="validatecc_test.asp" method="post">
    <table>
    	<tr>
    		<td>
    			card number
    		</td>		
    		<td>
    			<input name="ccnumber">
    		</td>		
    	</tr>
    	<tr>
    		<td>
    			card type
    		</td>		
    		<td>
    			<SELECT id=CardType_ID name="cctype">
    				<option value="American Express">American Express
    				<option value="Discover">Discover
    				<option value="Mastercard">Mastercard
    				<option value="Visa">Visa
    			</SELECT>
    		</td>		
    	</tr>
    	<tr>
    		<td>
    			expiration month
    		</td>		
    		<td>
    			<SELECT id=ExpMonth_ID name="ccexpmonth">
    			<!-- #include file="e_month.asp" -->
    			</SELECT>
    		</td>		
    	</tr>
    	<tr>
    		<td>
    			expiration year
    		</td>		
    		<td>
    			<SELECT id=ExpYear_ID name="ccexpyear">
    			<!-- #include file="e_year.asp" -->
    			</SELECT>
    		</td>		
    	</tr>
    </table>
    <input type="submit" name="submit" value="submit">
    </form>
    
    </BODY>
    </HTML>
    
    
    
    To use the credit card validation component you need to create a pipeline file. When you open the pipeline editor and choose New, you will have several choices of pipeline templates. For this article, I chose the Blank pipeline. This pipeline opens up with nothing in it. I selected the pipe and inserted a “stage” (from the right-click options). You can think of a stage as a single transaction. The stage can have as many components as you want but it is best to have the stage represent some meaningful single action. Since I just want to validate a credit card, the stage will have a single component.

    To insert the component, I chose “Insert Component” from the right-click choices on the stage. Figure 2 shows the list of component choices that are available to all stages. The “ValidateCCNumber” is the component I chose.

    Figure 2: Components Available to All Stages

    The Credit Card Validation component requires the following orderform fields to be filled in for the component to have enough information to validate the credit card: Order.cc_type, Order._cc_number, Order._cc_expmonth, Order._cc_expyear. The cc_type has to be the name of the credit card type such as “American Express”. The number should be a valid number for the specified credit card type. The month and year should also be numeric.

    The component that you are planning on using will have a list of required and optional fields from the orderform. You use the pipeline editor in order to view the component’s field list. Figure 3 shows the pipeline open, with the Credit Card Validation component’s properties available. This figure shows three stages but for now, you only need to focus on the middle stage named ValidateCCNumber. The first and last stages are for debugging, which I’ll get to later.

    Figure 3: The Order Form Fields

    Now that the pipeline is created with a single stage, you need to save the file into the “/Config” subdirectory in your commerce web site. The file extension should be “*.pcf”. A copy of the pipeline used for this article is available.

    The last piece is the code for the ValidateCreditCard function shown in Listing 2. The function returns the error from the execution of the pipeline or nothing at all (meaning the credit card information passed the validation). This code only inserts the required READ properties for the credit card validation component, but it could also have been an orderform with more information including the product list. The orderform variable is set to a commerce orderform. Then the four required properties are filled in. The brackets around the orderform names are required because of the leading underscore in the name. This is a VBScript issue and not a commerce server issue. The UtilGetPipeContext is a function available from i_util.asp. Don’t change the function, just call it. The next call is the UtilRunPipe which is another function from i_util.asp. The first parameter is the pipeline file “validatecc.pcf”. You don’t need to specify the “/Config” directory but the file does need to be in that directory. The second parameter is the orderform object that was just created and filled in. The last param is the pipeline context created from the previous call. The component will place any error messages in the “_Purchase_Errors” Dictionary object of the orderform. You can see that this is a written value from Figure 3. Since the “_Purchase_Errors is a Dictionary object, the code should check to a count of errors to determine if an error occurred. The code returns an empty string if there are no errors.

    Listing 2 – ValidateCreditCard function

    
    '------------------------------------------------------------------------------	
    Function ValidateCreditCard()
    	
    	sReturn = ""
    
    	'create dictionary object orderform
    	'only add required fields
    	
    	set orderForm = Server.CreateObject("Commerce.OrderForm")
    	
    	orderForm.[cc_type] = sCardType
    	orderForm.[_cc_number] = sCardNumber
    	orderForm.[_cc_expmonth] = sExpMonth
    	orderForm.[_cc_expyear] = sExpYear
     	
    	REM -- Create the pipe context
    
        set mscsPipeContext = UtilGetPipeContext()
    
        REM -- Run the plan
        errorLevel = UtilRunPipe("validatecc.pcf", orderForm, mscsPipeContext)
    	
    	if orderForm.[_Purchase_Errors].Count > 0 then
    		sReturn = orderForm.[_Purchase_Errors](0)
        end if
    	
    	ValidateCreditCard = sReturn
    	
    End Function
    
    
    If errors are found, a string is placed in the first element of the “_Purchase_Errors” Dictionary object. You can determine the actual error string that has been returned. The InitMessageManager function in the global.asa fills in the message map with strings for various errors. This particular error is in the "pur_badcc" variable. The line in the global.asa would look like Listing 3. The message manager is meant to be a single location for your commerce application strings. If you want to translate you Web site into a different language, you only need to hand one file to the localization team.

    Listing 3 – Error message for a bad credit card number

    
    call 	MSCSMessageManager.AddMessage("pur_badcc", &_
    "The credit-card number you provided is not valid.  " &_
    "Please verify your payment information or use a " &_
    "different card.")
    
    
    If the credit card number is not validated, the error message will show at the top of the ASP page (shown in Figure 4). Of course, once you have the error string, you can do anything that you want to with it. This is just an example.

    Figure 4: Web Page Shows Error.

    Since the pipeline works as a component that runs other components, we can not easily look at what is happening to the orderform as it is happening. But we can add a stage above and below the credit card validation stage that logs the orderform to a file on our system. These should be stages that hold a Scriptor component. The Scriptor component runs a VBS script. This makes it easy to add code to the pipeline stage without having to compile the code into a pipeline component. The scriptor will run an external file called “dumporder.vbs” which is available in the Site Server SDK under the samples directory.

    To add this file to the scriptor, choose the properties of the Scriptor component. The language field needs to correctly identify what scripting language the file is written in. The location of the file also needs to be specified. I included the dumporder.vbs file as an external location from the SDK installation directory. The last parameter indicates where I want the orderform variables to be dumped. I choose the first stage to dump to “c:\validatecc_dumporder1.txt” and the last stage to dump to “c:\validatecc_dumporder2.txt”. Figure 5 shows this information for the Scriptor component.

    Figure 5: Scriptor Component Properties

    You should only add the dumporder.vbs to the pipeline for testing or debugging. When you want to take the site live, you should remove these stages from the pipeline.

    If you run the page with these two dumporder stages, you will notice that two log files have appeared in the root of the c drive. Listings 3 and 4 show the orderform log file before and after the credit card validation stage. Listing 4 is based on a bad credit card number, so an error was returned. Notice that all of the fields are the same except for the “_Purchase_Errors”.

    Listing 3 : The Orderform Before the Credit Card Validation Stage

    
    ** Orderform Contents **
    Items List
    End of Items List
    Order Key is [_Basket_Errors] Start List
    End of List
    Order Key is [_Purchase_Errors] Start List
    End of List
    Order Key is [_Verify_With] Start Dictionary
    End of Dictionary
    Order Key [cc_type] {String} Value [American Express] {String}
    Order Key [_cc_number] {String} Value [] {String}
    Order Key [_cc_expmonth] {String} Value [1] {String}
    Order Key [_cc_expyear] {String} Value [1998] {String}
    ** End of Orderform Contents **
    
    
    Listing 4 : The Orderform after the credit card validation stage
    
    ** Orderform Contents **
    Items List
    End of Items List
    Order Key is [_Basket_Errors] Start List
    End of List
    Order Key is [_Purchase_Errors] Start List
    	Value [The credit-card number you provided is not valid. 
    	Please verify your payment information or use a different card.] {String}
    End of List
    Order Key is [_Verify_With] Start Dictionary
    End of Dictionary
    Order Key [cc_type] {String} Value [American Express] {String}
    Order Key [_cc_number] {String} Value [] {String}
    Order Key [_cc_expmonth] {String} Value [1] {String}
    Order Key [_cc_expyear] {String} Value [1998] {String}
    ** End of Orderform Contents **
    
    

    Summary

    That is all you need to do to validate a credit card with Site Server Commerce. Once you understand the Dictionary object and the pipeline, using Site Server Commerce is very easy.

    About the Author

    Dina Berry previously worked for Microsoft on the OLE DB and Site Server teams. She is currently working for Sign Me Up Marketing as a Product Manager for XBuilder. In her spare time she develops Site Server Commerce sites.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Bulk membership import/export tool
    This is a utility that is used for bulk membership import/export. Other than the command line help, I have no documentation.

    I make no warranties that it works. It is unsupported. Be sure to have a backup before you run this program. Use at your own risk.

    [Top]
    Other Articles
    Jun 12, 2001 - You've Got Mail! - Site Server Direct Mailer
    In this article, Shiva Manjunath shows us how to configure Site Server Direct Mailer, create test members and distribution lists, create template emails, and finally, create and run a Direct Mailer package to send the personalized emails to the target members.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers