Skip to content

Commit

Permalink
Merge pull request #50 from barrydeen/dev-Webhook
Browse files Browse the repository at this point in the history
Webhook Getters and Missing Methods
  • Loading branch information
ndeet authored Feb 13, 2022
2 parents 79852a7 + f988d8c commit 8585486
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 29 deletions.
6 changes: 3 additions & 3 deletions examples/store_onchain_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ public function getStoreOnChainWalletTransaction()
}
}

public function getStoreOnChainWalletUTXOS()
public function getStoreOnChainWalletUTXOs()
{
$cryptoCode = 'BTC';

try {
$client = new StoreOnChainWallet($this->host, $this->apiKey);
var_dump($client->getStoreOnChainWalletUTXOS(
var_dump($client->getStoreOnChainWalletUTXOs(
$this->storeId,
$cryptoCode
));
Expand All @@ -170,4 +170,4 @@ public function getStoreOnChainWalletUTXOS()
//$store->getStoreOnChainWalletOverview();
$store->getStoreOnChainWalletTransactions();
//$store->getStoreOnChainWalletTransaction();
//$store->getStoreOnChainWalletUTXOS();
//$store->getStoreOnChainWalletUTXOs();
2 changes: 1 addition & 1 deletion src/Client/StoreOnChainWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function getStoreOnChainWalletTransaction(
}
}

public function getStoreOnChainWalletUTXOS(
public function getStoreOnChainWalletUTXOs(
string $storeId,
string $cryptoCode
): \BTCPayServer\Result\StoreOnChainWalletUTXOList {
Expand Down
118 changes: 109 additions & 9 deletions src/Client/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ class Webhook extends AbstractClient
{
/**
* @param string $storeId
* @return \BTCPayServer\Result\Webhook[]
* @return \BTCPayServer\Result\WebhookList
*/
public function getWebhooks(string $storeId): array
public function getStoreWebhooks(string $storeId): \BTCPayServer\Result\WebhookList
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks';
$headers = $this->getRequestHeaders();
$method = 'GET';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
$r = [];
$data = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
foreach ($data as $item) {
$item = new \BTCPayServer\Result\Webhook($item);
$r[] = $item;
}
return $r;
return new \BTCPayServer\Result\WebhookList(
json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)
);
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
Expand All @@ -45,6 +41,83 @@ public function getWebhook(string $storeId, string $webhookId): \BTCPayServer\Re
}
}

public function getLatestDeliveries(string $storeId, string $webhookId, string $count): \BTCPayServer\Result\WebhookDeliveryList
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks/' . urlencode($webhookId) . '/deliveries?count=' . $count;
$headers = $this->getRequestHeaders();
$method = 'GET';
$response = $this->getHttpClient()->request($method, $url, $headers);

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

public function getDelivery(string $storeId, string $webhookId, string $deliveryId): \BTCPayServer\Result\WebhookDelivery
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks/' . urlencode($webhookId) . '/deliveries/' . urlencode($deliveryId);
$headers = $this->getRequestHeaders();
$method = 'GET';
$response = $this->getHttpClient()->request($method, $url, $headers);

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

/**
* Get the delivery's request.
*
* The delivery's JSON request sent to the endpoint.
*
* @param string $storeId
* @param string $webhookId
* @param string $deliveryId
* @return string JSON request
*/
public function getDeliveryRequest(string $storeId, string $webhookId, string $deliveryId): string
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks/' . urlencode($webhookId) . '/deliveries/' . urlencode($deliveryId);
$headers = $this->getRequestHeaders();
$method = 'GET';
$response = $this->getHttpClient()->request($method, $url, $headers);

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

/**
* Redeliver the delivery.
*
* @param string $storeId
* @param string $webhookId
* @param string $deliveryId
* @return string The new delivery id being broadcasted. (Broadcast happen asynchronously with this call)
*/
public function redeliverDelivery(string $storeId, string $webhookId, string $deliveryId): string
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks/' .
urlencode($webhookId) . '/deliveries/' . urlencode($deliveryId) . '/redeliver';
$headers = $this->getRequestHeaders();
$method = 'POST';
$response = $this->getHttpClient()->request($method, $url, $headers);

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

public function createWebhook(string $storeId, string $url, ?array $specificEvents, ?string $secret): \BTCPayServer\Result\Webhook
{
$data = [
Expand Down Expand Up @@ -115,4 +188,31 @@ public function deleteWebhook(string $storeId, string $webhookId): void
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

/**
* @deprecated 2.0.0 Please use `getStoreWebhooks()` instead.
* @see getStoreWebhooks()
*
* @param string $storeId
* @return \BTCPayServer\Result\Webhook[]
*/
public function getWebhooks(string $storeId): array
{
$url = $this->getApiUrl() . 'stores/' . urlencode($storeId) . '/webhooks';
$headers = $this->getRequestHeaders();
$method = 'GET';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
$r = [];
$data = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
foreach ($data as $item) {
$item = new \BTCPayServer\Result\Webhook($item);
$r[] = $item;
}
return $r;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}
}
12 changes: 12 additions & 0 deletions src/Result/AddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@

class AddressList extends \BTCPayServer\Result\AbstractListResult
{
public function all(): array
{
$r = [];
foreach ($this->getData()['addresses'] as $addressData) {
$r[] = new \BTCPayServer\Result\Address($addressData);
}
return $r;
}

/**
* @deprecated 2.0.0 Please use `all()` instead.
* @see all()
*
* @return \BTCPayServer\Result\Address[]
*/
public function getAddresses(): array
Expand Down
25 changes: 20 additions & 5 deletions src/Result/InvoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class InvoiceList extends AbstractListResult
/**
* @return \BTCPayServer\Result\Invoice[]
*/
public function getInvoices(): array
public function all(): array
{
$r = [];
foreach ($this->getData() as $invoiceData) {
$r[] = new \BTCPayServer\Result\Invoice($invoiceData);
$invoices = [];
foreach ($this->getData() as $invoice) {
$invoices[] = new \BTCPayServer\Result\Invoice($invoice);
}
return $r;
return $invoices;
}

/**
Expand All @@ -33,4 +33,19 @@ function (\BTCPayServer\Result\Invoice $invoice) use ($status) {
// Renumber results
return array_values($r);
}

/**
* @deprecated 2.0.0 Please use `all()` instead.
* @see all()
*
* @return \BTCPayServer\Result\Invoice[]
*/
public function getInvoices(): array
{
$r = [];
foreach ($this->getData() as $invoiceData) {
$r[] = new \BTCPayServer\Result\Invoice($invoiceData);
}
return $r;
}
}
2 changes: 1 addition & 1 deletion src/Result/LanguageCodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LanguageCodeList extends AbstractListResult
/**
* @return \BTCPayServer\Result\LanguageCode[]
*/
public function getLanguageCodes(): array
public function all(): array
{
$languageCodes = [];
foreach ($this->getData() as $languageCode) {
Expand Down
2 changes: 1 addition & 1 deletion src/Result/LightningChannelList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LightningChannelList extends AbstractListResult
/**
* @return \BTCPayServer\Result\LightningChannel[]
*/
public function getChannels(): array
public function all(): array
{
$channels = [];
foreach ($this->getData() as $channel) {
Expand Down
2 changes: 1 addition & 1 deletion src/Result/NotificationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NotificationList extends AbstractListResult
/**
* @return \BTCPayServer\Result\Notification[]
*/
public function getNotifications(): array
public function all(): array
{
$notifications = [];
foreach ($this->getData() as $notification) {
Expand Down
2 changes: 1 addition & 1 deletion src/Result/PullPaymentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PullPaymentList extends AbstractListResult
/**
* @return \BTCPayServer\Result\PullPayment[]
*/
public function getPullPayments(): array
public function all(): array
{
$pullPayments = [];
foreach ($this->getData() as $pullPaymentData) {
Expand Down
2 changes: 1 addition & 1 deletion src/Result/PullPaymentPayoutList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PullPaymentPayoutList extends AbstractListResult
/**
* @return \BTCPayServer\Result\PullPaymentPayout[]
*/
public function getPullPaymentPayouts(): array
public function all(): array
{
$pullPaymentPayouts = [];
foreach ($this->getData() as $pullPaymentPayoutData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StoreOnChainWalletTransactionDestinationList extends AbstractListResult
/**
* @return \BTCPayServer\Result\StoreOnChainWalletTransactionDestination[]
*/
public function getDestinations(): array
public function all(): array
{
$destinations = [];
foreach ($this->getData() as $destination) {
Expand Down
2 changes: 1 addition & 1 deletion src/Result/StoreOnChainWalletTransactionList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StoreOnChainWalletTransactionList extends AbstractListResult
/**
* @return \BTCPayServer\Result\StoreOnChainWalletTransaction[]
*/
public function getStoreOnChainWalletTransactions(): array
public function all(): array
{
$storeWalletTransactions = [];
foreach ($this->getData() as $storeWalletTransaction) {
Expand Down
8 changes: 4 additions & 4 deletions src/Result/StoreOnChainWalletUTXOList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class StoreOnChainWalletUTXOList extends AbstractListResult
/**
* @return \BTCPayServer\Result\StoreOnChainWalletUTXO[]
*/
public function getStoreOnChainWalletUTXOS(): array
public function all(): array
{
$storeWalletUTXOS = [];
$storeWalletUTXOs = [];
foreach ($this->getData() as $storeWalletUTXO) {
$storeWalletUTXOS[] = new \BTCPayServer\Result\StoreOnChainWalletUTXO($storeWalletUTXO);
$storeWalletUTXOs[] = new \BTCPayServer\Result\StoreOnChainWalletUTXO($storeWalletUTXO);
}
return $storeWalletUTXOS;
return $storeWalletUTXOs;
}
}
34 changes: 34 additions & 0 deletions src/Result/StorePaymentMethodCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ class StorePaymentMethodCollection extends AbstractListResult
/**
* @return \BTCPayServer\Result\AbstractStorePaymentMethodResult[]
*/
public function all(): array
{
$r = [];
foreach ($this->getData() as $paymentMethod => $paymentMethodData) {
// Consistency: Flatten the array to be consistent with the specific
// payment method endpoints.
$paymentMethodData += $paymentMethodData['data'];
unset($paymentMethodData['data']);

if (strpos($paymentMethod, 'LightningNetwork') !== false) {
// Consistency: Add back the cryptoCode missing on this endpoint
// results until it is there.
if (!isset($paymentMethodData['cryptoCode'])) {
$paymentMethodData['cryptoCode'] = str_replace('-LightningNetwork', '', $paymentMethod);
}
$r[] = new \BTCPayServer\Result\StorePaymentMethodLightningNetwork($paymentMethodData, $paymentMethod);
} else {
// Consistency: Add back the cryptoCode missing on this endpoint
// results until it is there.
if (!isset($paymentMethodData['cryptoCode'])) {
$paymentMethodData['cryptoCode'] = $paymentMethod;
}
$r[] = new \BTCPayServer\Result\StorePaymentMethodOnChain($paymentMethodData, $paymentMethod);
}
}
return $r;
}

/**
* @deprecated 2.0.0 Please use `all()` instead.
* @see all()
*
* @return \BTCPayServer\Result\AbstractStorePaymentMethodResult[]
*/
public function getPaymentMethods(): array
{
$r = [];
Expand Down
29 changes: 29 additions & 0 deletions src/Result/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,33 @@

class Webhook extends AbstractResult
{
public function getId(): string
{
$data = $this->getData();
return $data['id'];
}

public function isEnabled(): bool
{
$data = $this->getData();
return $data['enabled'];
}

public function hasAutomaticRedelivery(): bool
{
$data = $this->getData();
return $data['automaticRedelivery'];
}

public function getUrl(): string
{
$data = $this->getData();
return $data['url'];
}

public function getAuthorizedEvents(): WebhookAuthorizedEvents
{
$data = $this->getData();
return new WebhookAuthorizedEvents($data['authorizedEvents']);
}
}
Loading

0 comments on commit 8585486

Please sign in to comment.