setAccount($accountNumber, $routingNumber, $accountType); } if (!empty($chequeNumber)) { $this->setChequeNumber($chequeNumber); } if (!empty($bankName) && !empty($bankProvince)) { $this->setBank($bankName, $bankProvince); } if (!empty($idNumber) && !empty($idProvince)) { $this->setIdentification($idNumber, $idProvince); } if (!empty($void)) { $this->setVoid($void); } if (!empty($ssn)) { $this->setSsn($ssn); } } /** * Sets the bank account number, routing number, and account type. * * @todo fix account and routing numbers that begin with zero (ie. '091300010') * @param int $accountNumber * @param int $routingNumber * @param string $accountType * @return \LinkPoint\Payment_Cheque * @throws \LinkPoint\Exception */ public function setAccount($accountNumber, $routingNumber, $accountType = 'PC') { $this->_accountNumber = $accountNumber; if (!in_array($accountType, $this->getAccountTypes())) { throw new Exception('Invalid account type'); } $this->_accountType = $accountType; $routingNumber = preg_replace('[\D]', '', $routingNumber); if (strlen($routingNumber) != 9) { throw new Exception('Invalid bank routing number: '.$routingNumber); } $checkSum = 0; for ($i = 0, $j = 9; $i < $j; $i+= 3 ) { // loop through routingNumber character by character $checkSum += ($routingNumber[$i] * 3); $checkSum += ($routingNumber[($i + 1)] * 7); $checkSum += ($routingNumber[($i + 2)]); } if (($checkSum != 0) && (($checkSum % 10) == 0)) { $this->_routingNumber = $routingNumber; return $this; } else { throw new Exception('Invalid bank routing number: '.$routingNumber); } } /** * Returns the account number. * @return int */ public function getAccountNumber() { return $this->_accountNumber; } /** * Returns the bank routing number. * @return int */ public function getRoutingNumber() { return $this->_routingNumber; } /** * Returns the account type. * @return string */ public function getAccountType() { return $this->_accountType; } /** * Get global account types * * @return array */ public function getAccountTypes() { return array(Payment_Cheque::PERSONAL_CHECKING, Payment_Cheque::PERSONAL_SAVINGS, Payment_Cheque::BUSINESS_CHECKING, Payment_Cheque::BUSINESS_SAVINGS); } /** * Sets the cheque number. * * @param int $chequeNumber * @return \LinkPoint\Payment_Cheque * @throws \LinkPoint\Exception */ public function setChequeNumber($chequeNumber) { $this->_chequeNumber = $chequeNumber; return $this; } /** * Returns the cheque number. * * @return string */ public function getChequeNumber() { return $this->_chequeNumber; } /** * Sets the bank name and province. * * @param string $bankName * @param string $bankProvince * @return \LinkPoint\Payment_Cheque */ public function setBank($bankName, $bankProvince) { $this->_bankName = $bankName; $this->_bankProvince = $bankProvince; return $this; } /** * Returns bank information. * @param string $part * @return array|string * @throws \LinkPoint\Exception */ public function getBank($part = null) { if ($part == 'name') { return $this->_bankName; } elseif ($part == 'province') { return $this->_bankProvince; } else { return array($this->_bankName, $this->_bankProvince); } } /** * Sets identification number and issuing province. * * @param int $chequeNumber * @return \LinkPoint\Payment_Cheque */ public function setIdentification($idNumber, $idProvince) { $this->_idNumber = $idNumber; $this->_idProvince = $idProvince; return $this; } /** * Gets identification information. * * @param string $part * @return array|string|integer * @throws \LinkPoint\Exception */ public function getIdentification($part = null) { if ($part == 'number') { return $this->_idNumber; } elseif ($part == 'province') { return $this->_idProvince; } else { return array($this->_idNumber, $this->_idProvince); } } /** * Set whether to void the cheque. * * @param boolean $void * @return \LinkPoint\Payment_Cheque */ public function setVoid($void = false) { $this->_void = $void; return $this; } /** * Returns whether or not the cheque is to be voided. * @return boolean */ public function getVoid() { return $this->_void; } /** * Set the social security number. * * @param int $ssn * @return \LinkPoint\Payment_Cheque */ public function setSsn($ssn) { $this->_ssn = $ssn; return $this; } /** * Returns the social security number. * @return int */ public function getSsn() { return $this->_ssn; } /** * Convert the cheque object into an XML entity * that can be sent to the payment processor. */ public function __toString() { $doc = new \DOMDocument(); // root element $xml = $doc->createElement(Payment_Cheque::XML_ENTITY); // account number $xml->appendChild($doc->createElement('account', $this->getAccountNumber())); // routing number $xml->appendChild($doc->createElement('routing', $this->getRoutingNumber())); // cheque number if ($this->getChequeNumber()) { $xml->appendChild($doc->createElement('checknumber', $this->getChequeNumber())); } // account type $xml->appendChild($doc->createElement('accounttype', $this->getAccountType())); // bank name $xml->appendChild($doc->createElement('bankname', $this->getBank('name'))); // bank province $xml->appendChild($doc->createElement('bankstate', $this->getBank('province'))); // identification if ($this->getIdentification('number')) { // id number $xml->appendChild($doc->createElement('dl', $this->getIdentification('number'))); // id province $xml->appendChild($doc->createElement('dlstate', $this->getIdentification('province'))); } // void if ($this->getVoid()) { $xml->appendChild($doc->createElement('void', (int) $this->getVoid())); } // ssn if ($this->getSsn()) { $xml ->appendChild($doc->createElement('ssn', $this->getSsn())); } $doc->appendChild($xml); return $doc->saveHTML(); } }