private function serializeTransaction(DOMElement $element, Transaction $transaction) { $element->appendChild(new DOMElement("purchaseID", $transaction->getPurchaseId())); $element->appendChild(new DOMElement("amount", number_format($transaction->getAmount(), 2, '.', ''))); $element->appendChild(new DOMElement("currency", $transaction->getCurrency())); if ($transaction->getExpirationPeriod() === 60) { $element->appendChild(new DOMElement("expirationPeriod", "PT1H")); } else { $element->appendChild(new DOMElement("expirationPeriod", "PT" . $transaction->getExpirationPeriod() . "M")); } $element->appendChild(new DOMElement("language", $transaction->getLanguage())); $element->appendChild(new DOMElement("description", $transaction->getDescription())); $element->appendChild(new DOMElement("entranceCode", $transaction->getEntranceCode())); }
private function validateTransaction(Transaction $transaction) { if ($transaction->getAmount() < 0 || $transaction->getAmount() >= 1000000000000) { throw new ValidationException("Transaction.amount outside value range."); } if ($transaction->getCurrency() !== "EUR") { throw new ValidationException("Transaction.currency does not match format."); } if ($transaction->getExpirationPeriod() < 1 || $transaction->getExpirationPeriod() > 60) { throw new ValidationException("Transaction.expirationPeriod length outside range('PT1M', 'PT1H')."); } if (strlen($transaction->getLanguage()) !== 2) { throw new ValidationException("Transaction.language length not 2."); } if (strlen($transaction->getDescription()) < 1 || strlen($transaction->getDescription()) > 35) { throw new ValidationException("Transaction.description length outside range(1, 35)."); } if (strlen($transaction->getEntranceCode()) < 1 || strlen($transaction->getEntranceCode()) > 40) { throw new ValidationException("Transaction.entranceCode length outside range(1, 35)."); } $length = preg_match('/[a-z]+/', $transaction->getLanguage(), $matches); if ($length !== 1 || $matches[0] !== $transaction->getLanguage()) { throw new ValidationException("Transaction.language does not match format."); } $length = preg_match('/[a-zA-Z0-9]+/', $transaction->getEntranceCode(), $matches); if ($length !== 1 || $matches[0] !== $transaction->getEntranceCode()) { throw new ValidationException("Transaction.entranceCode does not match format."); } $length = preg_match('/[a-zA-Z0-9]+/', $transaction->getPurchaseId(), $matches); if ($length !== 1 || $matches[0] !== $transaction->getPurchaseId()) { throw new ValidationException("Transaction.purchaseId does not match format."); } }