Skip to content

Commit

Permalink
Add Global Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdmgv committed Mar 31, 2017
1 parent c9fe36d commit db96671
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Removed
- [x] Removed Email Credentials API, it is never used.

## [2.0.7] 2017-03-30

### Added
- [x] Added support for Global `Webhooks`

## [2.0.6] 2017-01-07

### Added
Expand Down
4 changes: 3 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
* @method Services\CouponService coupons()
* @method Services\RedemptionService couponsRedemptions()
* @method Services\WebsiteWebhookService websiteWebhook()
* @method Services\WebhooksService webhooks()
* @method Services\ValuesListService lists()
* @method Services\ValuesListTrackingService listsTracking()
*
Expand All @@ -102,7 +103,7 @@ final class Client
const BASE_HOST = 'https://api.rebilly.com';
const SANDBOX_HOST = 'https://api-sandbox.rebilly.com';
const CURRENT_VERSION = 'v2.1';
const SDK_VERSION = '2.0.6';
const SDK_VERSION = '2.0.7';

private static $services = [
'authenticationOptions' => Services\AuthenticationOptionsService::class,
Expand Down Expand Up @@ -146,6 +147,7 @@ final class Client
'coupons' => Services\CouponService::class,
'couponsRedemptions' => Services\RedemptionService::class,
'websiteWebhook' => Services\WebsiteWebhookService::class,
'webhooks' => Services\WebhooksService::class,
'lists' => Services\ValuesListService::class,
'listsTracking' => Services\ValuesListTrackingService::class,
'shippingZones' => Services\ShippingZoneService::class,
Expand Down
9 changes: 9 additions & 0 deletions src/Entities/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ public function __construct()
'tracking/lists' => function (array $content) {
return new Collection(new ValuesList(), $content);
},
'webhooks' => function (array $content) {
return new Collection(new Webhook(), $content);
},
'webhooks/{webhookId}' => function (array $content) {
return new Webhook($content);
},
'previews/webhooks' => function (array $content) {
return new Webhook($content);
},
];
}

Expand Down
150 changes: 150 additions & 0 deletions src/Entities/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Rebilly\Entities;

use Rebilly\Rest\Entity;

/**
* Class WebsiteWebhook
*
* ```json
* {
* "id"
* "eventsFilter"
* "status"
* "method"
* "url"
* "headers"
* "credentialHash"
* "createdTime"
* "updatedTime"
* "_links"
* }
* ```
*/
final class Webhook extends Entity
{
/**
* @return array
*/
public function getEventsFilter()
{
return $this->getAttribute('eventsFilter');
}

/**
* @param array $value
*
* @return $this
*/
public function setEventsFilter($value)
{
return $this->setAttribute('eventsFilter', $value);
}

/**
* @return string
*/
public function getStatus()
{
return $this->getAttribute('status');
}

/**
* @param string $value
*
* @return string
*/
public function setStatus($value)
{
return $this->setAttribute('status', $value);
}

/**
* @return string
*/
public function getMethod()
{
return $this->getAttribute('method');
}

/**
* @param string $value
*
* @return $this
*/
public function setMethod($value)
{
return $this->setAttribute('method', $value);
}

/**
* @return string
*/
public function getUrl()
{
return $this->getAttribute('url');
}

/**
* @param string $value
*
* @return $this
*/
public function setUrl($value)
{
return $this->setAttribute('url', $value);
}

/**
* @return array
*/
public function getHeaders()
{
return $this->getAttribute('headers');
}

/**
* @param array $value
*
* @return $this
*/
public function setHeaders($value)
{
return $this->setAttribute('headers', $value);
}

/**
* @return string
*/
public function getCredentialHash()
{
return $this->getAttribute('credentialHash');
}

/**
* @param string $value
*
* @return $this
*/
public function setCredentialHash($value)
{
return $this->setAttribute('credentialHash', $value);
}

/**
* @return string
*/
public function getCreatedTime()
{
return $this->getAttribute('createdTime');
}

/**
* @return string
*/
public function getSelfLink()
{
return $this->getLink('self');
}
}
95 changes: 95 additions & 0 deletions src/Services/WebhooksService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the PHP Rebilly API package.
*
* (c) 2015 Rebilly SRL
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Rebilly\Services;

use ArrayObject;
use JsonSerializable;
use Rebilly\Http\Exception\NotFoundException;
use Rebilly\Http\Exception\UnprocessableEntityException;
use Rebilly\Paginator;
use Rebilly\Rest\Collection;
use Rebilly\Rest\Service;

/**
* Class WebhooksService
*/
final class WebhooksService extends Service
{
/**
* @param array|ArrayObject $params
*
* @return Webkook[][]|Collection[]|Paginator
*/
public function paginator($params = [])
{
return new Paginator($this->client(), 'webhooks', $params);
}

/**
* @param array|ArrayObject $params
*
* @return Webkook[]|Collection
*/
public function search($params = [])
{
return $this->client()->get('webhooks', $params);
}

/**
* @param string $webhookId
*
* @throws NotFoundException The resource data does exist
*
* @return Webkook
*/
public function load($webhookId)
{
return $this->client()->get('webhooks/{webhookId}', ['webhookId' => $webhookId]);
}

/**
* @param array|JsonSerializable|Webkook $data
* @param null $webhookId
*
* @return Webkook
*/
public function create($data, $webhookId = null)
{
if (isset($webhookId)) {
return $this->client()->put($data, 'webhooks/{webhookId}', ['webhookId' => $webhookId]);
}

return $this->client()->post($data, 'webhooks');
}

/**
* @param string $webhookId
* @param array|JsonSerializable|Webkook $data
*
* @throws UnprocessableEntityException The input data does not valid
*
* @return Webkook
*/
public function update($webhookId, $data)
{
return $this->client()->put($data, 'webhooks/{webhookId}', ['webhookId' => $webhookId]);
}

/**
* @param $data
*
* @return mixed
*/
public function preview($data)
{
return $this->client()->post($data, 'previews/webhooks');
}
}
25 changes: 25 additions & 0 deletions tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ public function provideEntityClasses()
[Entities\Coupons\Redemption::class],
[Entities\ValuesList::class],
[Entities\Product::class],
[Entities\Webhook::class],
];
}

Expand Down Expand Up @@ -956,6 +957,11 @@ public function provideServiceClasses()
Services\ShippingZoneService::class,
Entities\Shipping\ShippingZone::class,
],
[
'webhooks',
Services\WebhooksService::class,
Entities\Webhook::class,
],
];
}

Expand Down Expand Up @@ -1083,6 +1089,25 @@ private function getFakeValue($attribute, $class)
return $faker->userName;
case 'webHookPassword':
return $faker->md5;
case 'eventsFilter':
return $faker->randomElements([
"gateway-account-requested",
"subscription-trial-ended",
"subscription-activated",
"subscription-canceled",
"subscription-renewed",
"transaction-processed",
"payment-card-expired",
"payment-declined",
"invoice-modified",
"invoice-created",
"dispute-created",
"suspended-payment-completed"
], 3);
case 'headers':
return [["name" => $faker->word, "value" => $faker->word, "status" => $faker->randomElement(["active", "inactive"])]];
case 'credentialHash':
return $faker->uuid;
case 'isActive':
case 'archived':
case 'starred':
Expand Down

0 comments on commit db96671

Please sign in to comment.