1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\FirstAtlanticCommerce; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\CreditCard as BaseCreditCard; |
6
|
|
|
use Omnipay\Common\Exception\InvalidCreditCardException; |
7
|
|
|
|
8
|
|
|
class CreditCard extends BaseCreditCard |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Validate this credit card. If the card is invalid, InvalidCreditCardException is thrown. |
12
|
|
|
* |
13
|
|
|
* This method is called internally by gateways to avoid wasting time with an API call |
14
|
|
|
* when the credit card is clearly invalid. |
15
|
|
|
* |
16
|
|
|
* Falls back to validating number, cvv, expiryMonth, expiryYear if no parameters are present. |
17
|
|
|
* |
18
|
|
|
* @param string ... Optional variable length list of required parameters |
19
|
|
|
* @throws InvalidCreditCardException |
20
|
|
|
*/ |
21
|
|
|
public function validate() |
22
|
|
|
{ |
23
|
|
|
$parameters = func_get_args(); |
24
|
|
|
|
25
|
|
|
if ( count($parameters) == 0 ) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$parameters = ['number', 'cvv', 'expiryMonth', 'expiryYear']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($parameters as $key) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$value = $this->parameters->get($key); |
33
|
|
|
|
34
|
|
|
if ( empty($value) ) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
throw new InvalidCreditCardException("The $key parameter is required"); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ( isset($parameters['expiryMonth']) && isset($parameters['expiryYear']) ) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if ( $this->getExpiryDate('Ym') < gmdate('Ym') ) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
throw new InvalidCreditCardException('Card has expired'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ( isset($parameters['number']) ) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if ( !Helper::validateLuhn( $this->getNumber() ) ) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
throw new InvalidCreditCardException('Card number is invalid'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ( !is_null( $this->getNumber() ) && !preg_match( '/^\d{12,19}$/i', $this->getNumber() ) ) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
throw new InvalidCreditCardException('Card number should have 12 to 19 digits'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( isset($parameters['cvv']) ) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if ( !is_null( $this->getCvv() ) && !preg_match( '/^\d{3,4}$/i', $this->getCvv() ) ) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
throw new InvalidCreditCardException('Card CVV should have 3 to 4 digits'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|