diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 02a5e3d..7dc8ed5 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -49,6 +49,7 @@ export default { { text: "Shipments", link: "/shipment/index.html" }, { text: "Transactions", link: "/transaction/index.html" }, { text: "Miscellaneous", link: "/miscellaneous/index.html" }, + { text: "Webhooks", link: "/webhook/index.html" }, ], } diff --git a/docs/webhook/index.md b/docs/webhook/index.md new file mode 100644 index 0000000..2d037dd --- /dev/null +++ b/docs/webhook/index.md @@ -0,0 +1,71 @@ +# Webhook API + +## Sample Webhook Payload + +```php +$payload = [ + 'active' => true, + 'events' => ['address.created', 'shipment.created'], + 'live' => true, + 'name' => 'My Webhook', + 'url' => 'https://example.com/webhook' +]; +``` + +## Create Webhook + +This method allows you to create a new webhook. + +```php +$webhook = TerminalAfrica::createWebhook($payload); +``` + +## Get webhooks + +This method allows you to retrieve a list of webhooks. + +```php +$payload = [ + "perPage": 50, + "page": 3 +]; + +// The $payload parameter is optional. +$webhooks = TerminalAfrica::getWebhooks($payload); +``` + +## Get webhook + +This method allows you to retrieve details of a specific webhook. + +```php +$webhookId = 'xxxxxxxxx'; +$webhook = TerminalAfrica::getWebhook($webhookId); +``` + +## Enable webhook + +This method allows you to enable a webhook. + +```php +$webhookId = 'xxxxxxxxx'; +$webhook = TerminalAfrica::enableWebhook($webhookId); +``` + +## Disable webhook + +This method allows you to disable a webhook. + +```php +$webhookId = 'xxxxxxxxx'; +$webhook = TerminalAfrica::disableWebhook($webhookId); +``` + +## Delete webhook + +This method allows you to delete a webhook. + +```php +$webhookId = 'xxxxxxxxx'; +$webhook = TerminalAfrica::deleteWebhook($webhookId); +``` diff --git a/src/Constants/TerminalAfricaConstant.php b/src/Constants/TerminalAfricaConstant.php index dcb31a0..00087e5 100644 --- a/src/Constants/TerminalAfricaConstant.php +++ b/src/Constants/TerminalAfricaConstant.php @@ -23,4 +23,6 @@ class TerminalAfricaConstant const TRANSACTION_ENDPOINT = '/transactions'; const USER_ENDPOINT = '/users'; + + const WEBHOOK_ENDPOINT = '/webhooks'; } diff --git a/src/TerminalAfricaService.php b/src/TerminalAfricaService.php index 6ac6970..852b446 100755 --- a/src/TerminalAfricaService.php +++ b/src/TerminalAfricaService.php @@ -15,6 +15,7 @@ use Cybernerdie\LaravelTerminalAfrica\Traits\ShipmentApi; use Cybernerdie\LaravelTerminalAfrica\Traits\TransactionApi; use Cybernerdie\LaravelTerminalAfrica\Traits\UserApi; +use Cybernerdie\LaravelTerminalAfrica\Traits\WebhookApi; use GuzzleHttp\Client; use Illuminate\Http\Client\RequestException; @@ -22,7 +23,7 @@ class TerminalAfricaService implements TerminalAfricaContract { use ShipmentApi, RateApi, AddressApi, CarrierApi, ClaimApi, PackagingApi, InsuranceApi, ParcelApi, TransactionApi, - UserApi, LocationApi; + UserApi, LocationApi, WebhookApi; /** * The base uri to consume the Terminal Africa API diff --git a/src/Traits/WebhookApi.php b/src/Traits/WebhookApi.php new file mode 100644 index 0000000..6a3056f --- /dev/null +++ b/src/Traits/WebhookApi.php @@ -0,0 +1,88 @@ +makeRequest( + method: 'POST', + endpoint: $endpoint, + formParams: $formParams + ); + } + + /** + * This method allows you to retrieve a list of webhooks. + */ + public function getWebhooks(array $queryParams = []): array + { + $endpoint = sprintf('%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT); + + return $this->makeRequest( + method: 'GET', + endpoint: $endpoint, + queryParams: $queryParams + ); + } + + /** + * This method allows you to retrieve details of a specific webhook. + */ + public function getWebhook(string $webhookId): array + { + $endpoint = sprintf('%s/%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT, $webhookId); + + return $this->makeRequest( + method: 'GET', + endpoint: $endpoint + ); + } + + /** + * This method allows you to disable a specific webhook. + */ + public function disableWebhook(string $webhookId): array + { + $endpoint = sprintf('%s/disable/%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT, $webhookId); + + return $this->makeRequest( + method: 'POST', + endpoint: $endpoint + ); + } + + /** + * This method allows you to enable a specific webhook. + */ + public function enableWebhook(string $webhookId): array + { + $endpoint = sprintf('%s/enable/%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT, $webhookId); + + return $this->makeRequest( + method: 'POST', + endpoint: $endpoint + ); + } + + /** + * This method allows you to delete a specific webhook. + */ + public function deleteWebhook(string $webhookId): array + { + $endpoint = sprintf('%s/%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT, $webhookId); + + return $this->makeRequest( + method: 'DELETE', + endpoint: $endpoint + ); + } +}