<?php
declare(strict_types=1);


function tripayRequest(
    string $endpoint,
    array $payload,
    array $config
): array {


    $curl = curl_init();


    curl_setopt_array($curl,[

        CURLOPT_URL =>
        $config['api_url'] . $endpoint,

        CURLOPT_RETURNTRANSFER => true,

        CURLOPT_HTTPHEADER => [

            "Authorization: Bearer "
            . $config['api_key'],

            "Content-Type: application/json"

        ],

        CURLOPT_POST => true,

        CURLOPT_POSTFIELDS =>
        json_encode($payload)

    ]);


    $response =
    curl_exec($curl);


    if($response === false){

        throw new Exception(
            curl_error($curl)
        );

    }


    curl_close($curl);


    return json_decode(
        $response,
        true
    );

}


function tripayGetChannels(array $config): array
{

    $curl = curl_init();


    curl_setopt_array($curl,[

        CURLOPT_URL =>
        $config['api_url']
        . "merchant/payment-channel",


        CURLOPT_RETURNTRANSFER => true,


        CURLOPT_HTTPHEADER => [

            "Authorization: Bearer "
            . $config['api_key'],

            "User-Agent: TCC-Billing/1.0"

        ]

    ]);


    $response =
    curl_exec($curl);


    if($response === false){

        throw new Exception(
            curl_error($curl)
        );

    }


    curl_close($curl);


    $data =
    json_decode(
        $response,
        true
    );


    return $data;

}
