From 036fc2beec0a3e8e70fb15b9c45bf46ee458cb15 Mon Sep 17 00:00:00 2001 From: BrijeshTRC Date: Wed, 20 Sep 2023 15:43:36 +0530 Subject: [PATCH 1/2] Webhook endpoints & model added --- src/Endpoints/Webhooks.php | 105 ++++++++++++++++++++++++++ src/Models/Webhook.php | 147 +++++++++++++++++++++++++++++++++++++ src/Partnero.php | 18 +++++ 3 files changed, 270 insertions(+) create mode 100644 src/Endpoints/Webhooks.php create mode 100644 src/Models/Webhook.php diff --git a/src/Endpoints/Webhooks.php b/src/Endpoints/Webhooks.php new file mode 100644 index 0000000..d4aaebf --- /dev/null +++ b/src/Endpoints/Webhooks.php @@ -0,0 +1,105 @@ +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 + ); + } +} diff --git a/src/Models/Webhook.php b/src/Models/Webhook.php new file mode 100644 index 0000000..43e2ca2 --- /dev/null +++ b/src/Models/Webhook.php @@ -0,0 +1,147 @@ +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(); + } +} diff --git a/src/Partnero.php b/src/Partnero.php index fed8b96..6fc30ec 100644 --- a/src/Partnero.php +++ b/src/Partnero.php @@ -11,6 +11,7 @@ use Partnero\Endpoints\Customers; use Partnero\Endpoints\Settings; use Partnero\Endpoints\Transactions; +use Partnero\Endpoints\Webhooks; class Partnero { @@ -75,6 +76,11 @@ class Partnero */ protected ?Transactions $transactions = null; + /** + * @var Webhooks|null + */ + protected ?Webhooks $webhooks = null; + /** * @param string $apiKey * @param array $options @@ -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; + } } From 7098f1e8e027c291064a4e49735aa524e3c72749 Mon Sep 17 00:00:00 2001 From: BrijeshTRC Date: Wed, 20 Sep 2023 18:21:48 +0530 Subject: [PATCH 2/2] Example updated for webhook --- README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5305b20..15c2f35 100644 --- a/README.md +++ b/README.md @@ -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 @@ -229,6 +235,75 @@ $partnero = new Partnero('api_key'); $partnero->transactions()->delete('transaction_123'); ``` + +## Webhooks + + +### Get a list of webhooks + +```php +use Partnero\Partnero; + +$partnero = new Partnero('api_key'); + +$partnero->webhooks()->list(50, 1); +``` + + +### Get webhook + +```php +use Partnero\Partnero; + +$partnero = new Partnero('api_key'); + +$partnero->webhooks()->find('webhook-key'); +``` + + +### 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); +``` + + +### 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); +``` + + +### Delete webhook + +```php +use Partnero\Partnero; + +$partnero = new Partnero('api_key'); + +$partnero->webhooks()->delete('webhook-key'); +``` + # Support and Feedback @@ -236,4 +311,4 @@ 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 [hello@partnero.com](mailto:hello@partnero.com) -The official documentation is at [https://developers.partnero.com](https://developers.partnero.com) \ No newline at end of file +The official documentation is at [https://developers.partnero.com](https://developers.partnero.com)