Skip to content

Commit

Permalink
Merge pull request #58 from ostdotcom/webhooks
Browse files Browse the repository at this point in the history
Merge branch webhooks to develop
  • Loading branch information
kedarchandrayan authored Jun 18, 2019
2 parents e666179 + c636738 commit c02e63f
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[OST JAVA SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-java/tree/v2.1.0)
[OST PHP SDK v2.2.0](https://github.com/ostdotcom/ost-sdk-php/tree/v2.2.0)
---

* Added webhooks module to call webhook management OST APIs.
* Support for verify webhook signature.

[OST PHP SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-php/tree/v2.1.0)
---

* Added base tokens module to V2 API's
Expand Down
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,84 @@ Get Base Tokens Detail:
$getParams = array();
$response = $baseTokensService->get($getParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Webhooks Module

To manage webhooks on the OST Platform Interface, use services provided by the Webhooks module. You can
use this service to create new webhooks and manage existing webhooks.

```php
$webhooksService = $ostObj->services->webhooks;
```

Create Webhook:

```php
$createParams = array();
$createParams['topics'] = array("transactions/initiate", "transactions/success");
$createParams['url'] = "https://testingWebhooks.com";
// $createParams['status'] = "inactive";
$response = $webhooksService->create($createParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

Update webhook:

```php
$updateParams = array();
$updateParams['webhook_id'] = "04ebb6be-8673-4999-8878-95ad047ddd73";
$updateParams['topics'] = array("transactions/initiate", "transactions/success", "transactions/failure");
$updateParams['status'] = "inactive";
$response = $webhooksService->update($updateParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

Get webhook:

```php
$getParams = array();
$getParams['webhook_id'] = "04ebb6be-8673-4999-8878-95ad047ddd73";
$response = $webhooksService->get($getParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

Get webhook List:

```php
$getParams = array();
// $getParams['limit'] = 1;
// $getParams['pagination_identifier'] = "eyJwYWdlIjoyLCJsaW1pdCI6MX0=";
$response = $webhooksService->getList($getParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

Delete webhook:

```php
$deleteParams = array();
$deleteParams['webhook_id'] = "415510fa-d57c-4c66-8c90-fd29ab7468b9";
$response = $webhooksService->delete($deleteParams)->wait();
echo json_encode($response, JSON_PRETTY_PRINT);
```

Verify webhook request signature:

```php
$params = array();
$webhookEventData = {"id":"54e3cd1c-afd7-4dcf-9c78-137c56a53582","topic":"transactions/success","created_at":1560838772,"webhook_id":"0823a4ea-5d87-44cf-8ca8-1e5a31bf8e46","version":"v2","data":{"result_type":"transaction","transaction":{"id":"ddebe817-b94f-4b51-9227-f543fae4715a","transaction_hash":"0x7ee737db22b58dc4da3f4ea4830ca709b388d84f31e77106cb79ee09fc6448f9","from":"0x69a581096dbddf6d1e0fff7ebc1254bb7a2647c6","to":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","nonce":3,"value":"0","gas_price":"1000000000","gas_used":120558,"transaction_fee":"120558000000000","block_confirmation":24,"status":"SUCCESS","updated_timestamp":1560838699,"block_timestamp":1560838698,"block_number":1554246,"rule_name":"Pricer","meta_property":{},"transfers":[{"from":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","from_user_id":"acfdea7d-278e-4ffc-aacb-4a21398a280c","to":"0x0a754aaab96d634337aac6556312de396a0ca46a","to_user_id":"7bc8e0bd-6761-4604-8f8e-e33f86f81309","amount":"112325386","kind":"transfer"}]}}}
$params["stringified_data"] = json_encode($webhookEventData);

// Get webhoook version from webhook events data.
$params["version"] = "v2";

// Get ost-timestamp from the response received in event.
$params["request_timestamp"] = "1559902637";

// Get signature from the response received in event.
$params["signature"] = "2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824";

$params["webhook_secret"] = "mySecret";
$response = $webhooksService->verifySignature($params);
echo json_encode($response, JSON_PRETTY_PRINT);
```
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.2.0
51 changes: 44 additions & 7 deletions src/Lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(array $params)
}

/**
* Send a GET request
* Send a GET request.
*
* @param string $endpoint endpoint to the GET request
* @param array $arguments optional object containing params which are to be sent across in the GET request
Expand Down Expand Up @@ -96,10 +96,10 @@ function ($reason) {
}

/**
* Send a POST request
* Send a POST request.
*
* @param string $endpoint endpoint to the GET request
* @param array $arguments optional object containing params which are to be sent across in the GET request
* @param string $endpoint endpoint to the POST request
* @param array $arguments optional object containing params which are to be sent across in the POST request
*
* @return object
*
Expand Down Expand Up @@ -134,6 +134,43 @@ function ($reason) {
);
}

/**
* Send a DELETE request.
*
* @param string $endpoint endpoint to the DELETE request
* @param array $arguments optional object containing params which are to be sent across in the DELETE request
*
* @return object
*
*/
public function delete($endpoint, array $arguments = array())
{
$argsCopy = $this->copyAndSanitizeArgs($arguments);

// build Path to hit by appending query params and api_signature
$stringToSign = $endpoint . '?' . $argsCopy;

$urlPath = $stringToSign . '&api_signature=' . $this->getSignature($stringToSign);

/** @var Promise $promise */
$promise = $this->getRequestClient()->deleteAsync(substr($urlPath, 1), $this->getCommonRequestParams());

return $promise->then(
// $onFulfilled
function ($response) {
return $this->parseResponse($response);
},
// $onRejected
function ($reason) {
if (get_class($reason) == "GuzzleHttp\Exception\ConnectException") {
return $this->customGenericErrorResponse('connect_exception');
} else {
return $this->customGenericErrorResponse('g_1');
}
}
);
}

/**
* Get Signature
*
Expand Down Expand Up @@ -170,7 +207,7 @@ private function sanitizeApiBaseUrl($apiBaseUrl)
}

/**
* Parse response of GET / POST requests
* Parse response of GET / POST / DELETE requests.
*
* @param object $response response obj of HTTP request
*
Expand Down Expand Up @@ -268,7 +305,7 @@ private function copyAndSanitizeArgs(array $arguments)

}

/**
/**
* gives nested query string
*
* @return string
Expand Down Expand Up @@ -344,7 +381,7 @@ private function getRequestClient()
}

/**
* returns common params for GET & POST requests
* returns common params for GET, POST & DELETE requests
*
* @return array
*
Expand Down
14 changes: 13 additions & 1 deletion src/Services/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,23 @@ protected function getRecoveryOwnerAddress(array $params)
return $this->getValueForKey($params, "recovery_owner_address");
}

/**
* getWebhookId from params array
*
* @param array $params request object which would fire API calls
*
* @return string
*/
protected function getWebhookId(array $params)
{
return $this->getValueForKey($params, "webhook_id");
}

/**
* Get Value for Given Key
*
* @param array $params request object which would fire API calls
*x
*
* @throws InvalidArgumentException, BadMethodCallException
*
* @return string
Expand Down
5 changes: 5 additions & 0 deletions src/Services/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Manifest
/** @var BaseTokens object which has methods to fire API's belonging to Base Tokens module */
public $baseTokens;

/** @var Webhooks object which has methods to fire API's belonging to Webhook module */
public $webhooks;


/**
* Constructor
Expand Down Expand Up @@ -92,6 +95,8 @@ public function __construct($params)

$this->baseTokens = new BaseTokens($requestObj);

$this->webhooks = new Webhooks($requestObj);

}

}
118 changes: 118 additions & 0 deletions src/Services/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Webhooks class
*/

namespace OST;

use OST\Base;

/**
* Class encapsulating methods to interact with API's for Webhook module
*/
class Webhooks extends Base
{

const PREFIX = '/webhooks';

/**
* Create a new webhook.
*
* @param array $params params for creating a new webhook
*
* @return object
*
*/
public function create(array $params = array())
{
return $this->requestObj->post($this->getPrefix() . '/', $params);
}

/**
* Update a webhook.
*
* @param array $params params for updating a webhook
*
* @return object
*
*/
public function update(array $params = array())
{
return $this->requestObj->post($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
}

/**
* Get a webhook.
*
* @param array $params params for fetching webhook
*
* @return object
*
*/
public function get(array $params = array())
{
return $this->requestObj->get($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
}

/**
* List all webhooks.
*
* @param array $params params for fetching webhooks list
*
* @return object
*
*/
public function getList(array $params = array())
{
return $this->requestObj->get($this->getPrefix() . '/', $params);
}


/**
* Delete a webhook.
*
* @param array $params params for deleting a webhook.
*
* @return object
*
*/
public function delete(array $params = array())
{
return $this->requestObj->delete($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
}

/**
* Verify webhook request signature.
*
* @param array $params params for verifying a webhook signature.
*
* @return object
*
*/
public function verifySignature(array $params = array())
{
$version = $params["version"];
$webhookSecret = $params["webhook_secret"];
$stringifiedData = $params["stringified_data"];
$requestTimestamp = $params["request_timestamp"];
$signature = $params["signature"];

$signatureParams = $requestTimestamp.".".$version.".".$stringifiedData;
$signatureToBeVerified = hash_hmac('sha256', $signatureParams, $webhookSecret);

if(strcmp($signatureToBeVerified, $signature) == 0)
{
return true;
} else {
return false;
}
}
}








Loading

0 comments on commit c02e63f

Please sign in to comment.