inacho / php-credit-card-validator

Validates popular debit and credit cards numbers against regular expressions and Luhn algorithm. Also validates the CVC and the expiration date.
MIT License
199 stars 104 forks source link

Make CreditCard::creditCardType() public. #6

Closed derekmd closed 6 years ago

derekmd commented 8 years ago

Payment gateway APIs require the credit card type in request parameters but why prompt users in the UI to select it when Visa, Amex, etc. can be automatically recognized at the time of performing auth or capture? validCreditCard() with $type as input is useful if the CC details are being stored now without pre-auth until a later date when e.g., a subscription renewal occurs. Otherwise let the payment gateway reject the card number.

Combined with https://github.com/inacho/php-credit-card-validator/pull/4 this will add credit card recognition so the correct type can be submitted to payment gateway APIs.

public function getTypeForGateway($type)
{
    CreditCard::TYPE_AMEX:
        return 'Amex constant specific for that payment API';

        break;
    CreditCard::TYPE_DANKORT:
    CreditCard::TYPE_DINERS_CLUB:
    CreditCard::TYPE_DISCOVER:
    CreditCard::TYPE_FORBRUGSFORENINGEN:
    CreditCard::TYPE_JCB:
    CreditCard::TYPE_MAESTRO:
    CreditCard::TYPE_MASTERCARD:
    CreditCard::TYPE_UNION_PAY:
    CreditCard::TYPE_VISA:
    CreditCard::TYPE_VISA_ELECTRON:
        // etc.

        break;
}

public function auth($creditCard)
{
    $payloadForApi = [
        'type' = $this->getTypeForGateway(CreditCard::creditCardType($creditCard->number));

        // ...
    ];

}

For one use case I ended up creating an intermediate wrapper class:

class InachoCreditCard extends \Inacho\CreditCard
{
    public static function getCardType($number)
    {
        $type = strtoupper(static::creditCardType($number));

        // CreditCardType contains constants for Moneris
        if (CreditCardType::isValidValue($type)) {
            return $type;
        }

        return null;
    }
}
tomsommer commented 8 years ago

+1

marius-c commented 7 years ago

+1

derekmd commented 6 years ago