<?php

namespace App\Classes\Modules\Accounting\Processors;

use App\Classes\Exceptions\MalformedRequestException;
use App\Models\Transaction;
use Illuminate\Support\Facades\Log;
use \Illuminate\Database\Eloquent\Collection;

class ListShippingPortalTransactions
{
    public function execute($filters)
    {
        try {
            $url = 'https://izyim.cief-malaysia.com/public/api/v1/transactions/mappable/query/with-details';
            $client = new \GuzzleHttp\Client(['verify' => false]);
            $response = $client->request('GET', $url . '?api-key=510acd13d8d24375cf038ad626c282565451461a9c2399357e0b65365300787e&filters=' . json_encode($filters));
            $body = $response->getBody();
            $data = json_decode($body, true);
            $payload = $data['payload'];
            return $data['payload']['data'];

        } catch (\Exception $exception) {
            // dd($exception->getMessage());
            throw new MalformedRequestException($exception->getMessage());
            //                preg_match('/\{.*\}/s', $exception->getMessage(), $matches);
            //                $jsonError = json_decode($matches[0]);
            // Retrieved Transactions failed
            //                throw new MalformedRequestException($jsonError->title);
        }

        // if not found
        throw new MalformedRequestException('Bill Number Not Found.');
    }
}
