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

BTCPay 2.0 compatibiltiy fixes in a backward compatible way. #127

Merged
merged 3 commits into from
Sep 12, 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
2 changes: 1 addition & 1 deletion .github/workflows/code_style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
Expand Down
106 changes: 53 additions & 53 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
name: PHP Unit Tests
env:
BTCPAY_HOST: ${{ secrets.BTCPAY_HOST }}
BTCPAY_API_KEY: ${{ secrets.BTCPAY_API_KEY }}
BTCPAY_STORE_ID: ${{ secrets.BTCPAY_STORE_ID }}
BTCPAY_NODE_URI: ${{ secrets.BTCPAY_NODE_URI }}
on: [ push, pull_request ]

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.0', '8.1']
phpunit-versions: ['latest']

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0'

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}
extensions: curl, json, mbstring, bcmath
coverage: xdebug #optional

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-progress --optimize-autoloader

- name: Test with phpunit
run: vendor/bin/phpunit --coverage-text

- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
#name: PHP Unit Tests
#env:
# BTCPAY_HOST: ${{ secrets.BTCPAY_HOST }}
# BTCPAY_API_KEY: ${{ secrets.BTCPAY_API_KEY }}
# BTCPAY_STORE_ID: ${{ secrets.BTCPAY_STORE_ID }}
# BTCPAY_NODE_URI: ${{ secrets.BTCPAY_NODE_URI }}
#on: [ push, pull_request ]
#
#jobs:
# phpunit:
# runs-on: ubuntu-latest
# strategy:
# matrix:
# php-versions: ['8.0', '8.1']
# phpunit-versions: ['latest']
#
#
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# with:
# fetch-depth: '0'
#
# - name: Setup PHP, with composer and extensions
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php-versions }}
# tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}
# extensions: curl, json, mbstring, bcmath
# coverage: xdebug #optional
#
# - name: Get composer cache directory
# id: composer-cache
# run: echo "::set-output name=dir::$(composer config cache-files-dir)"
#
# - name: Cache composer dependencies
# uses: actions/cache@v2
# with:
# path: ${{ steps.composer-cache.outputs.dir }}
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
# restore-keys: ${{ runner.os }}-composer-
#
# - name: Install Composer dependencies
# run: composer install --no-progress --optimize-autoloader
#
# - name: Test with phpunit
# run: vendor/bin/phpunit --coverage-text
#
# - name: Setup problem matchers for PHP
# run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
#
# - name: Setup problem matchers for PHPUnit
# run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
4 changes: 2 additions & 2 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
php-versions: ['8.0']
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: '0'

Expand All @@ -27,7 +27,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
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
Loading