<?php
class EnparaAPI {
    private $base_url = 'https://api.enpara.com/v1';
    private $bind_ip = '185.250.240.217';

    public function getStatement($client_id, $client_secret, $access_token, $account) {
        
        $ch = curl_init();

        $startDateTime = date('Y-m-d\TH:i:sP', strtotime('-1 day'));   // +03:00 offset
        $endDateTime   = date('Y-m-d\TH:i:sP');

        $query = http_build_query([
            'accountNo'     => (string)$account,
            'startDateTime' => $startDateTime,
            'endDateTime'   => $endDateTime
        ]);

        $url = $this->base_url . "/account-statement?" . $query;

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_INTERFACE, $this->bind_ip);

        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: Bearer ' . trim($access_token),
            'Client-Id: ' . trim($client_id),
            'Client-Secret: ' . trim($client_secret),
            'Content-Type: application/json',
            'Accept: application/json'
        ]);

        curl_setopt($ch, CURLOPT_POSTFIELDS, '');

        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $headers = substr($response, 0, $header_size);
        $body = substr($response, $header_size);

        curl_close($ch);

        return [
            'http_code' => $http_code,
            'headers'   => $headers,
            'body'      => $body
        ];
    }
}