Coding the Credit Card Validator
In this section we are going to add the code that communicates to the Validate web service, send the credit card number and deals with the response.
10: In the form1 design view, double click button1 to enter the code view screen. We want to capture the inputted credit card number from textbox1 and so inside the button1_Click type the following:
string cardnumber = textBox1.Text; |
We also want to create a string for holding the keycode. A valid keycode is required whenever you connect to the Validate Web Service. You will have been assigned a keycode from Barron McCann Technology.
Add the following line:
string keycode = "qwerty"; |
Obviously you will add your own keycode in place of 'querty' above.
11: Now to the interesting bit. We now want to instantiate a new object used for communicating with the web service. We do this like you create any other sort of object in c#, with the new statement.
ServiceReference1.validateSoapClient sc = new ServiceReference1.validateSoapClient(); |
You will notice that Intellisense is now fully working with the Validate web service. We now have an object called sc that we can use to reference.
Now we want to take cardnumber and keycode and pass them to a validate function that provides a simple credit card check. This function is called validateCreditCard . This function simply checks the number and passes back whether it is valid and also details of the card such as the issuer, bank etc. There is another function called validateCreditCardEx that also required expiry date and issue details but we don't need these for this simple tutorial.
Add the following line of code:
ServiceReference1. creditcard cardObject = sc.validateCreditCard(keycode, cardnumber); |
You will now have an object cardObject that contains all of the information
back from the Validate Web Service.
To determine if the card number was
valid use cardObject.validity. To determine the card issuer use
cardObject.issuer, etc.
Again Intellisense will show you all of the available cardObject properties.
12: To complete the tutorial add this line to add the validity of the card to label3.
label3.Text = cardObject.validity; |
And that's it. A fully functioning, albeit
trivially simple, credit card validator in five lines of code.
The final
button1click method should look like the following:
{ ServiceReference1. ServiceReference1. label3.Text = cardObject.validity; }
private
void
button1_Click(object
sender, EventArgs
e)
13: Run the application and enter a credit card number into the textbox and press validate. If you are using the non-secure web based test service you should use a test credit card such as one of the following:
Using these test number probably won't return details such as issuer or bank because they aren't real credit card numbers.
![]() |
©Copyright 2023 Paygate Solutions Limited |