Zend_Validate-CreditCard.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.creditcard">
  4. <title>CreditCard</title>
  5. <para>
  6. <classname>Zend_Validate_CreditCard</classname> allows you to validate if a given value
  7. could be a credit card number.
  8. </para>
  9. <para>
  10. A creditcard contains several items of metadata, including a hologram, account number, logo,
  11. expiration date, security code and the card holder name. The algorithms for verifying the
  12. combination of metadata are only known to the issuing company, and should be verified with
  13. them for purposes of payment. However, it's often useful to know whether or not a given
  14. number actually falls within the ranges of possible numbers <emphasis>prior</emphasis> to
  15. performing such verification, and, as such, <classname>Zend_Validate_CreditCard</classname>
  16. simply verifies that the credit card number provided is well-formed.
  17. </para>
  18. <para>
  19. For those cases where you have a service that can perform comprehensive verification,
  20. <classname>Zend_Validate_CreditCard</classname> also provides the ability to attach a
  21. service callback to trigger once the credit card number has been deemed valid; this callback
  22. will then be triggered, and its return value will determine overall validity.
  23. </para>
  24. <para>
  25. The following issuing institutes are accepted:
  26. </para>
  27. <itemizedlist>
  28. <listitem>
  29. <para>
  30. <emphasis>American Express</emphasis>
  31. </para>
  32. <para>
  33. <emphasis>China UnionPay</emphasis>
  34. </para>
  35. <para>
  36. <emphasis>Diners Club Card Blanche</emphasis>
  37. </para>
  38. <para>
  39. <emphasis>Diners Club International</emphasis>
  40. </para>
  41. <para>
  42. <emphasis>Diners Club US &amp; Canada</emphasis>
  43. </para>
  44. <para>
  45. <emphasis>Discover Card</emphasis>
  46. </para>
  47. <para>
  48. <emphasis>JCB</emphasis>
  49. </para>
  50. <para>
  51. <emphasis>Laser</emphasis>
  52. </para>
  53. <para>
  54. <emphasis>Maestro</emphasis>
  55. </para>
  56. <para>
  57. <emphasis>MasterCard</emphasis>
  58. </para>
  59. <para>
  60. <emphasis>Solo</emphasis>
  61. </para>
  62. <para>
  63. <emphasis>Visa</emphasis>
  64. </para>
  65. <para>
  66. <emphasis>Visa Electron</emphasis>
  67. </para>
  68. </listitem>
  69. </itemizedlist>
  70. <note>
  71. <title>Invalid institutes</title>
  72. <para>
  73. The institutes <emphasis>Bankcard</emphasis> and <emphasis>Diners Club
  74. enRoute</emphasis> do not exist anymore. Therefore they are treated as invalid.
  75. </para>
  76. <para>
  77. <emphasis>Switch</emphasis> has been rebranded to <emphasis>Visa</emphasis> and is
  78. therefore also treated as invalid.
  79. </para>
  80. </note>
  81. <sect3 id="zend.validate.set.creditcard.options">
  82. <title>Supported options for Zend_Validate_CreditCard</title>
  83. <para>
  84. The following options are supported for <classname>Zend_Validate_CreditCard</classname>:
  85. </para>
  86. <itemizedlist>
  87. <listitem>
  88. <para>
  89. <emphasis><property>service</property></emphasis>: A callback to an online
  90. service which will additionally be used for the validation.
  91. </para>
  92. </listitem>
  93. <listitem>
  94. <para>
  95. <emphasis><property>type</property></emphasis>: The type of creditcard which
  96. will be validated. See the below list of institutes for details.
  97. </para>
  98. </listitem>
  99. </itemizedlist>
  100. </sect3>
  101. <sect3 id="zend.validate.set.creditcard.basic">
  102. <title>Basic usage</title>
  103. <para>
  104. There are several credit card institutes which can be validated by
  105. <classname>Zend_Validate_CreditCard</classname>. Per default, all known institutes will
  106. be accepted. See the folowing example:
  107. </para>
  108. <programlisting language="php"><![CDATA[
  109. $valid = new Zend_Validate_CreditCard();
  110. if ($valid->isValid($input)) {
  111. // input appears to be valid
  112. } else {
  113. // input is invalid
  114. }
  115. ]]></programlisting>
  116. <para>
  117. The above example would validate against all known credit card institutes.
  118. </para>
  119. </sect3>
  120. <sect3 id="zend.validate.set.creditcard.institute">
  121. <title>Accepting defined credit cards</title>
  122. <para>
  123. Sometimes it is necessary to accept only defined credit card institutes instead of all;
  124. e.g., when you have a webshop which accepts only Visa and American Express cards.
  125. <classname>Zend_Validate_CreditCard</classname> allows you to do exactly this by
  126. limiting it to exactly these institutes.
  127. </para>
  128. <para>
  129. To use a limitation you can either provide specific institutes at initiation, or
  130. afterwards by using <methodname>setType()</methodname>. Each can take several arguments.
  131. </para>
  132. <para>
  133. You can provide a single institute:
  134. </para>
  135. <programlisting language="php"><![CDATA[
  136. $valid = new Zend_Validate_CreditCard(
  137. Zend_Validate_CreditCard::AMERICAN_EXPRESS
  138. );
  139. ]]></programlisting>
  140. <para>
  141. When you want to allow multiple institutes, then you can provide them as array:
  142. </para>
  143. <programlisting language="php"><![CDATA[
  144. $valid = new Zend_Validate_CreditCard(array(
  145. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  146. Zend_Validate_CreditCard::VISA
  147. ));
  148. ]]></programlisting>
  149. <para>
  150. And as with all validators, you can also pass an associative array of options or an
  151. instance of <classname>Zend_Config</classname>. In this case you have to provide the
  152. institutes with the <property>type</property> array key as simulated here:
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. $valid = new Zend_Validate_CreditCard(array(
  156. 'type' => array(Zend_Validate_CreditCard::AMERICAN_EXPRESS)
  157. ));
  158. ]]></programlisting>
  159. <table id="zend.validate.set.creditcard.institute.table">
  160. <title>Constants for credit card institutes</title>
  161. <tgroup cols="2">
  162. <thead>
  163. <row>
  164. <entry>Institute</entry>
  165. <entry>Constant</entry>
  166. </row>
  167. </thead>
  168. <tbody>
  169. <row>
  170. <entry><emphasis>American Express</emphasis></entry>
  171. <entry><constant>AMERICAN_EXPRESS</constant></entry>
  172. </row>
  173. <row>
  174. <entry><emphasis>China UnionPay</emphasis></entry>
  175. <entry><constant>UNIONPAY</constant></entry>
  176. </row>
  177. <row>
  178. <entry><emphasis>Diners Club Card Blanche</emphasis></entry>
  179. <entry><constant>DINERS_CLUB</constant></entry>
  180. </row>
  181. <row>
  182. <entry><emphasis>Diners Club International</emphasis></entry>
  183. <entry><constant>DINERS_CLUB</constant></entry>
  184. </row>
  185. <row>
  186. <entry><emphasis>Diners Club US &amp; Canada</emphasis></entry>
  187. <entry><constant>DINERS_CLUB_US</constant></entry>
  188. </row>
  189. <row>
  190. <entry><emphasis>Discover Card</emphasis></entry>
  191. <entry><constant>DISCOVER</constant></entry>
  192. </row>
  193. <row>
  194. <entry><emphasis>JCB</emphasis></entry>
  195. <entry><constant>JCB</constant></entry>
  196. </row>
  197. <row>
  198. <entry><emphasis>Laser</emphasis></entry>
  199. <entry><constant>LASER</constant></entry>
  200. </row>
  201. <row>
  202. <entry><emphasis>Maestro</emphasis></entry>
  203. <entry><constant>MAESTRO</constant></entry>
  204. </row>
  205. <row>
  206. <entry><emphasis>MasterCard</emphasis></entry>
  207. <entry><constant>MASTERCARD</constant></entry>
  208. </row>
  209. <row>
  210. <entry><emphasis>Solo</emphasis></entry>
  211. <entry><constant>SOLO</constant></entry>
  212. </row>
  213. <row>
  214. <entry><emphasis>Visa</emphasis></entry>
  215. <entry><constant>VISA</constant></entry>
  216. </row>
  217. <row>
  218. <entry><emphasis>Visa Electron</emphasis></entry>
  219. <entry><constant>VISA</constant></entry>
  220. </row>
  221. </tbody>
  222. </tgroup>
  223. </table>
  224. <para>
  225. You can also set or add institutes afterward instantiation by using the methods
  226. <methodname>setType()</methodname>, <methodname>addType()</methodname> and
  227. <methodname>getType()</methodname>.
  228. </para>
  229. <programlisting language="php"><![CDATA[
  230. $valid = new Zend_Validate_CreditCard();
  231. $valid->setType(array(
  232. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  233. Zend_Validate_CreditCard::VISA
  234. ));
  235. ]]></programlisting>
  236. <note>
  237. <title>Default institute</title>
  238. <para>
  239. When no institute is given at initiation then <constant>ALL</constant> will be
  240. used, which sets all institutes at once.
  241. </para>
  242. <para>
  243. In this case the usage of <methodname>addType()</methodname> is useless because all
  244. institutes are already added.
  245. </para>
  246. </note>
  247. </sect3>
  248. <sect3 id="zend.validate.set.creditcard.servicecheck">
  249. <title>Validation by using foreign APIs</title>
  250. <para>
  251. As said before <classname>Zend_Validate_CreditCard</classname> will only validate
  252. the credit card number. Fortunately, some institutes provide online
  253. <acronym>API</acronym>s which can validate a credit card number by using algorithms
  254. which are not available to the public. Most of these services are paid services.
  255. Therefore, this check is deactivated per default.
  256. </para>
  257. <para>
  258. When you have access to such an <acronym>API</acronym>, then you can use it as an addon
  259. for <classname>Zend_Validate_CreditCard</classname> and increase the security of the
  260. validation.
  261. </para>
  262. <para>
  263. To do so, you simply need to give a callback which will be called when the generic
  264. validation has passed. This prevents the <acronym>API</acronym> from being called
  265. for invalid numbers, which increases the performance of the application.
  266. </para>
  267. <para>
  268. <methodname>setService()</methodname> sets a new service, and
  269. <methodname>getService()</methodname> returns the set service. As a configuration
  270. option,
  271. you can give the array key '<property>service</property>' at initiation. For details
  272. about possible options take a look into <link
  273. linkend="zend.validate.set.callback">Callback</link>.
  274. </para>
  275. <programlisting language="php"><![CDATA[
  276. // Your service class
  277. class CcService
  278. {
  279. public function checkOnline($cardnumber, $types)
  280. {
  281. // some online validation
  282. }
  283. }
  284. // The validation
  285. $service = new CcService();
  286. $valid = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
  287. $valid->setService(array($service, 'checkOnline'));
  288. ]]></programlisting>
  289. <para>
  290. As you can see the callback method will be called with the creditcard number as the
  291. first parameter, and the accepted types as the second parameter.
  292. </para>
  293. </sect3>
  294. </sect2>
  295. <!--
  296. vim:se ts=4 sw=4 et:
  297. -->