Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Services/Billing/BillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Vultr\VultrPhp\Services\Billing;

use Vultr\VultrPhp\Services\VultrService;
use Vultr\VultrPhp\Services\VultrServiceException;
use Vultr\VultrPhp\Util\ListOptions;

/**
Expand Down Expand Up @@ -66,4 +67,17 @@ public function getInvoiceItems(int $invoice_id, ?ListOptions &$options = null)
{
return $this->getListObjects("billing/invoices/{$invoice_id}/items", new InvoiceItem(), $options);
}

/**
* Retrieve the pending charges of all the service types of the account.
*
* @see https://www.vultr.com/api/#operation/pending-charges
* @throws BillingException
* @throws VultrServiceException
* @return PendingCharge[]
*/
public function getPendingCharges() : array
{
return $this->getNonPaginatedListObjects('billing/pending-charges', new PendingCharge());
}
}
112 changes: 112 additions & 0 deletions src/Services/Billing/PendingCharge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace Vultr\VultrPhp\Services\Billing;

use Vultr\VultrPhp\Util\Model;

/**
* Holds billing pending charges information.
*/
class PendingCharge extends Model
{
protected string $description;
protected string $product;
protected string $startDate;
protected string $endDate;
protected int $units;
protected string $unitType;
protected float $unitPrice;
protected float $total;

public function getDescription() : string
{
return $this->description;
}

public function setDescription(string $description) : void
{
$this->description = $description;
}

public function getProduct() : string
{
return $this->product;
}

public function setProduct(string $product) : void
{
$this->product = $product;
}

public function getStartDate() : string
{
return $this->startDate;
}

public function setStartDate(string $start_date) : void
{
$this->startDate = $start_date;
}

public function getEndDate() : string
{
return $this->endDate;
}

public function setEndDate(string $end_date) : void
{
$this->endDate = $end_date;
}

public function getUnits() : int
{
return $this->units;
}

public function setUnits(int $units) : void
{
$this->units = $units;
}

public function getUnitType() : string
{
return $this->unitType;
}

public function setUnitType(string $unit_type) : void
{
$this->unitType = $unit_type;
}

public function getUnitPrice() : float
{
return $this->unitPrice;
}

public function setUnitPrice(float $unit_price) : void
{
$this->unitPrice = $unit_price;
}

public function getTotal() : float
{
return $this->total;
}

public function setTotal(float $total) : void
{
$this->total = $total;
}

public function getResponseName() : string
{
return 'pending_charge';
}

public function getModelExceptionClass() : string
{
return str_replace('PendingCharge', 'Billing', parent::getModelExceptionClass());
}
}
37 changes: 37 additions & 0 deletions src/Services/VultrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ protected function getListObjects(string $uri, ModelInterface $model, ?ListOptio
return $objects;
}

/**
* @param $uri - string - the url address to query after api.vultr.com/v2
* @param $model - ModelInterface - the object that will be mapped to the get response.
* @throws Child of VultrServiceObject
* @return ModelInterface[]
*/
protected function getNonPaginatedListObjects(string $uri, ModelInterface $model) : array
{
try
{
$response = $this->getClientHandler()->get($uri);
$stdclass = VultrUtil::decodeJSON((string)$response->getBody());

// Get the list name from the model
$list_name = $model->getResponseListName();

// Ensure the expected list exists
if (!isset($stdclass->$list_name)) {
throw new VultrServiceException("Response does not contain expected list: $list_name");
}

// Map each item in the list to a model instance
$objects = [];
foreach ($stdclass->$list_name as $object) {
$objects[] = VultrUtil::mapObject($object, clone $model);
}

return $objects;
}
catch (Throwable $e)
{
$exception_class = $model->getModelExceptionClass();
throw new $exception_class('Failed to list '.$model->getResponseListName().': '.$e->getMessage(), $e->getHTTPCode(), $e);
}
}


/**
* @param $uri - string - the url address to query after api.vultr.com/v2
* @param $model - ModelInterface - the object model that we are updating. This needs to be a fully initialized object.
Expand Down
29 changes: 29 additions & 0 deletions tests/Data/BillingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,33 @@ protected function dataGetInvoices() : array
}
', true);
}
protected function dataGetPendingCharges() : array
{
return json_decode('
{
"pending_charges": [
{
"description": "Load Balancer (my-loadbalancer)",
"start_date": "2020-10-10T01:56:20+00:00",
"end_date": "2020-10-10T01:56:20+00:00",
"units": 720,
"unit_type": "hours",
"unit_price": 0.0149,
"total": 10,
"product": "Load Balancer"
},
{
"description": "65.65.65.65 (1024 MB) [my-instance]",
"start_date": "2024-11-01T00:00:00+00:00",
"end_date": "2024-11-19T11:20:52+00:00",
"units": 444,
"unit_type": "hours",
"unit_price": 0.00744047619047619,
"total": 3.31,
"product": "Vultr Cloud Compute"
}
]
}
', true);
}
}
29 changes: 29 additions & 0 deletions tests/Suite/BillingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Vultr\VultrPhp\Services\Billing\BillingException;
use Vultr\VultrPhp\Services\Billing\Invoice;
use Vultr\VultrPhp\Services\Billing\InvoiceItem;
use Vultr\VultrPhp\Services\Billing\PendingCharge;
use Vultr\VultrPhp\Tests\VultrTest;

class BillingTest extends VultrTest
Expand Down Expand Up @@ -88,4 +89,32 @@ public function testGetInvoiceItems()
$this->expectException(BillingException::class);
$client->billing->getInvoiceItems(123456);
}

public function testGetPendingCharges()
{
$data = $this->getDataProvider()->getData();

$client = $this->getDataProvider()->createClientHandler([
new Response(200, ['Content-Type' => 'application/json'], json_encode($data)),
new Response(400, [], json_encode(['error' => 'Bad Request'])),
]);

foreach ($client->billing->getPendingCharges() as $pendingCharge)
{
$this->assertInstanceOf(PendingCharge::class, $pendingCharge);
foreach ($data[$pendingCharge->getResponseListName()] as $expectedCharge)
{
if ($expectedCharge['unit_price'] !== $pendingCharge->getUnitPrice()) continue;

foreach ($pendingCharge->toArray() as $prop => $prop_val)
{
$this->assertEquals($prop_val, $expectedCharge[$prop], "Prop {$prop} failed");
}
break;
}
}

$this->expectException(BillingException::class);
$client->billing->getPendingCharges();
}
}
Loading