Skip to content

Commit

Permalink
Add Webhook API support
Browse files Browse the repository at this point in the history
  • Loading branch information
zaporylie committed Jul 26, 2024
1 parent ca33bd4 commit 31069c7
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/Api/v1/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace zaporylie\Vipps\Api\v1;

use zaporylie\Vipps\Api\ApiBase;
use zaporylie\Vipps\Exceptions\Api\InvalidArgumentException;
use zaporylie\Vipps\Model\EPayment\v1\CancelModificationRequest;
use zaporylie\Vipps\Model\EPayment\v1\CaptureModificationRequest;
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentRequest;
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentResponse;
use zaporylie\Vipps\Model\EPayment\v1\GetPaymentResponse;
use zaporylie\Vipps\Model\EPayment\v1\PaymentAdjustResponse;
use zaporylie\Vipps\Model\EPayment\v1\RefundModificationRequest;
use zaporylie\Vipps\Model\Webhook\v1\GetWebhooksResponse;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookRequest;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse;
use zaporylie\Vipps\Resource\EPayment\v1\CapturePayment;
use zaporylie\Vipps\Resource\EPayment\v1\CreatePayment;
use zaporylie\Vipps\Resource\EPayment\v1\GetPayment;
use zaporylie\Vipps\Resource\EPayment\v1\GetPaymentEvents;
use zaporylie\Vipps\Resource\EPayment\v1\CancelPayment;
use zaporylie\Vipps\Resource\EPayment\v1\RefundPayment;
use zaporylie\Vipps\Resource\IdempotencyKeyFactory;
use zaporylie\Vipps\Resource\Webhook\v1\DeleteWebhook;
use zaporylie\Vipps\Resource\Webhook\v1\GetWebhooks;
use zaporylie\Vipps\Resource\Webhook\v1\RegisterWebhook;
use zaporylie\Vipps\VippsInterface;

/**
* Class Webhook
*
* @package Vipps\Api
*/
class Webhook extends ApiBase implements WebhookInterface
{

/**
* @var string
*/
protected $merchantSerialNumber;

/**
* @var string
*/
protected $version;

/**
* Gets merchantSerialNumber value.
*
* @return string
*/
public function getMerchantSerialNumber()
{
if (empty($this->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();
}
}
37 changes: 37 additions & 0 deletions src/Api/v1/WebhookInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace zaporylie\Vipps\Api\v1;

use zaporylie\Vipps\Model\EPayment\v1\CancelModificationRequest;
use zaporylie\Vipps\Model\EPayment\v1\CaptureModificationRequest;
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentRequest;
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentResponse;
use zaporylie\Vipps\Model\EPayment\v1\GetPaymentResponse;
use zaporylie\Vipps\Model\EPayment\v1\PaymentAdjustResponse;
use zaporylie\Vipps\Model\EPayment\v1\RefundModificationRequest;
use zaporylie\Vipps\Model\Webhook\v1\GetWebhooksResponse;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookRequest;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse;

interface WebhookInterface
{

/**
* @param \zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookRequest $request
*
* @return \zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse
*/
public function registerWebhook(RegisterWebhookRequest $request) : RegisterWebhookResponse;

/**
* @return \zaporylie\Vipps\Model\Webhook\v1\GetWebhooksResponse
*/
public function getWebhooks() : GetWebhooksResponse;

/**
* @param string $reference
*
* @return void
*/
public function deleteWebhook(string $reference) : void;
}
29 changes: 29 additions & 0 deletions src/Model/Webhook/v1/GetWebhooksResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace zaporylie\Vipps\Model\Webhook\v1;

use JMS\Serializer\Annotation as Serializer;

/**
* Class RegisterWebhookResponse
*
* @package Vipps\Model\Webhook
*/
class GetWebhooksResponse
{

/**
* @var \zaporylie\Vipps\Model\Webhook\v1\Webhook[]
* @Serializer\Type("array<zaporylie\Vipps\Model\Webhook\v1\Webhook>")
*/
protected $webhooks;

/**
* Gets webhooks value.
*
* @return \zaporylie\Vipps\Model\Webhook\v1\Webhook[]
*/
public function getWebhooks(): array {
return $this->webhooks;
}
}
50 changes: 50 additions & 0 deletions src/Model/Webhook/v1/RegisterWebhookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace zaporylie\Vipps\Model\Webhook\v1;

use JMS\Serializer\Annotation as Serializer;

/**
* Class RegisterWebhookRequest
*
* @package Vipps\Model\Webhook
*/
class RegisterWebhookRequest
{

/**
* @var string
* @Serializer\Type("string")
*/
protected $url;

/**
* @var array
* @Serializer\Type("array<string>")
*/
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;
}
}
44 changes: 44 additions & 0 deletions src/Model/Webhook/v1/RegisterWebhookResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace zaporylie\Vipps\Model\Webhook\v1;

use JMS\Serializer\Annotation as Serializer;

/**
* Class RegisterWebhookResponse
*
* @package Vipps\Model\Webhook
*/
class RegisterWebhookResponse
{

/**
* @var string
* @Serializer\Type("string")
*/
protected $id;

/**
* @var string
* @Serializer\Type("string")
*/
protected $secret;

/**
* Gets id value.
*
* @return string
*/
public function getId(): string {
return $this->id;
}

/**
* Gets secret value.
*
* @return string
*/
public function getSecret(): string {
return $this->secret;
}
}
59 changes: 59 additions & 0 deletions src/Model/Webhook/v1/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace zaporylie\Vipps\Model\Webhook\v1;

use JMS\Serializer\Annotation as Serializer;

/**
* Class Webhook
*
* @package Vipps\Model\Webhook
*/
class Webhook
{

/**
* @var string
* @Serializer\Type("string")
*/
protected $id;

/**
* @var string
* @Serializer\Type("string")
*/
protected $url;

/**
* @var array
* @Serializer\Type("array<string>")
*/
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;
}
}
55 changes: 55 additions & 0 deletions src/Resource/Webhook/v1/DeleteWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace zaporylie\Vipps\Resource\Webhook\v1;

use zaporylie\Vipps\Model\Authorization\ResponseGetToken;
use zaporylie\Vipps\Model\EPayment\v1\EventLog;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookRequest;
use zaporylie\Vipps\Model\Webhook\v1\RegisterWebhookResponse;
use zaporylie\Vipps\Model\Webhook\v1\Webhook;
use zaporylie\Vipps\Resource\AuthorizedResourceBase;
use zaporylie\Vipps\Resource\ResourceBase;
use zaporylie\Vipps\Resource\HttpMethod;
use zaporylie\Vipps\VippsInterface;

/**
* Class Webhook
*
* @package Vipps\Resource\Webhook
*/
class DeleteWebhook extends AuthorizedResourceBase
{

/**
* @var \zaporylie\Vipps\Resource\HttpMethod;
*/
protected $method = HttpMethod::DELETE;

/**
* @var string
*/
protected $path = '/webhooks/v1/webhooks/{id}';

/**
* GetToken constructor.
*
* @param \zaporylie\Vipps\VippsInterface $vipps
* @param string $subscription_key
* @param $reference
*/
public function __construct(VippsInterface $vipps, $subscription_key, $reference)
{
parent::__construct($vipps, $subscription_key);
$this->id = $reference;
}

/**
* @return mixed
*/
public function call()
{
$response = $this->makeCall();
$body = $response->getBody()->getContents();
return $body;
}
}
Loading

0 comments on commit 31069c7

Please sign in to comment.