From aa28ba7452ff91f7d4c8c55b33da36b3e2bb4e55 Mon Sep 17 00:00:00 2001 From: ndeet Date: Tue, 23 Apr 2024 23:35:32 +0200 Subject: [PATCH 1/2] Add refund invoice endpoint. Add getters to ApiKey result. --- src/Client/Invoice.php | 60 +++++++++++++++++++++++++++++++++++++++--- src/Result/ApiKey.php | 14 ++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/Client/Invoice.php b/src/Client/Invoice.php index 2b01f16..bfb0439 100644 --- a/src/Client/Invoice.php +++ b/src/Client/Invoice.php @@ -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 @@ -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); @@ -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'; @@ -206,4 +212,50 @@ 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); + } + } } diff --git a/src/Result/ApiKey.php b/src/Result/ApiKey.php index 513e3fc..223b81a 100644 --- a/src/Result/ApiKey.php +++ b/src/Result/ApiKey.php @@ -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']; + } } From 951bc5b9c9c19f3d537321b93bf91fb3f6cda219 Mon Sep 17 00:00:00 2001 From: ndeet Date: Thu, 25 Apr 2024 11:18:15 +0200 Subject: [PATCH 2/2] Fix formatting with cs-fixer. --- src/Client/Invoice.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Client/Invoice.php b/src/Client/Invoice.php index bfb0439..d96293f 100644 --- a/src/Client/Invoice.php +++ b/src/Client/Invoice.php @@ -229,8 +229,7 @@ public function refundInvoice( ?float $subtractPercentage = 0.0, ?PreciseNumber $customAmount = null, ?string $customCurrency = null - ): ResultPullPayment - { + ): ResultPullPayment { $url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/invoices/' . urlencode($invoiceId) . '/refund'; $headers = $this->getRequestHeaders(); $method = 'POST';