<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect2 id="zend.validate.set.creditcard">
    <title>CreditCard</title>

    <para>
        <classname>Zend_Validate_CreditCard</classname> allows you to validate if a given value
        could be a credit card number.
    </para>

    <para>
        A creditcard contains several items of metadata, including a hologram, account number, logo,
        expiration date, security code and the card holder name. The algorithms for verifying the
        combination of metadata are only known to the issuing company, and should be verified with
        them for purposes of payment. However, it's often useful to know whether or not a given
        number actually falls within the ranges of possible numbers <emphasis>prior</emphasis> to
        performing such verification, and, as such, <classname>Zend_Validate_CreditCard</classname>
        simply verifies that the credit card number provided is well-formed.
    </para>

    <para>
        For those cases where you have a service that can perform comprehensive verification,
        <classname>Zend_Validate_CreditCard</classname> also provides the ability to attach a
        service callback to trigger once the credit card number has been deemed valid; this callback
        will then be triggered, and its return value will determine overall validity.
    </para>

    <para>
        The following issuing institutes are accepted:
    </para>

    <itemizedlist>
        <listitem>
            <para>
                <emphasis>American Express</emphasis>
            </para>

            <para>
                <emphasis>China UnionPay</emphasis>
            </para>

            <para>
                <emphasis>Diners Club Card Blanche</emphasis>
            </para>

            <para>
                <emphasis>Diners Club International</emphasis>
            </para>

            <para>
                <emphasis>Diners Club US &amp; Canada</emphasis>
            </para>

            <para>
                <emphasis>Discover Card</emphasis>
            </para>

            <para>
                <emphasis>JCB</emphasis>
            </para>

            <para>
                <emphasis>Laser</emphasis>
            </para>

            <para>
                <emphasis>Maestro</emphasis>
            </para>

            <para>
                <emphasis>MasterCard</emphasis>
            </para>

            <para>
                <emphasis>Solo</emphasis>
            </para>

            <para>
                <emphasis>Visa</emphasis>
            </para>

            <para>
                <emphasis>Visa Electron</emphasis>
            </para>
        </listitem>
    </itemizedlist>

    <note>
        <title>Invalid institutes</title>

        <para>
            The institutes <emphasis>Bankcard</emphasis> and <emphasis>Diners Club
                enRoute</emphasis> do not exist anymore. Therefore they are treated as invalid.
        </para>

        <para>
            <emphasis>Switch</emphasis> has been rebranded to <emphasis>Visa</emphasis> and is
            therefore also treated as invalid.
        </para>
    </note>

    <sect3 id="zend.validate.set.creditcard.options">
        <title>Supported options for Zend_Validate_CreditCard</title>

        <para>
            The following options are supported for <classname>Zend_Validate_CreditCard</classname>:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <emphasis><property>service</property></emphasis>: A callback to an online
                    service which will additionally be used for the validation.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><property>type</property></emphasis>: The type of creditcard which
                    will be validated. See the below list of institutes for details.
                </para>
            </listitem>
        </itemizedlist>
    </sect3>

    <sect3 id="zend.validate.set.creditcard.basic">
        <title>Basic usage</title>

        <para>
            There are several credit card institutes which can be validated by
            <classname>Zend_Validate_CreditCard</classname>. Per default, all known institutes will
            be accepted. See the folowing example:
        </para>

        <programlisting language="php"><![CDATA[
$valid = new Zend_Validate_CreditCard();
if ($valid->isValid($input)) {
    // input appears to be valid
} else {
    // input is invalid
}
]]></programlisting>

        <para>
            The above example would validate against all known credit card institutes.
        </para>
    </sect3>

    <sect3 id="zend.validate.set.creditcard.institute">
        <title>Accepting defined credit cards</title>

        <para>
            Sometimes it is necessary to accept only defined credit card institutes instead of all;
            e.g., when you have a webshop which accepts only Visa and American Express cards.
            <classname>Zend_Validate_CreditCard</classname> allows you to do exactly this by
            limiting it to exactly these institutes.
        </para>

        <para>
            To use a limitation you can either provide specific institutes at initiation, or
            afterwards by using <methodname>setType()</methodname>. Each can take several arguments.
        </para>

        <para>
            You can provide a single institute:
        </para>

        <programlisting language="php"><![CDATA[
$valid = new Zend_Validate_CreditCard(
    Zend_Validate_CreditCard::AMERICAN_EXPRESS
);
]]></programlisting>

        <para>
            When you want to allow multiple institutes, then you can provide them as array:
        </para>

        <programlisting language="php"><![CDATA[
$valid = new Zend_Validate_CreditCard(array(
    Zend_Validate_CreditCard::AMERICAN_EXPRESS,
    Zend_Validate_CreditCard::VISA
));
]]></programlisting>

        <para>
            And as with all validators, you can also pass an associative array of options or an
            instance of <classname>Zend_Config</classname>. In this case you have to provide the
            institutes with the <property>type</property> array key as simulated here:
        </para>

        <programlisting language="php"><![CDATA[
$valid = new Zend_Validate_CreditCard(array(
    'type' => array(Zend_Validate_CreditCard::AMERICAN_EXPRESS)
));
]]></programlisting>

        <table id="zend.validate.set.creditcard.institute.table">
            <title>Constants for credit card institutes</title>

            <tgroup cols="2">
                <thead>
                    <row>
                        <entry>Institute</entry>
                        <entry>Constant</entry>
                    </row>
                </thead>

                <tbody>
                    <row>
                        <entry><emphasis>American Express</emphasis></entry>
                        <entry><constant>AMERICAN_EXPRESS</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>China UnionPay</emphasis></entry>
                        <entry><constant>UNIONPAY</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Diners Club Card Blanche</emphasis></entry>
                        <entry><constant>DINERS_CLUB</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Diners Club International</emphasis></entry>
                        <entry><constant>DINERS_CLUB</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Diners Club US &amp; Canada</emphasis></entry>
                        <entry><constant>DINERS_CLUB_US</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Discover Card</emphasis></entry>
                        <entry><constant>DISCOVER</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>JCB</emphasis></entry>
                        <entry><constant>JCB</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Laser</emphasis></entry>
                        <entry><constant>LASER</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Maestro</emphasis></entry>
                        <entry><constant>MAESTRO</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>MasterCard</emphasis></entry>
                        <entry><constant>MASTERCARD</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Solo</emphasis></entry>
                        <entry><constant>SOLO</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Visa</emphasis></entry>
                        <entry><constant>VISA</constant></entry>
                    </row>

                    <row>
                        <entry><emphasis>Visa Electron</emphasis></entry>
                        <entry><constant>VISA</constant></entry>
                    </row>
                </tbody>
            </tgroup>
        </table>

        <para>
            You can also set or add institutes afterward instantiation by using the methods
            <methodname>setType()</methodname>, <methodname>addType()</methodname> and
            <methodname>getType()</methodname>.
        </para>

        <programlisting language="php"><![CDATA[
$valid = new Zend_Validate_CreditCard();
$valid->setType(array(
    Zend_Validate_CreditCard::AMERICAN_EXPRESS,
    Zend_Validate_CreditCard::VISA
));
]]></programlisting>

        <note>
            <title>Default institute</title>

            <para>
                When no institute is given at initiation then <constant>ALL</constant> will be
                used, which sets all institutes at once.
            </para>

            <para>
                In this case the usage of <methodname>addType()</methodname> is useless because all
                institutes are already added.
            </para>
        </note>
    </sect3>

    <sect3 id="zend.validate.set.creditcard.servicecheck">
        <title>Validation by using foreign APIs</title>

        <para>
            As said before <classname>Zend_Validate_CreditCard</classname> will only validate
            the credit card number. Fortunately, some institutes provide online
            <acronym>API</acronym>s which can validate a credit card number by using algorithms
            which are not available to the public. Most of these services are paid services.
            Therefore, this check is deactivated per default.
        </para>

        <para>
            When you have access to such an <acronym>API</acronym>, then you can use it as an addon
            for <classname>Zend_Validate_CreditCard</classname> and increase the security of the
            validation.
        </para>

        <para>
            To do so, you simply need to give a callback which will be called when the generic
            validation has passed. This prevents the <acronym>API</acronym> from being called
            for invalid numbers, which increases the performance of the application.
        </para>

        <para>
            <methodname>setService()</methodname> sets a new service, and
            <methodname>getService()</methodname> returns the set service. As a configuration
            option,
            you can give the array key '<property>service</property>' at initiation. For details
            about possible options take a look into <link
                linkend="zend.validate.set.callback">Callback</link>.
        </para>

        <programlisting language="php"><![CDATA[
// Your service class
class CcService
{
    public function checkOnline($cardnumber, $types)
    {
        // some online validation
    }
}

// The validation
$service = new CcService();
$valid   = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
$valid->setService(array($service, 'checkOnline'));
]]></programlisting>

        <para>
            As you can see the callback method will be called with the creditcard number as the
            first parameter, and the accepted types as the second parameter.
        </para>
    </sect3>
</sect2>
<!--
vim:se ts=4 sw=4 et:
-->
