<?php

/*
This file handles the confirmation call from the Scheme Operator (after a payment was received). It is called twice:
1. for Vitality-Check, according to "Abbildung 6-11: epsp:VitalityCheckDetails" (eps Pflichtenheft 2.5)
2. for the actual payment confirmation (Zahlungsbestätigung)
*/
require_once '../vendor/autoloader.php';
use at\externet\eps_bank_transfer;
/**
 * @param string $plainXml Raw XML message, according to "Abbildung 6-6: PaymentConfirmationDetails" (eps Pflichtenheft 2.5)
 * @param at\externet\eps_bank_transfer\BankConfirmationDetails $bankConfirmationDetails
 * @return true
 */
$paymentConfirmationCallback = function ($plainXml, $bankConfirmationDetails) {
    // Handle "eps:StatusCode": "OK" or "NOK" or "VOK" or "UNKNOWN"
    if ($bankConfirmationDetails->GetStatusCode() == 'OK') {
        // TODO: Do your payment completion handling here
        // You should use $bankConfirmationDetails->GetRemittanceIdentifier();
    }
    // True is expected to be returned, otherwise the Scheme Operator will be informed that the server could not accept the payment confirmation
    return true;
};
$soCommunicator = new eps_bank_transfer\SoCommunicator();
$soCommunicator->HandleConfirmationUrl($paymentConfirmationCallback, null, 'php://input', 'php://output');
$targetUrl = null;
// Target URL to send TransferInitiatorDetails to. 'null' means: Use default URL. For test mode, insert: https://routing.eps.or.at/appl/epsSO-test/transinit/eps/v2_5
// Return urls
$transferMsgDetails = new eps_bank_transfer\TransferMsgDetails('http(s)://yourdomain.example.com/eps_confirm.php?id=12345', 'http(s)://yourdomain.example.com/ThankYou.html', 'http(s)://yourdomain.example.com/Failure.html');
$transferInitiatorDetails = new eps_bank_transfer\TransferInitiatorDetails($userID, $pin, $bic, 'John Q. Public', $iban, '12345', '9999', $transferMsgDetails);
// Optional: Include ONE (i.e. not both!) of the following two lines:
$transferInitiatorDetails->RemittanceIdentifier = 'Order123';
// "Zahlungsreferenz". Will be returned on payment confirmation = epi:RemittanceIdentifier
$transferInitiatorDetails->UnstructuredRemittanceIdentifier = 'Order123';
// "Verwendungszweck". Will be returned on payment confirmation = epi:UnstructuredRemittanceIdentifier
// Optional:
$transferInitiatorDetails->SetExpirationMinutes(60);
// Sets ExpirationTimeout. Value must be between 5 and 60
// Optional: Include information about one or more articles = epsp:WebshopDetails
$article = new eps_bank_transfer\WebshopArticle('ArticleName', 1, 9999);
$transferInitiatorDetails->WebshopArticles[] = $article;
// Send TransferInitiatorDetails to Scheme Operator
$soCommunicator = new eps_bank_transfer\SoCommunicator();
// Send transfer initiator details to $targetUrl
$plain = $soCommunicator->SendTransferInitiatorDetails($transferInitiatorDetails, $targetUrl);
$xml = new \SimpleXMLElement($plain);
$soAnswer = $xml->children(eps_bank_transfer\XMLNS_epsp);
$errorDetails = $soAnswer->BankResponseDetails->ErrorDetails;
if ('' . $errorDetails->ErrorCode != '000') {
    $errorCode = '' . $errorDetails->ErrorCode;
    $errorMsg = '' . $errorDetails->ErrorMsg;
} else {
    // This is the url you have to redirect the client to.
    $redirectUrl = $soAnswer->BankResponseDetails->ClientRedirectUrl;
    header('Location: ' . $redirectUrl);
}