diff --git a/examples/basic_usage.php b/examples/stores.php similarity index 56% rename from examples/basic_usage.php rename to examples/stores.php index 23d3c2c..37743b1 100644 --- a/examples/basic_usage.php +++ b/examples/stores.php @@ -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)); @@ -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(); } diff --git a/src/Client/Store.php b/src/Client/Store.php index ab2f32d..ba82a6f 100644 --- a/src/Client/Store.php +++ b/src/Client/Store.php @@ -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, @@ -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(); @@ -45,6 +53,7 @@ public function createStore( [ "name" => $name, "website" => $website, + "supportUrl" => $supportUrl, "defaultCurrency" => $defaultCurrency, "invoiceExpiration" => $invoiceExpiration, "displayExpirationTimer" => $displayExpirationTimer, @@ -52,6 +61,7 @@ public function createStore( "speedPolicy" => $speedPolicy, "lightningDescriptionTemplate" => $lightningDescriptionTemplate, "paymentTolerance" => $paymentTolerance, + "archived" => $archived, "anyoneCanCreateInvoice" => $anyoneCanCreateInvoice, "requiresRefundEmail" => $requiresRefundEmail, "checkoutType" => $checkoutType, @@ -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 ); @@ -97,6 +113,115 @@ public function getStore(string $storeId): ResultStore } } + /** + * 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[] */