Skip to content

Commit 385b7f6

Browse files
authored
Adding missing stores endpoints update/delete (#122)
* Adding missing functions of updating and deleting store; renaming examples file.
1 parent b88cd1c commit 385b7f6

File tree

2 files changed

+141
-5
lines changed

2 files changed

+141
-5
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
$apiKey = '';
99
$host = ''; // e.g. https://your.btcpay-server.tld
1010
$storeId = '';
11-
$invoiceId = '';
11+
$updateStoreId = '';
1212

1313
// Get information about store on BTCPay Server.
14+
1415
try {
1516
$client = new Store($host, $apiKey);
1617
var_dump($client->getStore($storeId));
@@ -21,7 +22,17 @@
2122
// Create a new store.
2223
try {
2324
$client = new Store($host, $apiKey);
24-
var_dump($client->createStore('my new store'));
25+
$newStore = $client->createStore('New store', null, 'EUR');
26+
var_dump($newStore);
27+
} catch (\Throwable $e) {
28+
echo "Error: " . $e->getMessage();
29+
}
30+
31+
// Update a store.
32+
// You need to pass all variables to make sure it does not get reset to defaults if you want to preserve them.
33+
try {
34+
$client = new Store($host, $apiKey);
35+
var_dump($client->updateStore($updateStoreId, 'Store name CHANGED'));
2536
} catch (\Throwable $e) {
2637
echo "Error: " . $e->getMessage();
2738
}

src/Client/Store.php

Lines changed: 128 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function createStore(
2020
int $paymentTolerance = 0,
2121
bool $anyoneCanCreateInvoice = false,
2222
bool $requiresRefundEmail = false,
23-
?string $checkoutType = 'V1',
23+
?string $checkoutType = 'V2',
2424
?array $receipt = null,
2525
bool $lightningAmountInSatoshi = false,
2626
bool $lightningPrivateRouteHints = false,
@@ -35,7 +35,15 @@ public function createStore(
3535
string $networkFeeMode = 'MultiplePaymentsOnly',
3636
bool $payJoinEnabled = false,
3737
bool $lazyPaymentMethods = false,
38-
string $defaultPaymentMethod = 'BTC'
38+
string $defaultPaymentMethod = 'BTC',
39+
?string $supportUrl = null,
40+
bool $archived = false,
41+
bool $autodetectLanguage = false,
42+
bool $showPayInWalletButton = true,
43+
bool $showStoreHeader = true,
44+
bool $celebratePayment = true,
45+
bool $playSoundOnPayment = false,
46+
?array $paymentMethodCriteria = null
3947
): ResultStore {
4048
$url = $this->getApiUrl() . 'stores';
4149
$headers = $this->getRequestHeaders();
@@ -45,13 +53,15 @@ public function createStore(
4553
[
4654
"name" => $name,
4755
"website" => $website,
56+
"supportUrl" => $supportUrl,
4857
"defaultCurrency" => $defaultCurrency,
4958
"invoiceExpiration" => $invoiceExpiration,
5059
"displayExpirationTimer" => $displayExpirationTimer,
5160
"monitoringExpiration" => $monitoringExpiration,
5261
"speedPolicy" => $speedPolicy,
5362
"lightningDescriptionTemplate" => $lightningDescriptionTemplate,
5463
"paymentTolerance" => $paymentTolerance,
64+
"archived" => $archived,
5565
"anyoneCanCreateInvoice" => $anyoneCanCreateInvoice,
5666
"requiresRefundEmail" => $requiresRefundEmail,
5767
"checkoutType" => $checkoutType,
@@ -68,8 +78,14 @@ public function createStore(
6878
"htmlTitle" => $htmlTitle,
6979
"networkFeeMode" => $networkFeeMode,
7080
"payJoinEnabled" => $payJoinEnabled,
81+
"autodetectLanguage" => $autodetectLanguage,
82+
"showPayInWalletButton" => $showPayInWalletButton,
83+
"showStoreHeader" => $showStoreHeader,
84+
"celebratePayment" => $celebratePayment,
85+
"playSoundOnPayment" => $playSoundOnPayment,
7186
"lazyPaymentMethods" => $lazyPaymentMethods,
72-
"defaultPaymentMethod" => $defaultPaymentMethod
87+
"defaultPaymentMethod" => $defaultPaymentMethod,
88+
"paymentMethodCriteria" => $paymentMethodCriteria
7389
],
7490
JSON_THROW_ON_ERROR
7591
);
@@ -97,6 +113,115 @@ public function getStore(string $storeId): ResultStore
97113
}
98114
}
99115

116+
/**
117+
* Update store settings. Make sure to pass all the settings, even if you don't want to change them.
118+
*/
119+
public function updateStore(
120+
string $storeId,
121+
string $name,
122+
?string $website = null,
123+
string $defaultCurrency = 'USD',
124+
int $invoiceExpiration = 900,
125+
int $displayExpirationTimer = 300,
126+
int $monitoringExpiration = 3600,
127+
string $speedPolicy = 'MediumSpeed',
128+
?string $lightningDescriptionTemplate = null,
129+
int $paymentTolerance = 0,
130+
bool $anyoneCanCreateInvoice = false,
131+
bool $requiresRefundEmail = false,
132+
?string $checkoutType = 'V2',
133+
?array $receipt = null,
134+
bool $lightningAmountInSatoshi = false,
135+
bool $lightningPrivateRouteHints = false,
136+
bool $onChainWithLnInvoiceFallback = false,
137+
bool $redirectAutomatically = false,
138+
bool $showRecommendedFee = true,
139+
int $recommendedFeeBlockTarget = 1,
140+
string $defaultLang = 'en',
141+
?string $customLogo = null,
142+
?string $customCSS = null,
143+
?string $htmlTitle = null,
144+
string $networkFeeMode = 'MultiplePaymentsOnly',
145+
bool $payJoinEnabled = false,
146+
bool $lazyPaymentMethods = false,
147+
string $defaultPaymentMethod = 'BTC',
148+
?string $supportUrl = null,
149+
bool $archived = false,
150+
bool $autodetectLanguage = false,
151+
bool $showPayInWalletButton = true,
152+
bool $showStoreHeader = true,
153+
bool $celebratePayment = true,
154+
bool $playSoundOnPayment = false,
155+
?array $paymentMethodCriteria = null
156+
): ResultStore {
157+
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId);
158+
$headers = $this->getRequestHeaders();
159+
$method = 'PUT';
160+
161+
$body = json_encode(
162+
[
163+
"name" => $name,
164+
"website" => $website,
165+
"supportUrl" => $supportUrl,
166+
"defaultCurrency" => $defaultCurrency,
167+
"invoiceExpiration" => $invoiceExpiration,
168+
"displayExpirationTimer" => $displayExpirationTimer,
169+
"monitoringExpiration" => $monitoringExpiration,
170+
"speedPolicy" => $speedPolicy,
171+
"lightningDescriptionTemplate" => $lightningDescriptionTemplate,
172+
"paymentTolerance" => $paymentTolerance,
173+
"archived" => $archived,
174+
"anyoneCanCreateInvoice" => $anyoneCanCreateInvoice,
175+
"requiresRefundEmail" => $requiresRefundEmail,
176+
"checkoutType" => $checkoutType,
177+
"receipt" => $receipt,
178+
"lightningAmountInSatoshi" => $lightningAmountInSatoshi,
179+
"lightningPrivateRouteHints" => $lightningPrivateRouteHints,
180+
"onChainWithLnInvoiceFallback" => $onChainWithLnInvoiceFallback,
181+
"redirectAutomatically" => $redirectAutomatically,
182+
"showRecommendedFee" => $showRecommendedFee,
183+
"recommendedFeeBlockTarget" => $recommendedFeeBlockTarget,
184+
"defaultLang" => $defaultLang,
185+
"customLogo" => $customLogo,
186+
"customCSS" => $customCSS,
187+
"htmlTitle" => $htmlTitle,
188+
"networkFeeMode" => $networkFeeMode,
189+
"payJoinEnabled" => $payJoinEnabled,
190+
"autodetectLanguage" => $autodetectLanguage,
191+
"showPayInWalletButton" => $showPayInWalletButton,
192+
"showStoreHeader" => $showStoreHeader,
193+
"celebratePayment" => $celebratePayment,
194+
"playSoundOnPayment" => $playSoundOnPayment,
195+
"lazyPaymentMethods" => $lazyPaymentMethods,
196+
"defaultPaymentMethod" => $defaultPaymentMethod,
197+
"paymentMethodCriteria" => $paymentMethodCriteria
198+
],
199+
JSON_THROW_ON_ERROR
200+
);
201+
202+
$response = $this->getHttpClient()->request($method, $url, $headers, $body);
203+
204+
if ($response->getStatus() === 200) {
205+
return new ResultStore(json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR));
206+
} else {
207+
throw $this->getExceptionByStatusCode($method, $url, $response);
208+
}
209+
}
210+
211+
public function deleteStore(string $storeId): bool
212+
{
213+
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId);
214+
$headers = $this->getRequestHeaders();
215+
$method = 'DELETE';
216+
$response = $this->getHttpClient()->request($method, $url, $headers);
217+
218+
if ($response->getStatus() === 200) {
219+
return true;
220+
} else {
221+
throw $this->getExceptionByStatusCode($method, $url, $response);
222+
}
223+
}
224+
100225
/**
101226
* @return \BTCPayServer\Result\Store[]
102227
*/

0 commit comments

Comments
 (0)