Wraps a transaction with a SOAP envelope

@param DOMDocument $xml The current DOMDocument object
@param DOMElement $transaction The current transaction to wrap

@return DOMElement
10730
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();
    }
195257
public function normalize($transaction, $format = null, array $context = array())
    {
        /** @var  $transaction Transaction */
        $credentials = $transaction->getCredentials();

        $createTransactionRequest = array();

        if ($format == 'xml') {
            $createTransactionRequest['@xmlns'] = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd';
        }

        $merchantAuthentication = array(
            'name' => $credentials->getCredential('username'),
            'transactionKey' => $credentials->getCredential('password'),
        );

        $transactionRequest = array(
            "transactionType" => $this->getTransactionType($transaction)
        );

        if ($transaction->getType()->getValue() != Transaction\TransactionType::VOID) {
            $transactionRequest['amount'] = $transaction->getAmount();
        }

        $account = $transaction->getAccount();
        if (in_array($transaction->getType()->getValue(), array(
            Transaction\TransactionType::SALE,
            Transaction\TransactionType::AUTH,
            Transaction\TransactionType::REFUND))
        ){
            $payment = array();
            if ($account instanceof CardAccount) {
                if ($account instanceof SwipedCardAccount
                    && $transaction->getType()->getValue() != Transaction\TransactionType::REFUND
                ){
                    $payment['trackData'] = array();
                    if ($account->getTrackOne()) {
                        $payment['trackData']['track1'] = $account->getTrackOne();
                    } elseif ($account->getTrackTwo()) {
                        $payment['trackData']['track2'] = $account->getTrackTwo();
                    }
                } else {
                    $payment = array(
                        'creditCard' => array(
                            'cardNumber' => $account->getAccountNumber(),
                            'expirationDate' => $account->getExpMonth()->getLongMonth() . $account->getExpYear()->getShortYear(),
                        )
                    );

                    if (isset($options['enable_cvv']) && true === $options['enable_cvv']
                        && $transaction->getType()->getValue() != Transaction\TransactionType::REFUND) {
                        $payment['creditCard']['cardCode'] = $account->getCvv();
                    }
                }
            } elseif ($account instanceof BankAccount) {
                $payment = array(
                    'bankAccount' => array(
                        'accountType' => 'checking',
                        'routingNumber' => $account->getRoutingNumber(),
                        'accountNumber' => $account->getAccountNumber(),
                        'nameOnAccount' => $account->getName()
                    )
                );
            } else {
                throw new InvalidArgumentException(sprintf("Unsupported Account Type %s", get_class($account)));
            }

            $transactionRequest['payment'] = $payment;
        }

        if ($context['event_id']) {
            $transactionRequest['order'] = array();
            $transactionRequest['order']['invoiceNumber'] = $context['event_id'];
        }


        if (in_array($transaction->getType()->getValue(), array(
            Transaction\TransactionType::CAPTURE,
            Transaction\TransactionType::REFUND,
            Transaction\TransactionType::VOID))
        ) {
            $transactionRequest['refTransId'] = $transaction->getParent()->getResult()->getExternalId();
        }


        if (in_array($transaction->getType()->getValue(), array(
            Transaction\TransactionType::SALE,
            Transaction\TransactionType::AUTH))
        ) {
                $names = explode(' ', $account->getName(), 2);
                $firstName = isset($names[0]) ? $names[0] : '';
                $lastName = isset($names[1]) ? $names[1] : '';

                if (isset($context['email'])) {
                    $transactionRequest['customer'] = array(
                        'email' => $context['email']
                    );
                }

                $transactionRequest['billTo'] = array(
                    'firstName' => $firstName,
                    'lastName' => $lastName,
                    'address' => $account->getAddress(),
                    'city' => $account->getCity(),
                    'state' => $account->getRegion(),
                    'zip' => $account->getPostalCode(),
                    'country' => $account->getCountry(),
                );
        }

        if ($account instanceof SwipedCardAccount
            && !in_array($transaction->getType()->getValue(), array(Transaction\TransactionType::REFUND, Transaction\TransactionType::VOID))
        ){
            $transactionRequest['retail'] = array(
                'marketType' => 2,
                'deviceType' => 7
            );
        }

        if (isset($context['test']) && $context['test'])  {
            $transactionRequest['transactionSettings']['setting'][] = array(
                'settingName' => 'testRequest',
                'settingValue' => 'true'
            );
        }

        if (isset($context['userFields'])) {
            $transactionRequest['userFields'] = array();
            $transactionRequest['userFields']['userField'] = array();
            foreach ($context['userFields'] as $name => $value) {
                $transactionRequest['userFields']['userField'][] = array(
                    'name' => $name,
                    'value' => $value
                );
            }
        }

        $createTransactionRequest['merchantAuthentication'] = $merchantAuthentication;
        $createTransactionRequest['transactionRequest'] = $transactionRequest;

        return $createTransactionRequest;
    }
