22 protected $_formBlockType = \Magento\Payment\Block\Form\Cc::class;
27 protected $_infoBlockType = \Magento\Payment\Block\Info\Cc::class;
32 protected $_canSaveCc =
false;
37 protected $_moduleList;
42 protected $_localeDate;
59 public function __construct(
62 \
Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
63 \
Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
64 \
Magento\Payment\Helper\Data $paymentData,
65 \
Magento\Framework\
App\Config\ScopeConfigInterface $scopeConfig,
67 \
Magento\Framework\Module\ModuleListInterface $moduleList,
68 \
Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
70 \
Magento\Framework\Data\Collection\AbstractDb $resourceCollection =
null,
77 $customAttributeFactory,
85 $this->_moduleList = $moduleList;
86 $this->_localeDate = $localeDate;
97 public function validate()
104 $info = $this->getInfoInstance();
106 $availableTypes = explode(
',', $this->getConfigData(
'cctypes'));
108 $ccNumber =
$info->getCcNumber();
111 $ccNumber = preg_replace(
'/[\-\s]+/',
'', $ccNumber);
112 $info->setCcNumber($ccNumber);
116 if (in_array(
$info->getCcType(), $availableTypes)) {
117 if ($this->validateCcNum(
119 ) || $this->otherCcType(
121 ) && $this->validateCcNumOther(
126 $ccTypeRegExpList = [
128 'SO' =>
'/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/',
129 'SM' =>
'/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)' .
130 '|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)' .
131 '|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))' .
132 '|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))' .
133 '|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',
135 'VI' =>
'/^4[0-9]{12}([0-9]{3})?$/',
137 'MC' =>
'/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/',
139 'AE' =>
'/^3[47][0-9]{13}$/',
141 'DI' =>
'/^(6011((0|9|[2-4])[0-9]{11,14}|(74|7[7-9]|8[6-9])[0-9]{10,13})|6(4[4-9][0-9]{13,16}|' .
143 'DN' =>
'/^3(0[0-5][0-9]{13,16}|095[0-9]{12,15}|(6|[8-9])[0-9]{14,17})/',
145 'UN' =>
'/^622(1(2[6-9][0-9]{10,13}|[3-9][0-9]{11,14})|[3-8][0-9]{12,15}|9([[0-1][0-9]{11,14}|' .
146 '2[0-5][0-9]{10,13}))|62[4-6][0-9]{13,16}|628[2-8][0-9]{12,15}/',
148 'JCB' =>
'/^35(2[8-9][0-9]{12,15}|[3-8][0-9]{13,16})/',
149 'MI' =>
'/^(5(0|[6-9])|63|67(?!59|6770|6774))\d*$/',
150 'MD' =>
'/^(6759(?!24|38|40|6[3-9]|70|76)|676770|676774)\d*$/',
153 $ccNumAndTypeMatches = isset(
154 $ccTypeRegExpList[
$info->getCcType()]
156 $ccTypeRegExpList[
$info->getCcType()],
159 $ccType = $ccNumAndTypeMatches ?
$info->getCcType() :
'OT';
161 if (!$ccNumAndTypeMatches && !$this->otherCcType(
$info->getCcType())) {
162 $errorMsg =
__(
'The credit card number doesn\'t match the credit card type.');
165 $errorMsg =
__(
'Invalid Credit Card Number');
168 $errorMsg =
__(
'This credit card type is not allowed for this payment method.');
172 if ($errorMsg ===
false && $this->hasVerification()) {
173 $verifcationRegEx = $this->getVerificationRegEx();
174 $regExp = isset($verifcationRegEx[
$info->getCcType()]) ? $verifcationRegEx[
$info->getCcType()] :
'';
175 if (!
$info->getCcCid() || !$regExp || !preg_match($regExp,
$info->getCcCid())) {
176 $errorMsg =
__(
'Please enter a valid credit card verification number.');
180 if (
$ccType !=
'SS' && !$this->_validateExpDate(
$info->getCcExpYear(),
$info->getCcExpMonth())) {
181 $errorMsg =
__(
'Please enter a valid credit card expiration date.');
185 throw new \Magento\Framework\Exception\LocalizedException($errorMsg);
195 public function hasVerification()
208 public function getVerificationRegEx()
210 $verificationExpList = [
211 'VI' =>
'/^[0-9]{3}$/',
212 'MC' =>
'/^[0-9]{3}$/',
213 'AE' =>
'/^[0-9]{4}$/',
214 'DI' =>
'/^[0-9]{3}$/',
215 'DN' =>
'/^[0-9]{3}$/',
216 'UN' =>
'/^[0-9]{3}$/',
217 'SS' =>
'/^[0-9]{3,4}$/',
218 'SM' =>
'/^[0-9]{3,4}$/',
219 'SO' =>
'/^[0-9]{3,4}$/',
220 'OT' =>
'/^[0-9]{3,4}$/',
221 'JCB' =>
'/^[0-9]{3,4}$/',
222 'MI' =>
'/^[0-9]{3}$/',
223 'MD' =>
'/^[0-9]{3}$/',
225 return $verificationExpList;
233 protected function _validateExpDate($expYear, $expMonth)
235 $date = new \DateTime();
236 if (!$expYear || !$expMonth || (
int)$date->format(
'Y') > $expYear
237 || (int)$date->format(
'Y') == $expYear && (int)$date->format(
'm') > $expMonth
251 public function assignData(\
Magento\Framework\DataObject
$data)
254 if (!is_object($additionalData)) {
255 $additionalData =
new DataObject($additionalData ?: []);
259 $info = $this->getInfoInstance();
262 'cc_type' => $additionalData->getCcType(),
263 'cc_owner' => $additionalData->getCcOwner(),
264 'cc_last_4' => substr($additionalData->getCcNumber(), -4),
265 'cc_number' => $additionalData->getCcNumber(),
266 'cc_cid' => $additionalData->getCcCid(),
267 'cc_exp_month' => $additionalData->getCcExpMonth(),
268 'cc_exp_year' => $additionalData->getCcExpYear(),
269 'cc_ss_issue' => $additionalData->getCcSsIssue(),
270 'cc_ss_start_month' => $additionalData->getCcSsStartMonth(),
271 'cc_ss_start_year' => $additionalData->getCcSsStartYear()
283 public function otherCcType(
$type)
285 return $type ==
'OT';
295 public function validateCcNum($ccNumber)
297 $cardNumber = strrev($ccNumber);
300 for (
$i = 0;
$i < strlen($cardNumber);
$i++) {
301 $currentNum = substr($cardNumber,
$i, 1);
313 if ($currentNum > 9) {
314 $firstNum = $currentNum % 10;
315 $secondNum = ($currentNum - $firstNum) / 10;
316 $currentNum = $firstNum + $secondNum;
319 $numSum += $currentNum;
325 return $numSum % 10 == 0;
335 public function validateCcNumOther($ccNumber)
337 return preg_match(
'/^\\d+$/', $ccNumber);
346 public function isAvailable(\
Magento\Quote\Api\Data\CartInterface
$quote =
null)
348 return $this->getConfigData(
'cctypes',
$quote ?
$quote->getStoreId() :
null) && parent::isAvailable(
$quote);
const KEY_ADDITIONAL_DATA
foreach( $_productCollection as $_product)() ?>" class $info