From 31069c75b3254643913a9be2e133a70dfc26e499 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Fri, 26 Jul 2024 13:54:27 +0200 Subject: [PATCH] Add Webhook API support --- src/Api/v1/Webhook.php | 113 ++++++++++++++++++ src/Api/v1/WebhookInterface.php | 37 ++++++ src/Model/Webhook/v1/GetWebhooksResponse.php | 29 +++++ .../Webhook/v1/RegisterWebhookRequest.php | 50 ++++++++ .../Webhook/v1/RegisterWebhookResponse.php | 44 +++++++ src/Model/Webhook/v1/Webhook.php | 59 +++++++++ src/Resource/Webhook/v1/DeleteWebhook.php | 55 +++++++++ src/Resource/Webhook/v1/GetWebhooks.php | 60 ++++++++++ src/Resource/Webhook/v1/RegisterWebhook.php | 69 +++++++++++ 9 files changed, 516 insertions(+) create mode 100644 src/Api/v1/Webhook.php create mode 100644 src/Api/v1/WebhookInterface.php create mode 100644 src/Model/Webhook/v1/GetWebhooksResponse.php create mode 100644 src/Model/Webhook/v1/RegisterWebhookRequest.php create mode 100644 src/Model/Webhook/v1/RegisterWebhookResponse.php create mode 100644 src/Model/Webhook/v1/Webhook.php create mode 100644 src/Resource/Webhook/v1/DeleteWebhook.php create mode 100644 src/Resource/Webhook/v1/GetWebhooks.php create mode 100644 src/Resource/Webhook/v1/RegisterWebhook.php diff --git a/src/Api/v1/Webhook.php b/src/Api/v1/Webhook.php new file mode 100644 index 0000000..a4ce4fe --- /dev/null +++ b/src/Api/v1/Webhook.php @@ -0,0 +1,113 @@ +merchantSerialNumber)) { + throw new InvalidArgumentException('Missing merchant serial number'); + } + return $this->merchantSerialNumber; + } + + /** + * @return string + */ + public function getVersion() + { + return $this->version; + } + + /** + * Webhook constructor. + * + * Webhook API needs one extra param - merchant serial number. + * + * @param \zaporylie\Vipps\VippsInterface $app + * @param string $subscription_key + * @param $merchant_serial_number + */ + public function __construct( + VippsInterface $app, + $subscription_key, + $merchant_serial_number + ) { + parent::__construct($app, $subscription_key); + $this->merchantSerialNumber = $merchant_serial_number; + $this->version = 'v1'; + } + + /** + * {@inheritDoc} + */ + public function registerWebhook(RegisterWebhookRequest $request): RegisterWebhookResponse + { + $resource = new RegisterWebhook($this->app, $this->getSubscriptionKey(), $request); + return $resource->call(); + } + + /** + * {@inheritDoc} + */ + public function getWebhooks(): GetWebhooksResponse + { + $resource = new GetWebhooks($this->app, $this->getSubscriptionKey()); + return $resource->call(); + } + + /** + * {@inheritDoc} + */ + public function deleteWebhook(string $reference): void + { + $resource = new DeleteWebhook($this->app, $this->getSubscriptionKey(), $reference); + $resource->call(); + } +} diff --git a/src/Api/v1/WebhookInterface.php b/src/Api/v1/WebhookInterface.php new file mode 100644 index 0000000..b91a766 --- /dev/null +++ b/src/Api/v1/WebhookInterface.php @@ -0,0 +1,37 @@ +") + */ + protected $webhooks; + + /** + * Gets webhooks value. + * + * @return \zaporylie\Vipps\Model\Webhook\v1\Webhook[] + */ + public function getWebhooks(): array { + return $this->webhooks; + } +} diff --git a/src/Model/Webhook/v1/RegisterWebhookRequest.php b/src/Model/Webhook/v1/RegisterWebhookRequest.php new file mode 100644 index 0000000..f32be51 --- /dev/null +++ b/src/Model/Webhook/v1/RegisterWebhookRequest.php @@ -0,0 +1,50 @@ +") + */ + protected $events; + + /** + * Sets url variable. + * + * @param string $url + * + * @return $this + */ + public function setUrl(string $url) { + $this->url = $url; + return $this; + } + + /** + * Sets events variable. + * + * @param array $events + * + * @return $this + */ + public function setEvents(array $events) { + $this->events = $events; + return $this; + } +} diff --git a/src/Model/Webhook/v1/RegisterWebhookResponse.php b/src/Model/Webhook/v1/RegisterWebhookResponse.php new file mode 100644 index 0000000..17ac625 --- /dev/null +++ b/src/Model/Webhook/v1/RegisterWebhookResponse.php @@ -0,0 +1,44 @@ +id; + } + + /** + * Gets secret value. + * + * @return string + */ + public function getSecret(): string { + return $this->secret; + } +} diff --git a/src/Model/Webhook/v1/Webhook.php b/src/Model/Webhook/v1/Webhook.php new file mode 100644 index 0000000..de751bd --- /dev/null +++ b/src/Model/Webhook/v1/Webhook.php @@ -0,0 +1,59 @@ +") + */ + protected $events; + + /** + * Gets id value. + * + * @return string + */ + public function getId(): string { + return $this->id; + } + + /** + * Gets url value. + * + * @return string + */ + public function getUrl(): string { + return $this->url; + } + + /** + * Gets events value. + * + * @return array + */ + public function getEvents(): array { + return $this->events; + } +} diff --git a/src/Resource/Webhook/v1/DeleteWebhook.php b/src/Resource/Webhook/v1/DeleteWebhook.php new file mode 100644 index 0000000..281d31c --- /dev/null +++ b/src/Resource/Webhook/v1/DeleteWebhook.php @@ -0,0 +1,55 @@ +id = $reference; + } + + /** + * @return mixed + */ + public function call() + { + $response = $this->makeCall(); + $body = $response->getBody()->getContents(); + return $body; + } +} diff --git a/src/Resource/Webhook/v1/GetWebhooks.php b/src/Resource/Webhook/v1/GetWebhooks.php new file mode 100644 index 0000000..e4e606b --- /dev/null +++ b/src/Resource/Webhook/v1/GetWebhooks.php @@ -0,0 +1,60 @@ +makeCall(); + /** @var \zaporylie\Vipps\Model\Webhook\v1\GetWebhooksResponse $responseObject */ + $responseObject = $this + ->getSerializer() + ->deserialize( + $response->getBody()->getContents(), + GetWebhooksResponse::class, + 'json' + ); + + return $responseObject; + } +} diff --git a/src/Resource/Webhook/v1/RegisterWebhook.php b/src/Resource/Webhook/v1/RegisterWebhook.php new file mode 100644 index 0000000..ceb9991 --- /dev/null +++ b/src/Resource/Webhook/v1/RegisterWebhook.php @@ -0,0 +1,69 @@ +body = $this + ->getSerializer() + ->serialize( + $request, + 'json' + ); + // This is important bit for making webhooks api not to fail. + $this->headers['Content-Type'] = 'application/json'; + } + + /** + * @return \zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse + */ + public function call() + { + $response = $this->makeCall(); + /** @var \zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse $responseObject */ + $responseObject = $this + ->getSerializer() + ->deserialize( + $response->getBody()->getContents(), + RegisterWebhookResponse::class, + 'json' + ); + + return $responseObject; + } +}