<?php
$md5signature = md5("<command>getTransactionDetail</command><vendor>".SAGEPAY_VENDOR."</vendor><user>".SAGEPAY_MTC_USER."</user><vendortxcode>".$order->payment['VendorTxCode']."</vendortxcode><password>".SAGEPAY_MTC_PASSWORD."</password>");
$xml = '<vspaccess><command>getTransactionDetail</command><vendor>'.SAGEPAY_VENDOR.'</vendor><user>'.SAGEPAY_MTC_USER.'</user><vendortxcode>'.$order->payment['VendorTxCode'].'</vendortxcode><signature>'.$md5signature.'</signature></vspaccess>';

$fields_string = "XML=$xml";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https:live.sagepay.com/access/access.htm");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
if (isset($disable_curl_ssl_check)) {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
$response = curl_exec($ch);
curl_close($ch);

$output = simplexml_load_string($response);

if ($order->hasOutstandingDeferredPayment()) {
    echo showmsg("Payment on order has not been captured yet", "warning", $path);
    ?>
    <form action="/shop/admin/orders/sage_capture.php" method="post" style="display: inline-block; margin-right: 50px; width: auto;">
        <input type="hidden" name="order_id" value="<?= (int)$order->getId() ?>">
        <input type="hidden" name="transaction_id" value="<?= (int)$order->payment['id'] ?>">
        <button type="submit" class="btn">
            Capture
        </button>
    </form>
    <form action="/shop/admin/orders/sage_void.php" method="post" style="display: inline-block; width: auto;">
        <input type="hidden" name="order_id" value="<?= (int)$order->getId() ?>">
        <input type="hidden" name="transaction_id" value="<?= (int)$order->payment['id'] ?>">
        <button type="submit" class="btn delete">
            Void
        </button>
    </form>
    <?php
}
?>

<table>
    <tr>
        <td class="fieldname">Order ID</td>
        <td><?= $order->payment['VendorTxCode'];?></td>
    </tr>
    <tr>
        <td class="fieldname">VPSTxId</td>
        <td><?= $order->payment['VPSTxId'];?></td>
    </tr>
    <tr>
        <td class="fieldname">TxAuthNo </td>
        <td><?= $order->payment['TxAuthNo'];?></td>
    </tr>
    <tr>
        <td class="fieldname">Amount </td>
        <td><?= $order->payment['Amount'];?></td>
    </tr>
    <?php /*?>
    <tr>
        <td class="fieldname">Amount Released</td>
        <td><?= $order->payment['released_amount'];?></td>
    </tr>
    <tr>
        <td class="fieldname">Amount Remaining</td>
        <td><?= $order->payment['AmountRemaining'];?></td>
    </tr>
    <?php */ ?>
    <tr>
        <td class="fieldname">AVSCV2</td>
        <td><?= $order->payment['AVSCV2'];?></td>
    </tr>
    <tr>
        <td class="fieldname">AddressResult</td>
        <td><?= $order->payment['AddressResult'];?></td>
    </tr>
    <tr>
        <td class="fieldname">PostCodeResult</td>
        <td><?= $order->payment['PostCodeResult'];?></td>
    </tr>
    <tr>
        <td class="fieldname">CV2Result</td>
        <td><?= $order->payment['CV2Result'];?></td>
    </tr>
    <tr>
        <td class="fieldname">GiftAid</td>
        <td><?= $order->payment['GiftAid'];?></td>
    </tr>
    <tr>
        <td class="fieldname">3DSecureStatus</td>
        <td><?= $order->payment['3DSecureStatus'];?></td>
    </tr>
    <tr>
        <td class="fieldname">CAVV</td>
        <td><?= $order->payment['CAVV'];?></td>
    </tr>


    <?php if ($output): ?>

        <tr>
            <td class="fieldname">3rd Man Score</td>
            <td><?= $output->t3mscore;?></td>
        </tr>
        <tr>
            <td class="fieldname">3rd Man Action</td>
            <td><?= $output->t3maction;?></td>
            <td>
                <?php if ($output->t3maction == 'OK') { ?>
                <img src="/admin/images/tick.png" alt="T3M Action: <?= $output->t3maction;?>"/>
                <?php } else { ?>
                <img src="/admin/images/warning.png" alt="T3M Action: <?= $output->t3maction;?>"/>
                <?php } ?>
            </td>
        </tr>
        <tr>
            <td class="fieldname">3rd Man ID</td>
            <td><?= $output->t3mid;?></td>
        </tr>

    <?php endif ?>

</table>
