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

class EPayNotifyRequestHandlerPayPal extends EPayNotifyRequestHandler {

  public function getEPayInfo() {
    $token = GetOperations::getVarValue('token');
    $payerId = GetOperations::getVarValue('PayerID');
    $paymentDetails = PayPal::getTransactionDetails($token);

    $epayInfo = new EPayInfo();
    $epayInfo->payerid = $payerId;
    $epayInfo->tid = $paymentDetails['TRANSACTIONID'];
    $epayInfo->tidMerchant = $this->getMerchantTransactionId($epayInfo->tid);
    $epayInfo->amount = MathOperations::floatToString($paymentDetails['AMT']);

    $wes = DBWorkWebEpaystate::getDefaultInstance()->getEpaystateByTransactionId($epayInfo->tid);
    $epayInfo->paymentState = $wes->payment_state;
    return $epayInfo;
  }

  public function getEmailXML() {
    $emailxml = new EMailXML();
    return $emailxml;
  }

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

  protected function validateRequestStructure() {
    return GetOperations::isDefined('token') && GetOperations::isDefined('PayerID');
  }

  protected function validateRequestContent() {
    $paymentDetails = PayPal::getTransactionDetails(GetOperations::getVarValue('token'));
    return DBWorkWebEpayTids::getDefaultInstance()->transactionIdExists($paymentDetails['TRANSACTIONID'], EPAYMENT_PAYPAL);
  }

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