134856
private function parseSubject(\DOMElement $xml) : void
    {
        /** @var \DOMElement[] $subject */
        $subject = Utils::xpQuery($xml, './saml_assertion:Subject');
        if (empty($subject)) {
            throw new \Exception('Missing subject in subject query.');
        } elseif (count($subject) > 1) {
            throw new \Exception('More than one <saml:Subject> in subject query.');
        }

        /** @var \DOMElement[] $nameId */
        $nameId = Utils::xpQuery($subject[0], './saml_assertion:NameID');
        if (empty($nameId)) {
            throw new \Exception('Missing <saml:NameID> in <saml:Subject>.');
        } elseif (count($nameId) > 1) {
            throw new \Exception('More than one <saml:NameID> in <saml:Subject>.');
        }
        $this->nameId = new NameID($nameId[0]);
    }
642155
public function toXML(DOMElement $parent) : DOMElement
    {
        $doc = $parent->ownerDocument;

        $e = $doc->createElementNS(XMLSecurityDSig::XMLDSIGNS, 'ds:KeyInfo');
        $parent->appendChild($e);

        if ($this->Id !== null) {
            $e->setAttribute('Id', $this->Id);
        }

        foreach ($this->info as $n) {
            $n->toXML($e);
        }

        return $e;
    }
1095557
public function handleVersionTwo(Transaction $transaction): Transaction
    {
        $transaction->id = Hash::sha256($transaction->serialize())->getHex();

        return $transaction;
    }
1063064
final public function write($xml)
    {

        if($xml instanceof \SimpleXMLElement){
            $xml = $xml->asXML();
        } elseif($xml instanceof \DOMDocument){
            $xml = $xml->saveXML();
        }

        $this->log("SENT: ".$xml);

        return parent::write($xml);
    }
342915
public function toXML(DOMElement $parent) : DOMElement
    {
        Assert::same($this->getElement()->namespaceURI, Constants::NS_SAML);
        Assert::same($this->getElement()->localName, "AttributeValue");

        return Utils::copyElement($this->element, $parent);
    }
499111
public function toXML(DOMElement $parent) : DOMElement
    {
        Assert::stringNotEmpty($this->Algorithm, 'Cannot convert DigestMethod to XML without an Algorithm set.');

        $doc = $parent->ownerDocument;
        $e = $doc->createElementNS(Common::NS, 'alg:DigestMethod');
        $parent->appendChild($e);
        $e->setAttribute('Algorithm', $this->Algorithm);

        return $e;
    }
1161586
public function getXML(\DOMDocument $document)
    {
        $xml = $document->createElement(sprintf('%s:TransactionDetails', $this->namespace));

        $comments = $document->createElement('ns1:Comments');
        $comments->textContent = $this->comments;

        $xml->appendChild($comments);

        if (!empty($this->invoiceNumber)) {
            $invoiceNumber = $document->createElement('ns1:InvoiceNumber');
            $invoiceNumber->textContent = $this->invoiceNumber;

            $xml->appendChild($invoiceNumber);
        }

        return $xml;
    }
1238437
public function toXML(DOMElement $parent) : DOMElement
    {
        Assert::notNull($this->Method, "Cannot convert SubjectConfirmation to XML without a Method set.");

        $e = $parent->ownerDocument->createElementNS(Constants::NS_SAML, 'saml:SubjectConfirmation');
        $parent->appendChild($e);

        /** @psalm-suppress PossiblyNullArgument */
        $e->setAttribute('Method', $this->Method);

        if ($this->NameID !== null) {
            $this->NameID->toXML($e);
        }
        if ($this->SubjectConfirmationData !== null) {
            $this->SubjectConfirmationData->toXML($e);
        }

        return $e;
    }
685603
public static function fromXML(\DOMElement $xml) : Message
    {
        if ($xml->namespaceURI !== Constants::NS_SAMLP) {
            throw new \Exception('Unknown namespace of SAML message: '.var_export($xml->namespaceURI, true));
        }

        switch ($xml->localName) {
            case 'AttributeQuery':
                return new AttributeQuery($xml);
            case 'AuthnRequest':
                return new AuthnRequest($xml);
            case 'LogoutResponse':
                return new LogoutResponse($xml);
            case 'LogoutRequest':
                return new LogoutRequest($xml);
            case 'Response':
                return new Response($xml);
            case 'ArtifactResponse':
                return new ArtifactResponse($xml);
            case 'ArtifactResolve':
                return new ArtifactResolve($xml);
            default:
                throw new \Exception('Unknown SAML message: '.var_export($xml->localName, true));
        }
    }
