Skip to content

Commit

Permalink
Add refund invoice endpoint. Add getters to ApiKey result. (#123)
Browse files Browse the repository at this point in the history
* Add refund invoice endpoint. 
* Add getters to ApiKey result.
  • Loading branch information
ndeet authored Apr 25, 2024
1 parent 385b7f6 commit c115b04
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
59 changes: 55 additions & 4 deletions src/Client/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BTCPayServer\Result\Invoice as ResultInvoice;
use BTCPayServer\Result\InvoiceList;
use BTCPayServer\Result\InvoicePaymentMethod;
use BTCPayServer\Result\PullPayment as ResultPullPayment;
use BTCPayServer\Util\PreciseNumber;

class Invoice extends AbstractClient
Expand Down Expand Up @@ -159,7 +160,8 @@ private function _getAllInvoicesWithFilter(
public function getPaymentMethods(string $storeId, string $invoiceId): array
{
$method = 'GET';
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices/' . urlencode($invoiceId) . '/payment-methods';
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices/'
. urlencode($invoiceId) . '/payment-methods';
$headers = $this->getRequestHeaders();
$response = $this->getHttpClient()->request($method, $url, $headers);

Expand All @@ -181,11 +183,15 @@ public function getPaymentMethods(string $storeId, string $invoiceId): array
}
}

/**
* Mark an invoice status.
*
* @see https://docs.btcpayserver.org/API/Greenfield/v1/#operation/Invoices_MarkInvoiceStatus
* @throws \JsonException
*/
public function markInvoiceStatus(string $storeId, string $invoiceId, string $markAs): ResultInvoice
{
$url = $this->getApiUrl() . 'stores/' . urlencode(
$storeId
) . '/invoices/' . urlencode($invoiceId) . '/status';
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices/' . urlencode($invoiceId) . '/status';
$headers = $this->getRequestHeaders();
$method = 'POST';

Expand All @@ -206,4 +212,49 @@ public function markInvoiceStatus(string $storeId, string $invoiceId, string $ma
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

/**
* Refund an invoice.
*
* @see https://docs.btcpayserver.org/API/Greenfield/v1/#operation/Invoices_Refund
* @throws \JsonException
*/
public function refundInvoice(
string $storeId,
string $invoiceId,
?string $refundVariant = 'CurrentRate',
?string $paymentMethod = 'BTC',
?string $name = null,
?string $description = null,
?float $subtractPercentage = 0.0,
?PreciseNumber $customAmount = null,
?string $customCurrency = null
): ResultPullPayment {
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices/' . urlencode($invoiceId) . '/refund';
$headers = $this->getRequestHeaders();
$method = 'POST';

$body = json_encode(
[
'name' => $name,
'description' => $description,
'paymentMethod' => $paymentMethod,
'refundVariant' => $refundVariant,
'subtractPercentage' => $subtractPercentage,
'customAmount' => $customAmount?->__toString(),
'customCurrency' => $customCurrency
],
JSON_THROW_ON_ERROR
);

$response = $this->getHttpClient()->request($method, $url, $headers, $body);

if ($response->getStatus() === 200) {
return new ResultPullPayment(
json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)
);
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}
}
14 changes: 14 additions & 0 deletions src/Result/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@

class ApiKey extends AbstractResult
{
public function getApiKey(): string
{
return $this->getData()['apiKey'];
}

public function getLabel(): string
{
return $this->getData()['label'];
}

public function getPermissions(): array
{
return $this->getData()['permissions'];
}
}

0 comments on commit c115b04

Please sign in to comment.