<?php

namespace App\ThirdParty\Xendit;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

class Xendit
{
    public function createPaymentLink($payload){

        $url = "https://api.xendit.co/v2/invoices";
        $key = env("XENDIT_PRIVATE_KEY");


        $payload['success_redirect_url'] = route('orders');
        $payload['failure_redirect_url'] = route('orders');

        $res = Http::withBasicAuth($key, "")
            ->withBody(json_encode($payload), 'application/json')
            ->post($url);

        if ($res->status() == 200)
            return $res->json();

        return null;
    }


    public function checkInvoiceStatus(string $invoiceId)
    {
        $url = $this->baseUrl . "/v2/invoices/{$invoiceId}";

        $res = Http::withBasicAuth($this->key, "")
            ->get($url);

        if ($res->successful()) {
            return $res->json();
        }

        Log::error("Xendit Check Invoice Failed", [
            'invoice_id' => $invoiceId,
            'status' => $res->status(),
            'body' => $res->body()
        ]);

        return null;
    }
}
