<?php
header("Access-Control-Allow-Origin: *");
//////////// SAAS ENV ////////////////////////////////////////////////////
require_once("class.restframework.php");
require_once("class.saas.php");
include_once('class.setting.php');
include_once('class.pesapal.php');
//////////////////////////////////////////////////////////////////////////
$restapi = new RestFrameWork();
// only allow get method
$saas = new SaaS($restapi);
$auth = $saas->_initAuth();
////////////////////////////////////////////////////////////////////////
$kpesapal = new Pesapal($auth, $restapi);
$settings = $kpesapal->settings;
////////////////////////////////////////////////////////////////////////////////////
# Get UUID
if (isset($_GET["get_uuid"])) {
    header("Content-Type: application/json; charset=UTF-8");
    http_response_code(200);
    echo json_encode($kpesapal->setting->getUUID());
}
////////////////////////////////////////
if (isset($_GET["get_settings"])) {
    header("Content-Type: application/json; charset=UTF-8");
    http_response_code(200);
    // $settings
    // echo json_encode();
}
//////////////////////////////////////////
if (isset($_GET["get_token"])) {
    header("Content-Type: application/json; charset=UTF-8");
    http_response_code(200);
    echo json_encode($kpesapal->getToken());
}
////////////////////////////////////////
if (isset($_GET["get_all_transactions"])) {
    header("Content-Type: application/json; charset=UTF-8");
    http_response_code(200);
    echo json_encode($pesapal->getAllTransations());
}
/////////////////////////////////////////////
if (isset($_GET["save_transaction"])) {
    header("Content-Type: application/json; charset=UTF-8");
    $data_object = file_get_contents("php://input");
    if (!empty($data_object)) {
        $data = json_decode($data_object);
        $response = $pesapal->saveTransation($data);
        http_response_code(200);
        echo json_encode($response);
    } else {
        echo json_encode(array("success" => true, "message" => "Incomplete data request"));
    }
}
