Skip to content

Commit d383ea7

Browse files
Merge pull request #4 from hyperwallet/feature/webhook-notifications-endpoint
Webhook notifications endpoint
2 parents 43756e0 + 9f0dab4 commit d383ea7

File tree

4 files changed

+298
-1
lines changed

4 files changed

+298
-1
lines changed

src/Hyperwallet/Hyperwallet.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyperwallet\Model\TransferMethod;
1616
use Hyperwallet\Model\TransferMethodConfiguration;
1717
use Hyperwallet\Model\User;
18+
use Hyperwallet\Model\WebhookNotification;
1819
use Hyperwallet\Response\ListResponse;
1920
use Hyperwallet\Util\ApiClient;
2021

@@ -919,6 +920,42 @@ public function listReceiptsForPrepaidCard($userToken, $prepaidCardToken, $optio
919920
});
920921
}
921922

923+
//--------------------------------------
924+
// Webhook Notifications
925+
//--------------------------------------
926+
927+
/**
928+
* Get a webhook notification
929+
*
930+
* @param string $webhookNotificationToken The webhook notification token
931+
* @return WebhookNotification
932+
*
933+
* @throws HyperwalletArgumentException
934+
* @throws HyperwalletApiException
935+
*/
936+
public function getWebhookNotification($webhookNotificationToken) {
937+
if (empty($webhookNotificationToken)) {
938+
throw new HyperwalletArgumentException('webhookNotificationToken is required!');
939+
}
940+
$body = $this->client->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => $webhookNotificationToken), array());
941+
return new WebhookNotification($body);
942+
}
943+
944+
/**
945+
* List all webhook notifications
946+
*
947+
* @param array $options
948+
* @return ListResponse
949+
*
950+
* @throws HyperwalletApiException
951+
*/
952+
public function listWebhookNotifications($options = array()) {
953+
$body = $this->client->doGet('/rest/v3/webhook-notifications', array(), $options);
954+
return new ListResponse($body, function($entry) {
955+
return new WebhookNotification($entry);
956+
});
957+
}
958+
922959
//--------------------------------------
923960
// Internal utils
924961
//--------------------------------------
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace Hyperwallet\Model;
3+
4+
/**
5+
* Represents a V3 Webhook Notification
6+
*
7+
* @property string $token The webhook notification token
8+
* @property string $type The webhook notification type
9+
* @property \DateTime $createdOn The webhook notification creation date
10+
*
11+
* @package Hyperwallet\Model
12+
*/
13+
class WebhookNotification extends BaseModel {
14+
15+
/**
16+
* The webhook notification payload
17+
*
18+
* @var object
19+
*/
20+
private $object;
21+
22+
/**
23+
* @internal
24+
*
25+
* Read only fields
26+
*
27+
* @var string[]
28+
*/
29+
private static $READ_ONLY_FIELDS = array('token', 'type', 'createdOn');
30+
31+
/**
32+
* Creates a instance of WebhookNotification
33+
*
34+
* @param string[] $properties The default properties
35+
*/
36+
public function __construct(array $properties = array()) {
37+
parent::__construct(self::$READ_ONLY_FIELDS, $properties);
38+
39+
$this->object = null;
40+
if (isset($properties['type'])) {
41+
if (strpos($properties['type'], 'USERS.BANK_ACCOUNTS') === 0) {
42+
$this->object = new BankAccount($properties['object']);
43+
} else if (strpos($properties['type'], 'USERS.PREPAID_CARDS') === 0) {
44+
$this->object = new PrepaidCard($properties['object']);
45+
} else if (strpos($properties['type'], 'USERS') === 0) {
46+
$this->object = new User($properties['object']);
47+
} else if (strpos($properties['type'], 'PAYMENTS') === 0) {
48+
$this->object = new Payment($properties['object']);
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Get the webhook notification token
55+
*
56+
* @return string
57+
*/
58+
public function getToken() {
59+
return $this->token;
60+
}
61+
62+
/**
63+
* Get the webhook notification type
64+
*
65+
* @return string
66+
*/
67+
public function getType() {
68+
return $this->type;
69+
}
70+
71+
/**
72+
* Get the webhook notification creation date
73+
*
74+
* @return \DateTime
75+
*/
76+
public function getCreatedOn() {
77+
return $this->createdOn ? new \DateTime($this->createdOn) : null;
78+
}
79+
80+
/**
81+
* Get the webhook notification payload
82+
*
83+
* @return BankAccount|PrepaidCard|User|Payment|null
84+
*/
85+
public function getObject() {
86+
return $this->object;
87+
}
88+
89+
}

tests/Hyperwallet/Tests/HyperwalletTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testConstructor_changedServer() {
4949
// TLS verification
5050
//--------------------------------------
5151

52-
public function testLisUser_noTLSIssues() {
52+
public function testListUser_noTLSIssues() {
5353
$client = new Hyperwallet('test-username', 'test-password');
5454
try {
5555
$client->listUsers();
@@ -1753,6 +1753,74 @@ public function testListReceiptsForPrepaidCard_withParameters() {
17531753
// Validate mock
17541754
\Phake::verify($apiClientMock)->doGet('/rest/v3/users/{user-token}/prepaid-cards/{prepaid-card-token}/receipts', array('user-token' => 'test-user-token', 'prepaid-card-token' => 'test-prepaid-card-token'), array('test' => 'value'));
17551755
}
1756+
1757+
//--------------------------------------
1758+
// Webhook Notifications
1759+
//--------------------------------------
1760+
1761+
public function testGetWebhookNotification_noUserToken() {
1762+
// Setup
1763+
$client = new Hyperwallet('test-username', 'test-password');
1764+
1765+
try {
1766+
$client->getWebhookNotification('');
1767+
$this->fail('HyperwalletArgumentException expected');
1768+
} catch (HyperwalletArgumentException $e) {
1769+
$this->assertEquals('webhookNotificationToken is required!', $e->getMessage());
1770+
}
1771+
}
1772+
1773+
public function testGetWebhookNotification_allParameters() {
1774+
// Setup
1775+
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
1776+
$apiClientMock = $this->createAndInjectApiClientMock($client);
1777+
1778+
\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => 'test-webhook-notification-token'), array())->thenReturn(array('success' => 'true'));
1779+
1780+
// Run test
1781+
$webhookNotification = $client->getWebhookNotification('test-webhook-notification-token');
1782+
$this->assertNotNull($webhookNotification);
1783+
$this->assertEquals(array('success' => 'true'), $webhookNotification->getProperties());
1784+
1785+
// Validate mock
1786+
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => 'test-webhook-notification-token'), array());
1787+
}
1788+
1789+
public function testListWebhookNotifications_noParameters() {
1790+
// Setup
1791+
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
1792+
$apiClientMock = $this->createAndInjectApiClientMock($client);
1793+
1794+
\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array())->thenReturn(array('count' => 1, 'data' => array()));
1795+
1796+
// Run test
1797+
$webhookNotificationList = $client->listWebhookNotifications();
1798+
$this->assertNotNull($webhookNotificationList);
1799+
$this->assertCount(0, $webhookNotificationList);
1800+
$this->assertEquals(1, $webhookNotificationList->getCount());
1801+
1802+
// Validate mock
1803+
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array());
1804+
}
1805+
1806+
public function testListWebhookNotifications_withParameters() {
1807+
// Setup
1808+
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
1809+
$apiClientMock = $this->createAndInjectApiClientMock($client);
1810+
1811+
\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array('test' => 'value'))->thenReturn(array('count' => 1, 'data' => array(array('success' => 'true'))));
1812+
1813+
// Run test
1814+
$webhookNotificationList = $client->listWebhookNotifications(array('test' => 'value'));
1815+
$this->assertNotNull($webhookNotificationList);
1816+
$this->assertCount(1, $webhookNotificationList);
1817+
$this->assertEquals(1, $webhookNotificationList->getCount());
1818+
1819+
$this->assertEquals(array('success' => 'true'), $webhookNotificationList[0]->getProperties());
1820+
1821+
// Validate mock
1822+
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array('test' => 'value'));
1823+
}
17561824

