Skip to content

Commit

Permalink
Adding filters to invoices search.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet committed Nov 5, 2024
1 parent 28197bf commit 74257cf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
9 changes: 9 additions & 0 deletions examples/get_invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}

// Get 2 invoices, skip 2
try {
echo 'Get invoices:' . PHP_EOL;
$client = new Invoice($host, $apiKey);
var_dump($client->getAllInvoices($storeId, 2,2));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
88 changes: 80 additions & 8 deletions src/Client/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,98 @@ public function getInvoice(
}
}

public function getAllInvoices(string $storeId): InvoiceList
{
return $this->_getAllInvoicesWithFilter($storeId, null);
public function getAllInvoices(
string $storeId,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, null, null, null, null, null, $take, $skip);
}

public function getInvoicesByOrderIds(string $storeId, array $orderIds): InvoiceList
{
return $this->_getAllInvoicesWithFilter($storeId, $orderIds);
public function getInvoicesByOrderIds(
string $storeId,
array $orderIds,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, $orderIds, null, null, null, null, $take, $skip);
}

public function getInvoicesByText(
string $storeId,
string $text,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, null, $text, null, null, null, $take, $skip);
}

public function getInvoicesByStatus(
string $storeId,
array $status,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, null, null, $status, null, null, $take, $skip);
}

private function _getAllInvoicesWithFilter(
public function getInvoicesByStartDate(
string $storeId,
array $filterByOrderIds = null
int $startDate,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, null, null, null, $startDate, null, $take, $skip);
}

public function getInvoicesByEndDate(
string $storeId,
int $endDate,
int $take = null,
int $skip = null
): InvoiceList {
return $this->getAllInvoicesWithFilter($storeId, null, null, null, null, $endDate, $take, $skip);
}

/**
* @see https://docs.btcpayserver.org/API/Greenfield/v1/#operation/Invoices_GetInvoices
*/
public function getAllInvoicesWithFilter(
string $storeId,
array $filterByOrderIds = null,
string $filterByText = null,
array $filterByStatus = null,
int $filterByStartDate = null,
int $filterByEndDate = null,
int $take = null,
int $skip = null
): InvoiceList {
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices?';
if ($filterByOrderIds !== null) {
foreach ($filterByOrderIds as $filterByOrderId) {
$url .= 'orderId=' . urlencode($filterByOrderId) . '&';
}
}
if ($filterByText !== null) {
$url .= 'textSearch=' . urlencode($filterByText) . '&';
}
if ($filterByStatus !== null) {
foreach ($filterByStatus as $filterByStatusItem) {
$url .= 'status=' . urlencode($filterByStatusItem) . '&';
}
}
if ($filterByStartDate !== null) {
$url .= 'startDate=' . $filterByStartDate . '&';
}
if ($filterByEndDate !== null) {
$url .= 'endDate=' . $filterByEndDate . '&';
}
if ($take !== null) {
$url .= 'take=' . $take . '&';
}
if ($skip !== null) {
$url .= 'skip=' . $skip . '&';
}

// Clean URL.
$url = rtrim($url, '&');
Expand Down

0 comments on commit 74257cf

Please sign in to comment.