<?php
 namespace App\Utils\CreditCard; class MatchCard { private string $cardNumberTruncated; private ?string $expireDate = null; public function __construct(string $cardNumber, ?string $expireDate=null) { $ccNumberDetailsObj = new \App\Utils\CreditCard\NumberDetails($cardNumber); $strippedCardNumber = $ccNumberDetailsObj->getStrippedNumber(); $cleanedCardNumber = preg_replace('/[^0-9]/', '0', $strippedCardNumber); $cardObj = new NumberDetails($cleanedCardNumber); $this->cardNumberTruncated = $cardObj->getMaskedNumber(); if ($expireDate!==null) { if (!validDate($expireDate)) { throw new \Exception('Invalid expire date for CC matching.'); } $this->expireDate = $expireDate; } } public function matchByAgencyCreditCards() :int|false { $agencyCreditCardsObj = new \App\EntityList\AgencyCreditCards(); $agencyCreditCardsList = $agencyCreditCardsObj->exportRecords(); foreach ($agencyCreditCardsList as $card) { $ccEnt = \Entity\AgencyCreditCard::selectForUpdate($card['id']); if ($this->matchCardByDetails($ccEnt->cardDetails['truncatedNumber'], $ccEnt->cardDetails['expireDate'])) { return $card['id']; } } return false; } public function matchByCorporateCreditCards(int $idCorporate) :int|false { if ($idCorporate<=0) { return false; } $corpCreditCardsObj = new \App\EntityList\CorporateCreditCards(); $corpCreditCardsObj->setFilter('corporateCreditCards.idCorporate', '=', $idCorporate); $corpCreditCardsList = $corpCreditCardsObj->exportRecords(); foreach ($corpCreditCardsList as $card) { $ccEnt = \Entity\CorporateCreditCard::selectForUpdate($card['id']); if ($this->matchCardByDetails($ccEnt->cardDetails['truncatedNumber'], $ccEnt->cardDetails['expireDate'])) { return $card['id']; } } return false; } private function matchCardByDetails(string $cardNumber, ?string $expireDate) :bool { if ($this->expireDate and $expireDate and $this->expireDate!=$expireDate) { return false; } if ($this->cardNumberTruncated===$cardNumber) { return true; } if ($this->expireDate and $expireDate and $this->expireDate===$expireDate) { if (substr($this->cardNumberTruncated, 0, 6)==='000000') { $cleanedCardNumber = '000000' . substr($cardNumber, 6); if ($this->cardNumberTruncated===$cleanedCardNumber) { return true; } } } return false; } } 