Skip to content

Commit

Permalink
Merge pull request #4 from partnero/webhook_updates
Browse files Browse the repository at this point in the history
Webhook Updates
  • Loading branch information
BrijeshTRC authored Sep 20, 2023
2 parents ebf5cff + 7098f1e commit 7795f3e
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 1 deletion.
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Partnero PHP SDK
* [Transactions API](#transactions)
* [Create transaction](#create-transaction)
* [Delete transaction](#delete-transaction)
* [Webhooks API](#webhooks)
* [Get a list of webhooks](#get-a-list-of-webhooks)
* [Get webhook](#get-webhook)
* [Create webhook](#create-webhook)
* [Update webhook](#update-webhook)
* [Delete webhook](#delete-webhook)
* [Support and Feedback](#support-and-feedback)

# Installation
Expand Down Expand Up @@ -229,11 +235,80 @@ $partnero = new Partnero('api_key');
$partnero->transactions()->delete('transaction_123');
```

<a name="webhooks"></a>
## Webhooks

<a name="get-a-list-of-webhooks"></a>
### Get a list of webhooks

```php
use Partnero\Partnero;

$partnero = new Partnero('api_key');

$partnero->webhooks()->list(50, 1);
```

<a name="get-webhook"></a>
### Get webhook

```php
use Partnero\Partnero;

$partnero = new Partnero('api_key');

$partnero->webhooks()->find('webhook-key');
```

<a name="create-webhook"></a>
### Create webhook

```php
use Partnero\Partnero;
use Partnero\Models\Webhook;

$partnero = new Partnero('api_key');

$webhook = (new Webhook())
->setName('Demo')
->setUrl('https://webhook.site/e68d154a-ad82')
->setEvents([
'partner.created'
]);

$partnero->webhooks()->create($webhook);
```

<a name="update-webhook"></a>
### Update webhook

```php
use Partnero\Partnero;
use Partnero\Models\Webhook;

$partnero = new Partnero('api_key');

$webhook = (new Webhook())->setName('Demo 2');

$partnero->webhooks()->update('webhook-key', $webhook);
```

<a name="delete-webhook"></a>
### Delete webhook

```php
use Partnero\Partnero;

$partnero = new Partnero('api_key');

$partnero->webhooks()->delete('webhook-key');
```

<a name="support-and-feedback"></a>
# Support and Feedback

In case you find any bugs, submit an issue directly here in GitHub.

If you have any troubles using our API or SDK feel free to contact our support by email [[email protected]](mailto:[email protected])

The official documentation is at [https://developers.partnero.com](https://developers.partnero.com)
The official documentation is at [https://developers.partnero.com](https://developers.partnero.com)
105 changes: 105 additions & 0 deletions src/Endpoints/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Partnero\Endpoints;

use JsonException;
use Partnero\Models\Webhook;
use Partnero\Exceptions\RequestException;
use Psr\Http\Client\ClientExceptionInterface;

class Webhooks extends AbstractEndpoint
{
/**
* @return string
*/
protected function getEndpointUri(): string
{
return 'webhooks';
}

/**
* @param int|null $limit
* @param int|null $page
* @return array
* @throws JsonException
* @throws RequestException
* @throws ClientExceptionInterface
*/
public function list(int|null $limit = null, int|null $page = null): array
{
$params = [];

if (!is_null($limit)) {
$params['limit'] = $limit;
}

if (!is_null($page)) {
$params['page'] = $page;
}

return $this->call('get', $params);
}

/**
* @param string|Webhook $key
* @return array
* @throws ClientExceptionInterface
* @throws JsonException
* @throws RequestException
*/
public function find(string|Webhook $key): array
{
return $this->call(
'get',
[],
$this->getEndpointUri() . '/' . $key
);
}

/**
* @param array|Webhook $webhook
* @return array
* @throws ClientExceptionInterface
* @throws JsonException
* @throws RequestException
*/
public function create(array|Webhook $webhook): array
{
return $this->call('post', $this->modelData($webhook));
}

/**
* @param string|Webhook $key
* @param array|Webhook $webhook
* @return array
* @throws ClientExceptionInterface
* @throws JsonException
* @throws RequestException
*/
public function update(string|Webhook $key, array|Webhook $webhook): array
{
return $this->call(
'put',
$this->modelData($webhook),
$this->getEndpointUri() . '/' . $key
);
}

/**
* @param string|Webhook $key
* @return array
* @throws ClientExceptionInterface
* @throws JsonException
* @throws RequestException
*/
public function delete(string|Webhook $key): array
{
return $this->call(
'delete',
[],
$this->getEndpointUri() . '/' . $key
);
}
}
147 changes: 147 additions & 0 deletions src/Models/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace Partnero\Models;

class Webhook extends AbstractModel
{
/**
* @var string|null
*/
protected ?string $key = null;

/**
* @var string|null
*/
protected ?string $name = null;

/**
* @var string|null
*/
protected ?string $url = null;

/**
* @var bool|null
*/
protected ?bool $isActive = null;

/**
* @var array|null
*/
protected ?array $events = null;

/**
* @return string|null
*/
public function getKey(): ?string
{
return $this->key;
}

/**
* @param string|null $key
* @return $this
*/
public function setKey(?string $key): Webhook
{
$this->key = $key;
return $this;
}

/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}

/**
* @param string|null $name
* @return $this
*/
public function setName(?string $name): Webhook
{
$this->name = $name;
return $this;
}

/**
* @return string|null
*/
public function getUrl(): ?string
{
return $this->url;
}

/**
* @param string|null $url
* @return $this
*/
public function setUrl(?string $url): Webhook
{
$this->url = $url;
return $this;
}

/**
* @return bool|null
*/
public function getIsActive(): ?bool
{
return $this->isActive;
}

/**
* @param bool|null $isActive
* @return $this
*/
public function setIsActive(?bool $isActive): Webhook
{
$this->isActive = $isActive;
return $this;
}

/**
* @return string[]|null
*/
public function getEvents(): ?array
{
return $this->events;
}

/**
* @param string[]|null $events
* @return $this
*
* @link https://developers.partnero.com/reference/webhooks.html#available-events
*/
public function setEvents(?array $events): Webhook
{
$this->events = $events;
return $this;
}

/**
* @return string[]
*/
public function __toArray(): array
{
return [
'key' => $this->getKey(),
'name' => $this->getName(),
'url' => $this->getUrl(),
'events' => $this->getEvents(),
'is_active' => $this->getIsActive(),
];
}

/**
* @return string
*/
public function __toString(): string
{
return (string)$this->getKey();
}
}
18 changes: 18 additions & 0 deletions src/Partnero.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Partnero\Endpoints\Customers;
use Partnero\Endpoints\Settings;
use Partnero\Endpoints\Transactions;
use Partnero\Endpoints\Webhooks;

class Partnero
{
Expand Down Expand Up @@ -75,6 +76,11 @@ class Partnero
*/
protected ?Transactions $transactions = null;

/**
* @var Webhooks|null
*/
protected ?Webhooks $webhooks = null;

/**
* @param string $apiKey
* @param array $options
Expand Down Expand Up @@ -191,4 +197,16 @@ public function settings(): Settings

return $this->settings;
}

/**
* @return Webhooks
*/
public function webhooks(): Webhooks
{
if (empty($this->webhooks)) {
$this->webhooks = new Webhooks($this->httpLayer, $this->options);
}

return $this->webhooks;
}
}

0 comments on commit 7795f3e

Please sign in to comment.