diff --git a/examples/api_key.php b/examples/api_key.php index 60abbf7..867c66b 100644 --- a/examples/api_key.php +++ b/examples/api_key.php @@ -17,3 +17,11 @@ } catch (\Throwable $e) { echo "Error: " . $e->getMessage(); } + +// Create a new api key. Needs server modify permission of used api. +try { + $client = new Apikey($host, $apiKey); + var_dump($client->createApiKey('api generated', ['btcpay.store.canmodifystoresettings'])); +} catch (\Throwable $e) { + echo "Error: " . $e->getMessage(); +} diff --git a/examples/basic_usage.php b/examples/basic_usage.php index 34e27f8..23d3c2c 100644 --- a/examples/basic_usage.php +++ b/examples/basic_usage.php @@ -17,3 +17,11 @@ } catch (\Throwable $e) { echo "Error: " . $e->getMessage(); } + +// Create a new store. +try { + $client = new Store($host, $apiKey); + var_dump($client->createStore('my new store')); +} catch (\Throwable $e) { + echo "Error: " . $e->getMessage(); +} diff --git a/src/Client/ApiKey.php b/src/Client/ApiKey.php index 776b17d..ca1b346 100644 --- a/src/Client/ApiKey.php +++ b/src/Client/ApiKey.php @@ -77,4 +77,30 @@ public function getCurrent(): ResultApiKey throw $this->getExceptionByStatusCode($method, $url, $response); } } + + /** + * Create a new API Key. + */ + public function createApikey(?string $label = null, ?array $permissions = null): ResultApiKey + { + $url = $this->getApiUrl() . 'api-keys'; + $headers = $this->getRequestHeaders(); + $method = 'POST'; + + $body = json_encode( + [ + 'label' => $label, + 'permissions' => $permissions + ], + JSON_THROW_ON_ERROR + ); + + $response = $this->getHttpClient()->request($method, $url, $headers, $body); + + if ($response->getStatus() === 200) { + return new ResultApiKey(json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)); + } else { + throw $this->getExceptionByStatusCode($method, $url, $response); + } + } } diff --git a/src/Client/Store.php b/src/Client/Store.php index 15e36f3..ab2f32d 100644 --- a/src/Client/Store.php +++ b/src/Client/Store.php @@ -8,7 +8,82 @@ class Store extends AbstractClient { - public function getStore($storeId): ResultStore + public function createStore( + 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 = 'V1', + ?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' + ): ResultStore { + $url = $this->getApiUrl() . 'stores'; + $headers = $this->getRequestHeaders(); + $method = 'POST'; + + $body = json_encode( + [ + "name" => $name, + "website" => $website, + "defaultCurrency" => $defaultCurrency, + "invoiceExpiration" => $invoiceExpiration, + "displayExpirationTimer" => $displayExpirationTimer, + "monitoringExpiration" => $monitoringExpiration, + "speedPolicy" => $speedPolicy, + "lightningDescriptionTemplate" => $lightningDescriptionTemplate, + "paymentTolerance" => $paymentTolerance, + "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, + "lazyPaymentMethods" => $lazyPaymentMethods, + "defaultPaymentMethod" => $defaultPaymentMethod + ], + 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 getStore(string $storeId): ResultStore { $url = $this->getApiUrl() . 'stores/' . urlencode($storeId); $headers = $this->getRequestHeaders();