tambah function semakan billplz

This commit is contained in:
BTMS2
2025-05-06 10:12:20 +08:00
parent d95767c5f9
commit 791ca8dd84
8 changed files with 580 additions and 1 deletions
+49
View File
@@ -0,0 +1,49 @@
<?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()];
}
}
}