1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.validate.set.creditcard">
4 <title>CreditCard</title>
7 <classname>Zend_Validate_CreditCard</classname> allows you to validate if a given value
8 could be a credit card number.
12 A creditcard contains several items of metadata, including a hologram, account number, logo,
13 expiration date, security code and the card holder name. The algorithms for verifying the
14 combination of metadata are only known to the issuing company, and should be verified with
15 them for purposes of payment. However, it's often useful to know whether or not a given
16 number actually falls within the ranges of possible numbers <emphasis>prior</emphasis> to
17 performing such verification, and, as such, <classname>Zend_Validate_CreditCard</classname>
18 simply verifies that the credit card number provided is well-formed.
22 For those cases where you have a service that can perform comprehensive verification,
23 <classname>Zend_Validate_CreditCard</classname> also provides the ability to attach a
24 service callback to trigger once the credit card number has been deemed valid; this callback
25 will then be triggered, and its return value will determine overall validity.
29 The following issuing institutes are accepted:
35 <emphasis>American Express</emphasis>
39 <emphasis>China UnionPay</emphasis>
43 <emphasis>Diners Club Card Blanche</emphasis>
47 <emphasis>Diners Club International</emphasis>
51 <emphasis>Diners Club US & Canada</emphasis>
55 <emphasis>Discover Card</emphasis>
59 <emphasis>JCB</emphasis>
63 <emphasis>Laser</emphasis>
67 <emphasis>Maestro</emphasis>
71 <emphasis>MasterCard</emphasis>
75 <emphasis>Solo</emphasis>
79 <emphasis>Visa</emphasis>
83 <emphasis>Visa Electron</emphasis>
89 <title>Invalid institutes</title>
92 The institutes <emphasis>Bankcard</emphasis> and <emphasis>Diners Club
93 enRoute</emphasis> do not exist anymore. Therefore they are treated as invalid.
97 <emphasis>Switch</emphasis> has been rebranded to <emphasis>Visa</emphasis> and is
98 therefore also treated as invalid.
102 <sect3 id="zend.validate.set.creditcard.options">
103 <title>Supported options for Zend_Validate_CreditCard</title>
106 The following options are supported for <classname>Zend_Validate_CreditCard</classname>:
112 <emphasis><property>service</property></emphasis>: A callback to an online
113 service which will additionally be used for the validation.
119 <emphasis><property>type</property></emphasis>: The type of creditcard which
120 will be validated. See the below list of institutes for details.
126 <sect3 id="zend.validate.set.creditcard.basic">
127 <title>Basic usage</title>
130 There are several credit card institutes which can be validated by
131 <classname>Zend_Validate_CreditCard</classname>. Per default, all known institutes will
132 be accepted. See the folowing example:
135 <programlisting language="php"><![CDATA[
136 $valid = new Zend_Validate_CreditCard();
137 if ($valid->isValid($input)) {
138 // input appears to be valid
145 The above example would validate against all known credit card institutes.
149 <sect3 id="zend.validate.set.creditcard.institute">
150 <title>Accepting defined credit cards</title>
153 Sometimes it is necessary to accept only defined credit card institutes instead of all;
154 e.g., when you have a webshop which accepts only Visa and American Express cards.
155 <classname>Zend_Validate_CreditCard</classname> allows you to do exactly this by
156 limiting it to exactly these institutes.
160 To use a limitation you can either provide specific institutes at initiation, or
161 afterwards by using <methodname>setType()</methodname>. Each can take several arguments.
165 You can provide a single institute:
168 <programlisting language="php"><![CDATA[
169 $valid = new Zend_Validate_CreditCard(
170 Zend_Validate_CreditCard::AMERICAN_EXPRESS
175 When you want to allow multiple institutes, then you can provide them as array:
178 <programlisting language="php"><![CDATA[
179 $valid = new Zend_Validate_CreditCard(array(
180 Zend_Validate_CreditCard::AMERICAN_EXPRESS,
181 Zend_Validate_CreditCard::VISA
186 And as with all validators, you can also pass an associative array of options or an
187 instance of <classname>Zend_Config</classname>. In this case you have to provide the
188 institutes with the <property>type</property> array key as simulated here:
191 <programlisting language="php"><![CDATA[
192 $valid = new Zend_Validate_CreditCard(array(
193 'type' => array(Zend_Validate_CreditCard::AMERICAN_EXPRESS)
197 <table id="zend.validate.set.creditcard.institute.table">
198 <title>Constants for credit card institutes</title>
203 <entry>Institute</entry>
204 <entry>Constant</entry>
210 <entry><emphasis>American Express</emphasis></entry>
211 <entry><constant>AMERICAN_EXPRESS</constant></entry>
215 <entry><emphasis>China UnionPay</emphasis></entry>
216 <entry><constant>UNIONPAY</constant></entry>
220 <entry><emphasis>Diners Club Card Blanche</emphasis></entry>
221 <entry><constant>DINERS_CLUB</constant></entry>
225 <entry><emphasis>Diners Club International</emphasis></entry>
226 <entry><constant>DINERS_CLUB</constant></entry>
230 <entry><emphasis>Diners Club US & Canada</emphasis></entry>
231 <entry><constant>DINERS_CLUB_US</constant></entry>
235 <entry><emphasis>Discover Card</emphasis></entry>
236 <entry><constant>DISCOVER</constant></entry>
240 <entry><emphasis>JCB</emphasis></entry>
241 <entry><constant>JCB</constant></entry>
245 <entry><emphasis>Laser</emphasis></entry>
246 <entry><constant>LASER</constant></entry>
250 <entry><emphasis>Maestro</emphasis></entry>
251 <entry><constant>MAESTRO</constant></entry>
255 <entry><emphasis>MasterCard</emphasis></entry>
256 <entry><constant>MASTERCARD</constant></entry>
260 <entry><emphasis>Solo</emphasis></entry>
261 <entry><constant>SOLO</constant></entry>
265 <entry><emphasis>Visa</emphasis></entry>
266 <entry><constant>VISA</constant></entry>
270 <entry><emphasis>Visa Electron</emphasis></entry>
271 <entry><constant>VISA</constant></entry>
278 You can also set or add institutes afterward instantiation by using the methods
279 <methodname>setType()</methodname>, <methodname>addType()</methodname> and
280 <methodname>getType()</methodname>.
283 <programlisting language="php"><![CDATA[
284 $valid = new Zend_Validate_CreditCard();
285 $valid->setType(array(
286 Zend_Validate_CreditCard::AMERICAN_EXPRESS,
287 Zend_Validate_CreditCard::VISA
292 <title>Default institute</title>
295 When no institute is given at initiation then <constant>ALL</constant> will be
296 used, which sets all institutes at once.
300 In this case the usage of <methodname>addType()</methodname> is useless because all
301 institutes are already added.
306 <sect3 id="zend.validate.set.creditcard.servicecheck">
307 <title>Validation by using foreign APIs</title>
310 As said before <classname>Zend_Validate_CreditCard</classname> will only validate
311 the credit card number. Fortunately, some institutes provide online
312 <acronym>API</acronym>s which can validate a credit card number by using algorithms
313 which are not available to the public. Most of these services are paid services.
314 Therefore, this check is deactivated per default.
318 When you have access to such an <acronym>API</acronym>, then you can use it as an addon
319 for <classname>Zend_Validate_CreditCard</classname> and increase the security of the
324 To do so, you simply need to give a callback which will be called when the generic
325 validation has passed. This prevents the <acronym>API</acronym> from being called
326 for invalid numbers, which increases the performance of the application.
330 <methodname>setService()</methodname> sets a new service, and
331 <methodname>getService()</methodname> returns the set service. As a configuration
333 you can give the array key '<property>service</property>' at initiation. For details
334 about possible options take a look into <link
335 linkend="zend.validate.set.callback">Callback</link>.
338 <programlisting language="php"><![CDATA[
339 // Your service class
342 public function checkOnline($cardnumber, $types)
344 // some online validation
349 $service = new CcService();
350 $valid = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
351 $valid->setService(array($service, 'checkOnline'));
355 As you can see the callback method will be called with the creditcard number as the
356 first parameter, and the accepted types as the second parameter.