<?php

require_once '../../vendor/autoload.php';
require_once '../../include/DatabaseConn.php';
require_once '../../include/Constants.php';
require_once '../../models/Invoice.php';

$db = new DatabaseConn();
$link = $db->connect();

$std = new stdClass();

$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
$email = filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL);

header("content-type: application/json");

if (is_null($id) || is_null($email)) {
    $error = 'Token missing in request body';
    $std->error = $error;
    echo (json_encode($std));
    die();
} else {
    $paid = PAID;
    $time = time();
    Invoice::confirmInvoice($id, $link, $email, $time);
    if (mysqli_affected_rows($link) == 1) {
        $std->msg = PAID;
        echo (json_encode($std));
        die();
    } else {
        $error = 'Already updated';
        $std->error = $error;
        echo (json_encode($std));
        die();
    }
}

