protected function buildEnvelope(DOMDocument $xml, DOMElement $transaction)
    {
        $soapEnvelope = $xml->createElement('soapenv:Envelope');
        $soapEnvelope->setAttribute(
            'xmlns:soapenv',
            'http://schemas.xmlsoap.org/soap/envelope/'
        );
        $soapEnvelope->setAttribute('xmlns', static::XML_NAMESPACE);

        $soapBody = $xml->createElement('soapenv:Body');
        $request = $xml->createElement('PosRequest');

        $version = $xml->createElement('Ver1.0');
        $header = $xml->createElement('Header');

        if (!empty($this->secretApiKey)) {
            $header->appendChild(
                $xml->createElement('SecretAPIKey', trim($this->secretApiKey))
            );
        }
        if (!empty($this->siteId)) {
            $header->appendChild(
                $xml->createElement('SiteId', $this->siteId)
            );
        }
        if (!empty($this->deviceId)) {
            $header->appendChild(
                $xml->createElement('DeviceId', $this->deviceId)
            );
        }
        if (!empty($this->licenseId)) {
            $header->appendChild(
                $xml->createElement('LicenseId', $this->licenseId)
            );
        }
        if (!empty($this->username)) {
            $header->appendChild(
                $xml->createElement('UserName', $this->username)
            );
        }
        if (!empty($this->password)) {
            $header->appendChild(
                $xml->createElement('Password', $this->password)
            );
        }
        if (!empty($this->developerId)) {
            $header->appendChild(
                $xml->createElement('DeveloperID', $this->developerId)
            );
        }
        if (!empty($this->versionNumber)) {
            $header->appendChild(
                $xml->createElement('VersionNbr', $this->versionNumber)
            );
        }

        $version->appendChild($header);
        $transactionElement = $xml->createElement('Transaction');
        $transactionElement->appendChild($xml->importNode($transaction, true));
        $version->appendChild($transactionElement);

        $request->appendChild($version);
        $soapBody->appendChild($request);
        $soapEnvelope->appendChild($soapBody);
        $xml->appendChild($soapEnvelope);

        return $xml->saveXML();
    }