Skip to content

Commit

Permalink
Added methods to handle webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Paul authored and Joshua Paul committed May 13, 2023
1 parent 529ed0e commit c42e183
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
}

Expand Down
71 changes: 71 additions & 0 deletions docs/webhook/index.md
Original file line number Diff line number Diff line change
@@ -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);
```
2 changes: 2 additions & 0 deletions src/Constants/TerminalAfricaConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class TerminalAfricaConstant
const TRANSACTION_ENDPOINT = '/transactions';

const USER_ENDPOINT = '/users';

const WEBHOOK_ENDPOINT = '/webhooks';
}
3 changes: 2 additions & 1 deletion src/TerminalAfricaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
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;

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
Expand Down
88 changes: 88 additions & 0 deletions src/Traits/WebhookApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Cybernerdie\LaravelTerminalAfrica\Traits;

use Cybernerdie\LaravelTerminalAfrica\Constants\TerminalAfricaConstant;

trait WebhookApi
{
/**
* This method allows you to create a new webhook.
*/
public function createWebhook(array $formParams): array
{
$endpoint = sprintf('%s', TerminalAfricaConstant::WEBHOOK_ENDPOINT);

return $this->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
);
}
}

1 comment on commit c42e183

@vercel
Copy link

@vercel vercel bot commented on c42e183 May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.