<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class EPayNotifyRequestHandlerPmPayment extends EPayNotifyRequestHandler {

  public function getEPayInfo() {
    $tid = RequestOperations::getVarValue('txid');
    $paymentState = PmPaymentTransactionStatus::fromRequest();
    $epayInfo = new EPayInfo();
    $epayInfo->tid = $tid;
    $epayInfo->tidMerchant = $this->getMerchantTransactionId($tid);
    $epayInfo->amount = $this->getAmount();
    $epayInfo->paymentState = $paymentState->status;
    $epayInfo->paymentDate = $paymentState->created_at;
    $epayInfo->institution = RequestOperations::getVarValue('payment_method');
    return $epayInfo;
  }

  public function getEmailXML() {
    $paymentState = PmPaymentTransactionStatus::fromRequest();
    $emailxml = new EmailXML();
    $emailxml->epay_ordertime = $paymentState->created_at;
    $emailxml->epay_amount = $this->getAmount();
    return $emailxml;
  }

  public function getEmailXMLKurse() {
    $emailxmlkurse = new EmailXMLKurse(); 
    $emailxmlkurse->kursnummer = "dummykurs"; 
    return array($emailxmlkurse);
  }

  protected function validateRequestStructure() {
    return RequestOperations::isDefined('ags')
            && RequestOperations::isDefined('txid')
            && RequestOperations::isDefined('amount')
            && RequestOperations::isDefined('desc')
            && RequestOperations::isDefined('status')
            && RequestOperations::isDefined('payment_method')
            && RequestOperations::isDefined('created_at');
  }

  
  protected function validateRequestContent() {
    return RequestOperations::getVarValue('ags') != ""
            && RequestOperations::getVarValue('txid') != ""
            && RequestOperations::getVarValue('amount') != ""
            && RequestOperations::getVarValue('status') != ""
            && RequestOperations::getVarValue('payment_method') != ""
            && RequestOperations::getVarValue('created_at') != "";
  }

  private function getMerchantTransactionId($tid) {
    $dbw = DBWorkWebEpayTids::getDefaultInstance();
    return $dbw->getMerchantTransactionIdByTransactionId($tid);
  }

  private function getAmount() {
    return MathOperations::floatToString(RequestOperations::getVarValue('amount') / 100);
  }
}