Skip to content

Commit

Permalink
BTCPay 2.0 compatibiltiy fixes in a backward compatible way.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet committed Sep 12, 2024
1 parent c115b04 commit 143401e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Client/StorePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions src/Result/InvoicePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand Down
35 changes: 32 additions & 3 deletions src/Result/StorePaymentMethodCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,53 @@ 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
// results until it is there.
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);
}
}
Expand Down

0 comments on commit 143401e

Please sign in to comment.