Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditCard.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
40  const ALL = 'All';
41  const AMERICAN_EXPRESS = 'American_Express';
42  const UNIONPAY = 'Unionpay';
43  const DINERS_CLUB = 'Diners_Club';
44  const DINERS_CLUB_US = 'Diners_Club_US';
45  const DISCOVER = 'Discover';
46  const JCB = 'JCB';
47  const LASER = 'Laser';
48  const MAESTRO = 'Maestro';
49  const MASTERCARD = 'Mastercard';
50  const SOLO = 'Solo';
51  const VISA = 'Visa';
52 
53  const CHECKSUM = 'creditcardChecksum';
54  const CONTENT = 'creditcardContent';
55  const INVALID = 'creditcardInvalid';
56  const LENGTH = 'creditcardLength';
57  const PREFIX = 'creditcardPrefix';
58  const SERVICE = 'creditcardService';
59  const SERVICEFAILURE = 'creditcardServiceFailure';
60 
66  protected $_messageTemplates = array(
67  self::CHECKSUM => "'%value%' seems to contain an invalid checksum",
68  self::CONTENT => "'%value%' must contain only digits",
69  self::INVALID => "Invalid type given. String expected",
70  self::LENGTH => "'%value%' contains an invalid amount of digits",
71  self::PREFIX => "'%value%' is not from an allowed institute",
72  self::SERVICE => "'%value%' seems to be an invalid creditcard number",
73  self::SERVICEFAILURE => "An exception has been raised while validating '%value%'",
74  );
75 
81  protected $_cardLength = array(
82  self::AMERICAN_EXPRESS => array(15),
83  self::DINERS_CLUB => array(14),
84  self::DINERS_CLUB_US => array(16),
85  self::DISCOVER => array(16),
86  self::JCB => array(16),
87  self::LASER => array(16, 17, 18, 19),
88  self::MAESTRO => array(12, 13, 14, 15, 16, 17, 18, 19),
89  self::MASTERCARD => array(16),
90  self::SOLO => array(16, 18, 19),
91  self::UNIONPAY => array(16, 17, 18, 19),
92  self::VISA => array(16),
93  );
94 
100  protected $_cardType = array(
101  self::AMERICAN_EXPRESS => array('34', '37'),
102  self::DINERS_CLUB => array('300', '301', '302', '303', '304', '305', '36'),
103  self::DINERS_CLUB_US => array('54', '55'),
104  self::DISCOVER => array('6011', '622126', '622127', '622128', '622129', '62213',
105  '62214', '62215', '62216', '62217', '62218', '62219',
106  '6222', '6223', '6224', '6225', '6226', '6227', '6228',
107  '62290', '62291', '622920', '622921', '622922', '622923',
108  '622924', '622925', '644', '645', '646', '647', '648',
109  '649', '65'),
110  self::JCB => array('3528', '3529', '353', '354', '355', '356', '357', '358'),
111  self::LASER => array('6304', '6706', '6771', '6709'),
112  self::MAESTRO => array('5018', '5020', '5038', '6304', '6759', '6761', '6763'),
113  self::MASTERCARD => array('51', '52', '53', '54', '55'),
114  self::SOLO => array('6334', '6767'),
115  self::UNIONPAY => array('622126', '622127', '622128', '622129', '62213', '62214',
116  '62215', '62216', '62217', '62218', '62219', '6222', '6223',
117  '6224', '6225', '6226', '6227', '6228', '62290', '62291',
118  '622920', '622921', '622922', '622923', '622924', '622925'),
119  self::VISA => array('4'),
120  );
121 
127  protected $_type = array();
128 
134  protected $_service;
135 
141  public function __construct($options = array())
142  {
143  if ($options instanceof Zend_Config) {
144  $options = $options->toArray();
145  } else if (!is_array($options)) {
146  $options = func_get_args();
147  $temp['type'] = array_shift($options);
148  if (!empty($options)) {
149  $temp['service'] = array_shift($options);
150  }
151 
152  $options = $temp;
153  }
154 
155  if (!array_key_exists('type', $options)) {
156  $options['type'] = self::ALL;
157  }
158 
159  $this->setType($options['type']);
160  if (array_key_exists('service', $options)) {
161  $this->setService($options['service']);
162  }
163  }
164 
170  public function getType()
171  {
172  return $this->_type;
173  }
174 
181  public function setType($type)
182  {
183  $this->_type = array();
184  return $this->addType($type);
185  }
186 
193  public function addType($type)
194  {
195  if (is_string($type)) {
196  $type = array($type);
197  }
198 
199  foreach($type as $typ) {
200  if (defined('self::' . strtoupper($typ)) && !in_array($typ, $this->_type)) {
201  $this->_type[] = $typ;
202  }
203 
204  if (($typ == self::ALL)) {
205  $this->_type = array_keys($this->_cardLength);
206  }
207  }
208 
209  return $this;
210  }
211 
217  public function getService()
218  {
219  return $this->_service;
220  }
221 
229  public function setService($service)
230  {
231  if (!is_callable($service)) {
232  #require_once 'Zend/Validate/Exception.php';
233  throw new Zend_Validate_Exception('Invalid callback given');
234  }
235 
236  $this->_service = $service;
237  return $this;
238  }
239 
248  public function isValid($value)
249  {
250  $this->_setValue($value);
251 
252  if (!is_string($value)) {
253  $this->_error(self::INVALID, $value);
254  return false;
255  }
256 
257  if (!ctype_digit($value)) {
258  $this->_error(self::CONTENT, $value);
259  return false;
260  }
261 
262  $length = strlen($value);
263  $types = $this->getType();
264  $foundp = false;
265  $foundl = false;
266  foreach ($types as $type) {
267  foreach ($this->_cardType[$type] as $prefix) {
268  if (substr($value, 0, strlen($prefix)) == $prefix) {
269  $foundp = true;
270  if (in_array($length, $this->_cardLength[$type])) {
271  $foundl = true;
272  break 2;
273  }
274  }
275  }
276  }
277 
278  if ($foundp == false){
279  $this->_error(self::PREFIX, $value);
280  return false;
281  }
282 
283  if ($foundl == false) {
284  $this->_error(self::LENGTH, $value);
285  return false;
286  }
287 
288  $sum = 0;
289  $weight = 2;
290 
291  for ($i = $length - 2; $i >= 0; $i--) {
292  $digit = $weight * $value[$i];
293  $sum += floor($digit / 10) + $digit % 10;
294  $weight = $weight % 2 + 1;
295  }
296 
297  if ((10 - $sum % 10) % 10 != $value[$length - 1]) {
298  $this->_error(self::CHECKSUM, $value);
299  return false;
300  }
301 
302  if (!empty($this->_service)) {
303  try {
304  #require_once 'Zend/Validate/Callback.php';
305  $callback = new Zend_Validate_Callback($this->_service);
306  $callback->setOptions($this->_type);
307  if (!$callback->isValid($value)) {
308  $this->_error(self::SERVICE, $value);
309  return false;
310  }
311  } catch (Zend_Exception $e) {
312  $this->_error(self::SERVICEFAILURE, $value);
313  return false;
314  }
315  }
316 
317  return true;
318  }
319 }
_error($messageKey, $value=null)
Definition: Abstract.php:284
$type
Definition: item.phtml:13
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
__construct($options=array())
Definition: CreditCard.php:141
$i
Definition: gallery.phtml:31