Skip to content

Commit

Permalink
create api key; create store (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet authored Feb 10, 2023
1 parent 5a500be commit d4e2036
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/api_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 8 additions & 0 deletions examples/basic_usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
26 changes: 26 additions & 0 deletions src/Client/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
77 changes: 76 additions & 1 deletion src/Client/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d4e2036

Please sign in to comment.