<?php

require_once('../../vendor/autoload.php');
require_once('../../main_includes/init.php');

extract($_POST);
function generate_reference()
{
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $nums = '0123456789';
    $nums_length = strlen($nums);
    $reference = '';
    for ($i = 0; $i < 12; $i++) {

        $reference .= $nums[rand(0, $nums_length - 1)];
    }
    for ($i = 0; $i < 2; $i++) {

        $reference .= $characters[rand(0, $charactersLength - 1)];
    }
    for ($i = 0; $i < 3; $i++) {

        $reference .= $nums[rand(0, $nums_length - 1)];
    }
    return $reference;
}

$user = User::find_by_id($_SESSION['user_id']);
    if (!$user) {
        header("Location: ".ROOT_URL);
    }
    
    if (intval($amount) < 500) {
        // header("Location: ".ROOT_URL);
        return false;
    }

    $reference = generate_reference();

    $my_transaction= new MyTransaction;
    $my_transaction-> user_id = $user->id;
    $my_transaction-> amount = $amount;
    $my_transaction-> method = "initialized";
    $my_transaction-> reference = $reference;
    $my_transaction-> status = 'unverify';
    $my_transaction-> date_created = date('c');
    $my_transaction-> date_updated = date('c');

    $my_transaction->save();

    echo json_encode($my_transaction);