409300
protected function getBlock(\DOMElement $element) {
    $block = '';
    /** @var \DOMNode $node */
    foreach ($element->childNodes as $node) {
      switch ($node->nodeType) {

        case XML_ELEMENT_NODE:
          /** @var \DOMElement $node */
          switch ($node->nodeName) {

            // Explicitly handle these elements.

            case 'i':
            case 'em':
              $block .= '*' . $this->getBlock($node) . '*';
              break;

            case 'b':
            case 'strong':
              $block .= '**' . $this->getBlock($node) . '**';
              break;

            case 'a':
              if ($node->hasAttribute('href')) {
                $block .= '[' . $this->getBlock($node) . ']' .
                  '(' . $node->getAttribute('href') . ')';
              }
              break;

            // Other inline elements.
            default:
              // Recursively handle any descendant elements we care about.
              $block .= $this->getBlock($node);
              break;

          }
          break;

        case XML_TEXT_NODE:
          /** @var \DOMText $node */
          $block .= self::escape($node->textContent);
          break;

      }
    }

    // Handle white-listed elements.
    if (in_array($element->nodeName, $this->white_list)) {
      $tag = $element->cloneNode();
      if (!empty($block)) {
        $tag->appendChild(new \DOMText($block));
      }
      $block = $this->dom->saveXML($tag);
    }

    return $block;
  }
557416
public function toXML(DOMElement $parent) : DOMElement
    {
        $doc = $parent->ownerDocument;

        $e = $doc->createElementNS(EntityAttributes::NS, 'mdattr:EntityAttributes');
        $parent->appendChild($e);

        /** @var \SAML2\XML\saml\Attribute|\SAML2\XML\Chunk $child */
        foreach ($this->children as $child) {
            $child->toXML($e);
        }

        return $e;
    }
1201784
public function addSoapOperation(DOMElement $binding, $soapAction)
    {
        $soapOperation = $this->dom->createElement('soap:operation');
        $soapOperation->setAttribute('soapAction', $soapAction);

        $binding->insertBefore($soapOperation, $binding->firstChild);

        return $soapOperation;
    }
1064173
public static function fromXml(\DOMElement $xml)
    {
        $static = new static();
        $static->libraryItemMetadata = LibraryItemMetadata::fromXml($xml);

        $expenseId = $xml->getElementsByTagName('id');
        $static->expenseId = StringLiteral::fromXml($expenseId);

        $date = $xml->getElementsByTagName('date');
        $static->date = DateTime::fromXml($date);

        $amount = $xml->getElementsByTagName('amount');
        $static->amount = FloatLiteral::fromXml($amount);

        $stringLiterals = array(
            'title' => $xml->getElementsByTagName('title'),
            'description' => $xml->getElementsByTagName('description'),
            'type' => $xml->getElementsByTagName('type'),
        );
        foreach ($stringLiterals as $propertyName => $xmlTag) {
            $static->$propertyName = StringLiteral::fromXml($xmlTag);
        }

        return $static;
    }
668066
public function getXML(\DOMDocument $document)
    {
        $xml = $document->createElement('ns1:Payment');

        if (!empty($this->hostedDataId)) {
            $hostedDataId = $document->createElement('ns1:HostedDataID');
            $hostedDataId->textContent = $this->hostedDataId;

            $xml->appendChild($hostedDataId);
        }

        if (!empty($this->amount)) {
            $amount                = $document->createElement('ns1:ChargeTotal');
            $amount->textContent   = $this->amount;
            $currency              = $document->createElement('ns1:Currency');
            $currency->textContent = self::CURRENCY_EUR;

            $xml->appendChild($amount);
            $xml->appendChild($currency);
        }

        return $xml;
    }
264259
public function handleVersionOne(Transaction $transaction): Transaction
    {
        if (isset($transaction->secondSignature)) {
            $transaction->signSignature = $transaction->secondSignature;
        }

        if (Types::VOTE === $transaction->type) {
            $transaction->recipientId = Address::fromPublicKey($transaction->senderPublicKey, $transaction->network);
        }

        if (Types::MULTI_SIGNATURE_REGISTRATION === $transaction->type) {
            $transaction->asset['multisignature']['keysgroup'] = array_map(function ($key) {
                return '+'.$key;
            }, $transaction->asset['multisignature']['keysgroup']);
        }

        if (isset($transaction->vendorFieldHex)) {
            $transaction->vendorField = hex2bin($transaction->vendorFieldHex);
        }

        if (! isset($transaction->id)) {
            $transaction->id = $transaction->getId();
        }

        if (Types::SECOND_SIGNATURE_REGISTRATION === $transaction->type) {
            $transaction->recipientId = Address::fromPublicKey($transaction->senderPublicKey, $transaction->network);
        }

        if (Types::MULTI_SIGNATURE_REGISTRATION === $transaction->type) {
            $transaction->recipientId = Address::fromPublicKey($transaction->senderPublicKey, $transaction->network);
        }

        return $transaction;
    }