<?php

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf 
 */
class EPayNotifierDatei extends EPayNotifier
{
  
  public function doNotify($pEmailXml, $pEPayInfo, $pKurse, $pDateiendung = '.epaynotifyxml', $pPayonline = '') {
    $zentralsettings = ToolSettingsAPI::getToolSettings(0);
    
    $pEmailXml->addEPayInfo($pEPayInfo);
    if ($pPayonline == '') {
      $pPayonline = ePaymentApi::getPayonlineProviderKeyFromSession();
    }
    $pEmailXml->payonline = $pPayonline;

    $anmeldungNode = new XMLNode('ANMELDUNG');
    $anmeldungNode->addChild($pEmailXml->getXMLNode());
    $kurseNode = new XMLNode('KURSE');
    foreach ($pKurse as $kurs) {
      $kurseNode->addChild($kurs->getXMLNode());
    }
    $anmeldungNode->addChild($kurseNode);
    $rootNode = new XMLNode('ANMELDUNGEN');
    $rootNode->addChild(new XMLNode('MESSAGE_TYPE', 'EPAY_STATECHANGE'));
    $rootNode->addChild($anmeldungNode);
    $body = '<?xml version="1.0" encoding="' . CodingOperations::getInternalEncoding() . '" ?' . ">\r\n" .
            $rootNode->getXMLstring($rootNode);
    if ($zentralsettings->encryptXML) {
      $encodingResult = EncodingAPI::encodeComplete($body);
      if ($encodingResult !== false) {
        $body = $encodingResult;
      }
    }

    $bezeichnungsanhang = 'mtid' . $pEPayInfo->tidMerchant;
    $success = $this->commit($this->target, $body, $pEmailXml->getVarValue('name') . ', ' . $pEmailXml->getVarValue('vorname'), $pDateiendung, $bezeichnungsanhang);

    return $success;
  }

  
  protected function commit($pTo, $pBody, $pName = null, $pDateiendung = '.xml', $pBezeichnungsanhang = '') {
    $tmpfilefilename = SimpleFileWork::writeAnmeldeFile($pTo, $pBody, $pDateiendung, $pBezeichnungsanhang);
    
    return $tmpfilefilename !== false;
  }

}