Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Hyperwallet/Hyperwallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyperwallet\Model\Receipt;
use Hyperwallet\Model\TransferMethodConfiguration;
use Hyperwallet\Model\User;
use Hyperwallet\Model\WebhookNotification;
use Hyperwallet\Response\ListResponse;
use Hyperwallet\Util\ApiClient;

Expand Down Expand Up @@ -887,6 +888,42 @@ public function listReceiptsForPrepaidCard($userToken, $prepaidCardToken, $optio
});
}

//--------------------------------------
// Webhook Notifications
//--------------------------------------

/**
* Get a webhook notification
*
* @param string $webhookNotificationToken The webhook notification token
* @return WebhookNotification
*
* @throws HyperwalletArgumentException
* @throws HyperwalletApiException
*/
public function getWebhookNotification($webhookNotificationToken) {
if (empty($webhookNotificationToken)) {
throw new HyperwalletArgumentException('webhookNotificationToken is required!');
}
$body = $this->client->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => $webhookNotificationToken), array());
return new WebhookNotification($body);
}

/**
* List all webhook notifications
*
* @param array $options
* @return ListResponse
*
* @throws HyperwalletApiException
*/
public function listWebhookNotifications($options = array()) {
$body = $this->client->doGet('/rest/v3/webhook-notifications', array(), $options);
return new ListResponse($body, function($entry) {
return new WebhookNotification($entry);
});
}

//--------------------------------------
// Internal utils
//--------------------------------------
Expand Down
89 changes: 89 additions & 0 deletions src/Hyperwallet/Model/WebhookNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
namespace Hyperwallet\Model;

/**
* Represents a V3 Webhook Notification
*
* @property string $token The webhook notification token
* @property string $type The webhook notification type
* @property \DateTime $createdOn The webhook notification creation date
*
* @package Hyperwallet\Model
*/
class WebhookNotification extends BaseModel {

/**
* The webhook notification payload
*
* @var object
*/
private $object;

/**
* @internal
*
* Read only fields
*
* @var string[]
*/
private static $READ_ONLY_FIELDS = array('token', 'type', 'createdOn');

/**
* Creates a instance of WebhookNotification
*
* @param string[] $properties The default properties
*/
public function __construct(array $properties = array()) {
parent::__construct(self::$READ_ONLY_FIELDS, $properties);

$this->object = null;
if (isset($properties['type'])) {
if (strpos($properties['type'], 'USERS.BANK_ACCOUNTS') === 0) {
$this->object = new BankAccount($properties['object']);
} else if (strpos($properties['type'], 'USERS.PREPAID_CARDS') === 0) {
$this->object = new PrepaidCard($properties['object']);
} else if (strpos($properties['type'], 'USERS') === 0) {
$this->object = new User($properties['object']);
} else if (strpos($properties['type'], 'PAYMENTS') === 0) {
$this->object = new Payment($properties['object']);
}
}
}

/**
* Get the webhook notification token
*
* @return string
*/
public function getToken() {
return $this->token;
}

/**
* Get the webhook notification type
*
* @return string
*/
public function getType() {
return $this->type;
}

/**
* Get the webhook notification creation date
*
* @return \DateTime
*/
public function getCreatedOn() {
return $this->createdOn ? new \DateTime($this->createdOn) : null;
}

/**
* Get the webhook notification payload
*
* @return BankAccount|PrepaidCard|User|Payment|null
*/
public function getObject() {
return $this->object;
}

}
70 changes: 69 additions & 1 deletion tests/Hyperwallet/Tests/HyperwalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testConstructor_changedServer() {
// TLS verification
//--------------------------------------

public function testLisUser_noTLSIssues() {
public function testListUser_noTLSIssues() {
$client = new Hyperwallet('test-username', 'test-password');
try {
$client->listUsers();
Expand Down Expand Up @@ -1632,6 +1632,74 @@ public function testListReceiptsForPrepaidCard_withParameters() {
// Validate mock
\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'));
}

//--------------------------------------
// Webhook Notifications
//--------------------------------------

public function testGetWebhookNotification_noUserToken() {
// Setup
$client = new Hyperwallet('test-username', 'test-password');

try {
$client->getWebhookNotification('');
$this->fail('HyperwalletArgumentException expected');
} catch (HyperwalletArgumentException $e) {
$this->assertEquals('webhookNotificationToken is required!', $e->getMessage());
}
}

public function testGetWebhookNotification_allParameters() {
// Setup
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
$apiClientMock = $this->createAndInjectApiClientMock($client);

\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => 'test-webhook-notification-token'), array())->thenReturn(array('success' => 'true'));

// Run test
$webhookNotification = $client->getWebhookNotification('test-webhook-notification-token');
$this->assertNotNull($webhookNotification);
$this->assertEquals(array('success' => 'true'), $webhookNotification->getProperties());

// Validate mock
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications/{webhook-notification-token}', array('webhook-notification-token' => 'test-webhook-notification-token'), array());
}

public function testListWebhookNotifications_noParameters() {
// Setup
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
$apiClientMock = $this->createAndInjectApiClientMock($client);

\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array())->thenReturn(array('count' => 1, 'data' => array()));

// Run test
$webhookNotificationList = $client->listWebhookNotifications();
$this->assertNotNull($webhookNotificationList);
$this->assertCount(0, $webhookNotificationList);
$this->assertEquals(1, $webhookNotificationList->getCount());

// Validate mock
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array());
}

