Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refund invoice endpoint. Add getters to ApiKey result. #123

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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'];
}
}
Loading