<?php

namespace App\Classes\Modules\Billplzs\Services;

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

class DeletesBillplzBill
{

    /**
     * @param string $billID
     * @throws MalformedRequestException
     */
    public function execute(string $billID) {
        try{
            $response = Http::withBasicAuth(config('billplz.api_key').':', '')->delete(config('billplz.base_url').'/api/v3/bills/'.$billID);
            Log::info('DeletesBillplzBill bill with id '. $billID . ' response: '.json_encode($response));

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

                // $data['url'] = $data['url'].'?auto_submit=true';
                return (object) $data;
            }else{
                return null;
            }
        }catch(\Exception $exception){
            throw new MalformedRequestException('Unable to get correct response from billplz server: ' . $exception->getMessage());
        }
    }
}
