Files
lelong/app/Services/BillplzService.php
T
2025-05-06 10:12:20 +08:00

50 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use Billplz\Client as BillplzClient;
use Http\Client\Common\HttpMethodsClient;
use GuzzleHttp\Client as GuzzleHttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Laravie\Codex\Exceptions\NotFoundException;
class BillplzService
{
protected $client;
public function __construct()
{
// Buat HttpMethodsClient dengan GuzzleHttpClient dan GuzzleMessageFactory
$http = new HttpMethodsClient(
new GuzzleHttpClient(), // Guzzle HTTP client
new GuzzleMessageFactory() // Guzzle message factory untuk request dan response
);
// Get API key dan base URL dari environment
$apiKey = env('BILLPLZ_API_KEY');
$baseUrl = env('BILLPLZ_API_URL', 'https://api.billplz.com'); // Default production URL
// Mencipta client Billplz dengan adapter HTTP
$this->client = new BillplzClient($http, $apiKey, $baseUrl);
}
public function testGetBill(string $billId)
{
try {
// Minta details bill berdasarkan ID
$response = $this->client->bill()->get($billId);
// Pastikan respons berjaya
if ($response->isSuccessful()) {
return $response->toArray(); // Kembalikan hasil dalam bentuk array
}
return ['error' => 'Request failed', 'details' => $response->toArray()];
} catch (NotFoundException $e) {
return ['error' => 'Bill not found', 'details' => $e->getMessage()];
} catch (\Throwable $e) {
return ['error' => 'Unexpected error', 'details' => $e->getMessage()];
}
}
}