<?php
require_once '../configuration.php';
require_once SDK_ROOTPATH . '/../vendor/autoload.php';


//Payment reverse
try {
    //Generating pcidss order to reverse see more https://docs.fondy.eu/docs/page/4/
    $TestOrderData = [
        'order_id' => time(),
        'card_number' => '4444555511116666',
        'cvv2' => '333',
        'expiry_date' => '1232',
        'currency' => 'USD',
        'amount' => 1000,
        'client_ip' => '127.2.2.1'
    ];
    // Call method to generate order for refund
    $reverse_order_data = Cloudipsp\Pcidss::start($TestOrderData); //next order will be refunded
    // Refund request always generated by merchant using host-to-host
    // Required param to refund it "order_id" see more https://docs.fondy.eu/docs/page/12/
    if ($reverse_order_data->isApproved()) {// Checking if prev payment valid(signature)
        $dataToReverse = [
            'currency' => 'USD',
            'amount' => 100,
            'order_id' => $TestOrderData['order_id']
        ];
        $refund_order = Cloudipsp\Order::reverse($dataToReverse);
    }
    //getting returned data
    ?>
    <!doctype html>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title>Order refund</title>
        <style>
            table tr td, table tr th {
                padding: 10px;
            }
        </style>
    </head>
    <body>
    <table style="margin: auto" border="1">
        <thead>
        <tr>
            <th style="text-align: center" colspan="2">Request data</th>
        </tr>
        <tr>
            <th style="text-align: left"
                colspan="2"><?php printf("<pre>%s</pre>", json_encode(['request' => $dataToReverse], JSON_PRETTY_PRINT)) ?></th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Response status:</td>
            <td><?= $refund_order->getData()['response_status'] ?></td>
        </tr>
        <tr>
            <td>Normal response:</td>
            <td>
                <pre><?php print_r($refund_order->getData()); ?></pre>
            </td>
        </tr>
        <tr>
            <td>Check if order is refunded:</td>
            <td><?php var_dump($refund_order->isReversed()); ?></td>
        </tr>
        </tbody>
    </table>
    </body>
    </html>
    <?php
} catch (\Exception $e) {
    echo "Fail: " . $e->getMessage();
}