Omnipay\Common\CreditCard::validate PHP Method

validate() public method

This method is called internally by gateways to avoid wasting time with an API call when the credit card is clearly invalid. Generally if you want to validate the credit card yourself with custom error messages, you should use your framework's validation library, not this method.
public validate ( ) : void
return void
    public function validate()
    {
        foreach (array('number', 'expiryMonth', 'expiryYear') as $key) {
            if (!$this->getParameter($key)) {
                throw new InvalidCreditCardException("The {$key} parameter is required");
            }
        }
        if ($this->getExpiryDate('Ym') < gmdate('Ym')) {
            throw new InvalidCreditCardException('Card has expired');
        }
        if (!Helper::validateLuhn($this->getNumber())) {
            throw new InvalidCreditCardException('Card number is invalid');
        }
        if (!is_null($this->getNumber()) && !preg_match('/^\\d{12,19}$/i', $this->getNumber())) {
            throw new InvalidCreditCardException('Card number should have 12 to 19 digits');
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @expectedException Omnipay\Common\Exception\InvalidCreditCardException
  * @expectedExceptionMessage Card number should have 12 to 19 digits
  */
 public function testInvalidShortCard()
 {
     $this->card->setNumber('4440');
     $this->card->validate();
 }
CreditCard