<?php

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

$accountid = $database->get("accounts", ["[>]authkeys" => ["accountid"]], "accounts.accountid", ["key" => $VARS["key"]]);

$amount = $VARS["amount"] * 1.0;

if ($amount <= 0 || $amount > 999.99) {
    sendJsonResp($Strings->get("Amount must be more than zero and less than 999.99.", false), "ERROR");
}

do {
    $txcode = substr(hash("sha256", random_bytes(100)), 0, 20);
} while ($database->has("merchant_transactions", ["txcode" => $txcode]));

$database->insert("merchant_transactions", ["txcode" => $txcode, "amount" => $amount, "merchantid" => $accountid]);

exitWithJson([
    "status" => "OK",
    "txcode" => $txcode,
    "amount" => $amount
]);