17571825
//--------------------------------------
17581826
// Internal utils
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Hyperwallet\Tests\Model;
3+
4+
use Hyperwallet\Model\BankAccount;
5+
use Hyperwallet\Model\Payment;
6+
use Hyperwallet\Model\PrepaidCard;
7+
use Hyperwallet\Model\User;
8+
use Hyperwallet\Model\WebhookNotification;
9+
10+
class WebhookNotificationTest extends ModelTestCase {
11+
12+
protected function getModelName() {
13+
return 'WebhookNotification';
14+
}
15+
16+
/**
17+
* @dataProvider ignoredPropertiesProvider
18+
*
19+
* @param string $property The property to look for
20+
*/
21+
public function testGettersForIgnoredProperties($property) {
22+
$this->performGettersForIgnoredPropertiesTest($property);
23+
}
24+
25+
/**
26+
* @dataProvider propertiesProvider
27+
*
28+
* @param string $property The property to look for
29+
*/
30+
public function testGetterReturnValueIsSet($property) {
31+
$this->performGetterReturnValueIsSetTest($property);
32+
}
33+
34+
/**
35+
* @dataProvider propertiesProvider
36+
*
37+
* @param string $property The property to look for
38+
*/
39+
public function testGetterReturnValueIsNotSet($property) {
40+
$this->performGetterReturnValueIsNotSetTest($property);
41+
}
42+
43+
/**
44+
* @dataProvider notificationTypeProvider
45+
*
46+
* @param string $type The notification type
47+
* @param object $clazz The expected class type
48+
*
49+
*/
50+
public function testConstructorObjectConversion($type, $clazz) {
51+
$data = array(
52+
'type' => $type,
53+
'test2' => 'value2',
54+
'object' => array(
55+
'test' => 'value'
56+
)
57+
);
58+
59+
$notification = new WebhookNotification($data);
60+
if ($clazz === null) {
61+
$this->assertNull($notification->getObject());
62+
} else {
63+
$this->assertNotNull($notification->getObject());
64+
$this->assertInstanceOf($clazz, $notification->getObject());
65+
66+
$this->assertEquals(array(
67+
'test' => 'value'
68+
), $notification->getObject()->getProperties());
69+
}
70+
}
71+
72+
public function notificationTypeProvider() {
73+
return array(
74+
'USERS.CREATED' => array('USERS.CREATED', User::class),
75+
'USERS.UPDATED.STATUS.ACTIVATED' => array('USERS.UPDATED.STATUS.ACTIVATED', User::class),
76+
'USERS.UPDATED.STATUS.LOCKED' => array('USERS.UPDATED.STATUS.LOCKED', User::class),
77+
'USERS.UPDATED.STATUS.FROZEN' => array('USERS.UPDATED.STATUS.FROZEN', User::class),
78+
'USERS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.UPDATED.STATUS.DE_ACTIVATED', User::class),
79+
80+
'USERS.BANK_ACCOUNTS.CREATED' => array('USERS.BANK_ACCOUNTS.CREATED', BankAccount::class),
81+
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.ACTIVATED' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.ACTIVATED', BankAccount::class),
82+
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.INVALID' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.INVALID', BankAccount::class),
83+
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.DE_ACTIVATED', BankAccount::class),
84+
85+
'USERS.PREPAID_CARDS.CREATED' => array('USERS.PREPAID_CARDS.CREATED', PrepaidCard::class),
86+
'USERS.PREPAID_CARDS.UPDATED.STATUS.QUEUED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.QUEUED', PrepaidCard::class),
87+
'USERS.PREPAID_CARDS.UPDATED.STATUS.PRE_ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.PRE_ACTIVATED', PrepaidCard::class),
88+
'USERS.PREPAID_CARDS.UPDATED.STATUS.ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.ACTIVATED', PrepaidCard::class),
89+
'USERS.PREPAID_CARDS.UPDATED.STATUS.DECLINED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.DECLINED', PrepaidCard::class),
90+
'USERS.PREPAID_CARDS.UPDATED.STATUS.LOCKED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.LOCKED', PrepaidCard::class),
91+
'USERS.PREPAID_CARDS.UPDATED.STATUS.SUSPENDED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.SUSPENDED', PrepaidCard::class),
92+
'USERS.PREPAID_CARDS.UPDATED.STATUS.LOST_OR_STOLEN' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.LOST_OR_STOLEN', PrepaidCard::class),
93+
'USERS.PREPAID_CARDS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.DE_ACTIVATED', PrepaidCard::class),
94+
'USERS.PREPAID_CARDS.UPDATED.STATUS.COMPLIANCE_HOLD' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.COMPLIANCE_HOLD', PrepaidCard::class),
95+
'USERS.PREPAID_CARDS.UPDATED.STATUS.KYC_HOLD' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.KYC_HOLD', PrepaidCard::class),
96+
97+
'PAYMENTS.CREATED' => array('PAYMENTS.CREATED', Payment::class),
98+
99+
'TEST' => array('TEST', null),
100+
);
101+
}
102+
103+
}

0 commit comments

Comments
 (0)