<?php
 namespace App\Utils\CreditCard; class MatchCard { protected string $cardNumberTruncated; protected ?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; } return false; } } 