<?php

namespace App\Classes\Modules\Billplzs\Services;

use App\Classes\Exceptions\MalformedRequestException;
use Illuminate\Support\Facades\Http;

class GetBillplzBill
{

    /**
     * @param string $billPlzId
     * @return null|object
     * @throws MalformedRequestException
     */
    public function execute(string $billPlzId) {
        try{
            $response = Http::withBasicAuth(config('billplz.api_key').':', '')->get(config('billplz.base_url').'/api/v3/bills/'.$billPlzId);

            if($response->successful()){
                $data = $response->json();

                return (object) $data;
            }else{
                return null;
            }
        }catch(\Exception $exception){
            throw new MalformedRequestException('Unable to get correct response from billplz server');
        }
    }
}