public function testListWebhookNotifications_withParameters() {
// Setup
$client = new Hyperwallet('test-username', 'test-password', 'test-program-token');
$apiClientMock = $this->createAndInjectApiClientMock($client);

\Phake::when($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array('test' => 'value'))->thenReturn(array('count' => 1, 'data' => array(array('success' => 'true'))));

// Run test
$webhookNotificationList = $client->listWebhookNotifications(array('test' => 'value'));
$this->assertNotNull($webhookNotificationList);
$this->assertCount(1, $webhookNotificationList);
$this->assertEquals(1, $webhookNotificationList->getCount());

$this->assertEquals(array('success' => 'true'), $webhookNotificationList[0]->getProperties());

// Validate mock
\Phake::verify($apiClientMock)->doGet('/rest/v3/webhook-notifications', array(), array('test' => 'value'));
}

//--------------------------------------
// Internal utils
Expand Down
103 changes: 103 additions & 0 deletions tests/Hyperwallet/Tests/Model/WebhookNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace Hyperwallet\Tests\Model;

use Hyperwallet\Model\BankAccount;
use Hyperwallet\Model\Payment;
use Hyperwallet\Model\PrepaidCard;
use Hyperwallet\Model\User;
use Hyperwallet\Model\WebhookNotification;

class WebhookNotificationTest extends ModelTestCase {

protected function getModelName() {
return 'WebhookNotification';
}

/**
* @dataProvider ignoredPropertiesProvider
*
* @param string $property The property to look for
*/
public function testGettersForIgnoredProperties($property) {
$this->performGettersForIgnoredPropertiesTest($property);
}

/**
* @dataProvider propertiesProvider
*
* @param string $property The property to look for
*/
public function testGetterReturnValueIsSet($property) {
$this->performGetterReturnValueIsSetTest($property);
}

/**
* @dataProvider propertiesProvider
*
* @param string $property The property to look for
*/
public function testGetterReturnValueIsNotSet($property) {
$this->performGetterReturnValueIsNotSetTest($property);
}

/**
* @dataProvider notificationTypeProvider
*
* @param string $type The notification type
* @param object $clazz The expected class type
*
*/
public function testConstructorObjectConversion($type, $clazz) {
$data = array(
'type' => $type,
'test2' => 'value2',
'object' => array(
'test' => 'value'
)
);

$notification = new WebhookNotification($data);
if ($clazz === null) {
$this->assertNull($notification->getObject());
} else {
$this->assertNotNull($notification->getObject());
$this->assertInstanceOf($clazz, $notification->getObject());

$this->assertEquals(array(
'test' => 'value'
), $notification->getObject()->getProperties());
}
}

public function notificationTypeProvider() {
return array(
'USERS.CREATED' => array('USERS.CREATED', User::class),
'USERS.UPDATED.STATUS.ACTIVATED' => array('USERS.UPDATED.STATUS.ACTIVATED', User::class),
'USERS.UPDATED.STATUS.LOCKED' => array('USERS.UPDATED.STATUS.LOCKED', User::class),
'USERS.UPDATED.STATUS.FROZEN' => array('USERS.UPDATED.STATUS.FROZEN', User::class),
'USERS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.UPDATED.STATUS.DE_ACTIVATED', User::class),

'USERS.BANK_ACCOUNTS.CREATED' => array('USERS.BANK_ACCOUNTS.CREATED', BankAccount::class),
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.ACTIVATED' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.ACTIVATED', BankAccount::class),
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.INVALID' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.INVALID', BankAccount::class),
'USERS.BANK_ACCOUNTS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.BANK_ACCOUNTS.UPDATED.STATUS.DE_ACTIVATED', BankAccount::class),

'USERS.PREPAID_CARDS.CREATED' => array('USERS.PREPAID_CARDS.CREATED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.QUEUED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.QUEUED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.PRE_ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.PRE_ACTIVATED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.ACTIVATED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.DECLINED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.DECLINED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.LOCKED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.LOCKED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.SUSPENDED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.SUSPENDED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.LOST_OR_STOLEN' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.LOST_OR_STOLEN', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.DE_ACTIVATED' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.DE_ACTIVATED', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.COMPLIANCE_HOLD' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.COMPLIANCE_HOLD', PrepaidCard::class),
'USERS.PREPAID_CARDS.UPDATED.STATUS.KYC_HOLD' => array('USERS.PREPAID_CARDS.UPDATED.STATUS.KYC_HOLD', PrepaidCard::class),

'PAYMENTS.CREATED' => array('PAYMENTS.CREATED', Payment::class),

'TEST' => array('TEST', null),
);
}

}