Skip to content

Commit

Permalink
Adding missing functions of updating and deleting store; renaming exa…
Browse files Browse the repository at this point in the history
…mples file.
  • Loading branch information
ndeet committed Apr 2, 2024
1 parent b88cd1c commit 74423a5
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 6 deletions.
15 changes: 13 additions & 2 deletions examples/basic_usage.php → examples/stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
$apiKey = '';
$host = ''; // e.g. https://your.btcpay-server.tld
$storeId = '';
$invoiceId = '';
$updateStoreId = '';

// Get information about store on BTCPay Server.

try {
$client = new Store($host, $apiKey);
var_dump($client->getStore($storeId));
Expand All @@ -21,7 +22,17 @@
// Create a new store.
try {
$client = new Store($host, $apiKey);
var_dump($client->createStore('my new store'));
$newStore = $client->createStore('New store', null, 'EUR');
var_dump($newStore);
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}

// Update a store.
// You need to pass all variables to make sure it does not get reset to defaults if you want to preserve them.
try {
$client = new Store($host, $apiKey);
var_dump($client->updateStore($updateStoreId, 'Store name CHANGED'));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
133 changes: 129 additions & 4 deletions src/Client/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function createStore(
int $paymentTolerance = 0,
bool $anyoneCanCreateInvoice = false,
bool $requiresRefundEmail = false,
?string $checkoutType = 'V1',
?string $checkoutType = 'V2',
?array $receipt = null,
bool $lightningAmountInSatoshi = false,
bool $lightningPrivateRouteHints = false,
Expand All @@ -35,7 +35,15 @@ public function createStore(
string $networkFeeMode = 'MultiplePaymentsOnly',
bool $payJoinEnabled = false,
bool $lazyPaymentMethods = false,
string $defaultPaymentMethod = 'BTC'
string $defaultPaymentMethod = 'BTC',
?string $supportUrl = null,
bool $archived = false,
bool $autodetectLanguage = false,
bool $showPayInWalletButton = true,
bool $showStoreHeader = true,
bool $celebratePayment = true,
bool $playSoundOnPayment = false,
?array $paymentMethodCriteria = null
): ResultStore {
$url = $this->getApiUrl() . 'stores';
$headers = $this->getRequestHeaders();
Expand All @@ -45,13 +53,15 @@ public function createStore(
[
"name" => $name,
"website" => $website,
"supportUrl" => $supportUrl,
"defaultCurrency" => $defaultCurrency,
"invoiceExpiration" => $invoiceExpiration,
"displayExpirationTimer" => $displayExpirationTimer,
"monitoringExpiration" => $monitoringExpiration,
"speedPolicy" => $speedPolicy,
"lightningDescriptionTemplate" => $lightningDescriptionTemplate,
"paymentTolerance" => $paymentTolerance,
"archived" => $archived,
"anyoneCanCreateInvoice" => $anyoneCanCreateInvoice,
"requiresRefundEmail" => $requiresRefundEmail,
"checkoutType" => $checkoutType,
Expand All @@ -68,8 +78,14 @@ public function createStore(
"htmlTitle" => $htmlTitle,
"networkFeeMode" => $networkFeeMode,
"payJoinEnabled" => $payJoinEnabled,
"autodetectLanguage" => $autodetectLanguage,
"showPayInWalletButton" => $showPayInWalletButton,
"showStoreHeader" => $showStoreHeader,
"celebratePayment" => $celebratePayment,
"playSoundOnPayment" => $playSoundOnPayment,
"lazyPaymentMethods" => $lazyPaymentMethods,
"defaultPaymentMethod" => $defaultPaymentMethod
"defaultPaymentMethod" => $defaultPaymentMethod,
"paymentMethodCriteria" => $paymentMethodCriteria
],
JSON_THROW_ON_ERROR
);
Expand Down Expand Up @@ -98,8 +114,117 @@ public function getStore(string $storeId): ResultStore
}

/**
* @return \BTCPayServer\Result\Store[]
* Update store settings. Make sure to pass all the settings, even if you don't want to change them.
*/
public function updateStore(
string $storeId,
string $name,
?string $website = null,
string $defaultCurrency = 'USD',
int $invoiceExpiration = 900,
int $displayExpirationTimer = 300,
int $monitoringExpiration = 3600,
string $speedPolicy = 'MediumSpeed',
?string $lightningDescriptionTemplate = null,
int $paymentTolerance = 0,
bool $anyoneCanCreateInvoice = false,
bool $requiresRefundEmail = false,
?string $checkoutType = 'V2',
?array $receipt = null,
bool $lightningAmountInSatoshi = false,
bool $lightningPrivateRouteHints = false,
bool $onChainWithLnInvoiceFallback = false,
bool $redirectAutomatically = false,
bool $showRecommendedFee = true,
int $recommendedFeeBlockTarget = 1,
string $defaultLang = 'en',
?string $customLogo = null,
?string $customCSS = null,
?string $htmlTitle = null,
string $networkFeeMode = 'MultiplePaymentsOnly',
bool $payJoinEnabled = false,
bool $lazyPaymentMethods = false,
string $defaultPaymentMethod = 'BTC',
?string $supportUrl = null,
bool $archived = false,
bool $autodetectLanguage = false,
bool $showPayInWalletButton = true,
bool $showStoreHeader = true,
bool $celebratePayment = true,
bool $playSoundOnPayment = false,
?array $paymentMethodCriteria = null
): ResultStore {
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId);
$headers = $this->getRequestHeaders();
$method = 'PUT';

$body = json_encode(
[
"name" => $name,
"website" => $website,
"supportUrl" => $supportUrl,
"defaultCurrency" => $defaultCurrency,
"invoiceExpiration" => $invoiceExpiration,
"displayExpirationTimer" => $displayExpirationTimer,
"monitoringExpiration" => $monitoringExpiration,
"speedPolicy" => $speedPolicy,
"lightningDescriptionTemplate" => $lightningDescriptionTemplate,
"paymentTolerance" => $paymentTolerance,
"archived" => $archived,
"anyoneCanCreateInvoice" => $anyoneCanCreateInvoice,
"requiresRefundEmail" => $requiresRefundEmail,
"checkoutType" => $checkoutType,
"receipt" => $receipt,
"lightningAmountInSatoshi" => $lightningAmountInSatoshi,
"lightningPrivateRouteHints" => $lightningPrivateRouteHints,
"onChainWithLnInvoiceFallback" => $onChainWithLnInvoiceFallback,
"redirectAutomatically" => $redirectAutomatically,
"showRecommendedFee" => $showRecommendedFee,
"recommendedFeeBlockTarget" => $recommendedFeeBlockTarget,
"defaultLang" => $defaultLang,
"customLogo" => $customLogo,
"customCSS" => $customCSS,
"htmlTitle" => $htmlTitle,
"networkFeeMode" => $networkFeeMode,
"payJoinEnabled" => $payJoinEnabled,
"autodetectLanguage" => $autodetectLanguage,
"showPayInWalletButton" => $showPayInWalletButton,
"showStoreHeader" => $showStoreHeader,
"celebratePayment" => $celebratePayment,
"playSoundOnPayment" => $playSoundOnPayment,
"lazyPaymentMethods" => $lazyPaymentMethods,
"defaultPaymentMethod" => $defaultPaymentMethod,
"paymentMethodCriteria" => $paymentMethodCriteria
],
JSON_THROW_ON_ERROR
);

$response = $this->getHttpClient()->request($method, $url, $headers, $body);

if ($response->getStatus() === 200) {
return new ResultStore(json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR));
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

public function deleteStore(string $storeId): bool
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId);
$headers = $this->getRequestHeaders();
$method = 'DELETE';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return true;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

/**
* @return \BTCPayServer\Result\Store[]
*/
public function getStores(): array
{
$url = $this->getApiUrl() . 'stores';
Expand Down

0 comments on commit 74423a5

Please sign in to comment.