From 143401e19ead6d347543aa66c0892cf49be0664e Mon Sep 17 00:00:00 2001 From: ndeet Date: Thu, 12 Sep 2024 16:26:04 +0200 Subject: [PATCH] BTCPay 2.0 compatibiltiy fixes in a backward compatible way. --- src/Client/StorePaymentMethod.php | 5 ++- src/Result/InvoicePaymentMethod.php | 13 ++++++-- src/Result/StorePaymentMethodCollection.php | 35 +++++++++++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/Client/StorePaymentMethod.php b/src/Client/StorePaymentMethod.php index e2ecac7..ebef8f4 100644 --- a/src/Client/StorePaymentMethod.php +++ b/src/Client/StorePaymentMethod.php @@ -15,9 +15,12 @@ */ class StorePaymentMethod extends AbstractClient { - public function getPaymentMethods(string $storeId): array + public function getPaymentMethods(string $storeId, bool $includeConfig = false): array { $url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/payment-methods'; + if ($includeConfig) { + $url .= '?includeConfig=true'; + } $headers = $this->getRequestHeaders(); $method = 'GET'; $response = $this->getHttpClient()->request($method, $url, $headers); diff --git a/src/Result/InvoicePaymentMethod.php b/src/Result/InvoicePaymentMethod.php index 640430e..fd478c0 100644 --- a/src/Result/InvoicePaymentMethod.php +++ b/src/Result/InvoicePaymentMethod.php @@ -59,24 +59,31 @@ public function getAmount(): string public function getNetworkFee(): string { $data = $this->getData(); - return $data['networkFee']; + // BTCPay 2.0.0 compatibility: networkFee was renamed to paymentMethodFee. + return $data['networkFee'] ?? $data['paymentMethodFee']; } public function getPaymentMethod(): string { $data = $this->getData(); - return $data['paymentMethod']; + // BTCPay 2.0.0 compatibility: paymentMethod was renamed to paymentMethodId. + return $data['paymentMethod'] ?? $data['paymentMethodId']; } public function getCryptoCode(): string { $data = $this->getData(); + // BTCPay 2.0.0 compatibility: cryptoCode was renamed to currency. + if (isset($data['currency'])) { + return $data['currency']; + } + // For future compatibility check if cryptoCode exists. if (isset($data['cryptoCode'])) { return $data['cryptoCode']; } else { // Extract cryptoCode from paymentMethod string. - $parts = explode('-', $data['paymentMethod']); + $parts = explode('-', $this->getPaymentMethod()); return $parts[0]; } } diff --git a/src/Result/StorePaymentMethodCollection.php b/src/Result/StorePaymentMethodCollection.php index 1da262a..190513d 100644 --- a/src/Result/StorePaymentMethodCollection.php +++ b/src/Result/StorePaymentMethodCollection.php @@ -13,17 +13,40 @@ public function all(): array { $r = []; foreach ($this->getData() as $paymentMethod => $paymentMethodData) { + // BTCPay 2.0 compatibility: List is not a keyed array anymore so fix it here. + if (is_numeric($paymentMethod)) { + $paymentMethod = $paymentMethodData['paymentMethodId']; + // Extract the cryptoCode from the paymentMethodId. e.g. "BTC-CHAIN" -> "BTC" + $parts = explode('-', $paymentMethod); + $extractedCryptoCode = $parts[0]; + } + // Consistency: Flatten the array to be consistent with the specific // payment method endpoints. - $paymentMethodData += $paymentMethodData['data']; - unset($paymentMethodData['data']); + if (isset($paymentMethodData['data'])) { + $paymentMethodData += $paymentMethodData['data']; + unset($paymentMethodData['data']); + } - if (strpos($paymentMethod, 'LightningNetwork') !== false) { + // BTCPay 2.0 compatibility: Handle config data if exists. + if (isset($paymentMethodData['config'])) { + $paymentMethodData += $paymentMethodData['config']; + unset($paymentMethodData['config']); + } + + // BTCPay 2.0 compatibility: Check for renamed LN payment method id. + if (preg_match('/(LightningNetwork|-LN$)/', $paymentMethod)) { // Consistency: Add back the cryptoCode missing on this endpoint // results until it is there. if (!isset($paymentMethodData['cryptoCode'])) { $paymentMethodData['cryptoCode'] = str_replace('-LightningNetwork', '', $paymentMethod); } + + // BTCPay 2.0 compatibility: put the currency code in the cryptoCode field. + if (isset($extractedCryptoCode)) { + $paymentMethodData['cryptoCode'] = $extractedCryptoCode; + } + $r[] = new StorePaymentMethodLightningNetwork($paymentMethodData, $paymentMethod); } else { // Consistency: Add back the cryptoCode missing on this endpoint @@ -31,6 +54,12 @@ public function all(): array if (!isset($paymentMethodData['cryptoCode'])) { $paymentMethodData['cryptoCode'] = $paymentMethod; } + + // BTCPay 2.0 compatibility: put the currency code in the cryptoCode field. + if (isset($extractedCryptoCode)) { + $paymentMethodData['cryptoCode'] = $extractedCryptoCode; + } + $r[] = new StorePaymentMethodOnChain($paymentMethodData, $paymentMethod); } }