Skip to content

Commit c02e63f

Browse files
Merge pull request #58 from ostdotcom/webhooks
Merge branch webhooks to develop
2 parents e666179 + c636738 commit c02e63f

8 files changed

Lines changed: 357 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
[OST JAVA SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-java/tree/v2.1.0)
1+
[OST PHP SDK v2.2.0](https://github.com/ostdotcom/ost-sdk-php/tree/v2.2.0)
2+
---
3+
4+
* Added webhooks module to call webhook management OST APIs.
5+
* Support for verify webhook signature.
6+
7+
[OST PHP SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-php/tree/v2.1.0)
28
---
39

410
* Added base tokens module to V2 API's

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,4 +471,84 @@ Get Base Tokens Detail:
471471
$getParams = array();
472472
$response = $baseTokensService->get($getParams)->wait();
473473
echo json_encode($response, JSON_PRETTY_PRINT);
474+
```
475+
476+
### Webhooks Module
477+
478+
To manage webhooks on the OST Platform Interface, use services provided by the Webhooks module. You can
479+
use this service to create new webhooks and manage existing webhooks.
480+
481+
```php
482+
$webhooksService = $ostObj->services->webhooks;
483+
```
484+
485+
Create Webhook:
486+
487+
```php
488+
$createParams = array();
489+
$createParams['topics'] = array("transactions/initiate", "transactions/success");
490+
$createParams['url'] = "https://testingWebhooks.com";
491+
// $createParams['status'] = "inactive";
492+
$response = $webhooksService->create($createParams)->wait();
493+
echo json_encode($response, JSON_PRETTY_PRINT);
494+
```
495+
496+
Update webhook:
497+
498+
```php
499+
$updateParams = array();
500+
$updateParams['webhook_id'] = "04ebb6be-8673-4999-8878-95ad047ddd73";
501+
$updateParams['topics'] = array("transactions/initiate", "transactions/success", "transactions/failure");
502+
$updateParams['status'] = "inactive";
503+
$response = $webhooksService->update($updateParams)->wait();
504+
echo json_encode($response, JSON_PRETTY_PRINT);
505+
```
506+
507+
Get webhook:
508+
509+
```php
510+
$getParams = array();
511+
$getParams['webhook_id'] = "04ebb6be-8673-4999-8878-95ad047ddd73";
512+
$response = $webhooksService->get($getParams)->wait();
513+
echo json_encode($response, JSON_PRETTY_PRINT);
514+
```
515+
516+
Get webhook List:
517+
518+
```php
519+
$getParams = array();
520+
// $getParams['limit'] = 1;
521+
// $getParams['pagination_identifier'] = "eyJwYWdlIjoyLCJsaW1pdCI6MX0=";
522+
$response = $webhooksService->getList($getParams)->wait();
523+
echo json_encode($response, JSON_PRETTY_PRINT);
524+
```
525+
526+
Delete webhook:
527+
528+
```php
529+
$deleteParams = array();
530+
$deleteParams['webhook_id'] = "415510fa-d57c-4c66-8c90-fd29ab7468b9";
531+
$response = $webhooksService->delete($deleteParams)->wait();
532+
echo json_encode($response, JSON_PRETTY_PRINT);
533+
```
534+
535+
Verify webhook request signature:
536+
537+
```php
538+
$params = array();
539+
$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"}]}}}
540+
$params["stringified_data"] = json_encode($webhookEventData);
541+
542+
// Get webhoook version from webhook events data.
543+
$params["version"] = "v2";
544+
545+
// Get ost-timestamp from the response received in event.
546+
$params["request_timestamp"] = "1559902637";
547+
548+
// Get signature from the response received in event.
549+
$params["signature"] = "2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824";
550+
551+
$params["webhook_secret"] = "mySecret";
552+
$response = $webhooksService->verifySignature($params);
553+
echo json_encode($response, JSON_PRETTY_PRINT);
474554
```

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

src/Lib/Request.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(array $params)
5858
}
5959

6060
/**
61-
* Send a GET request
61+
* Send a GET request.
6262
*
6363
* @param string $endpoint endpoint to the GET request
6464
* @param array $arguments optional object containing params which are to be sent across in the GET request
@@ -96,10 +96,10 @@ function ($reason) {
9696
}
9797

9898
/**
99-
* Send a POST request
99+
* Send a POST request.
100100
*
101-
* @param string $endpoint endpoint to the GET request
102-
* @param array $arguments optional object containing params which are to be sent across in the GET request
101+
* @param string $endpoint endpoint to the POST request
102+
* @param array $arguments optional object containing params which are to be sent across in the POST request
103103
*
104104
* @return object
105105
*
@@ -134,6 +134,43 @@ function ($reason) {
134134
);
135135
}
136136

137+
/**
138+
* Send a DELETE request.
139+
*
140+
* @param string $endpoint endpoint to the DELETE request
141+
* @param array $arguments optional object containing params which are to be sent across in the DELETE request
142+
*
143+
* @return object
144+
*
145+
*/
146+
public function delete($endpoint, array $arguments = array())
147+
{
148+
$argsCopy = $this->copyAndSanitizeArgs($arguments);
149+
150+
// build Path to hit by appending query params and api_signature
151+
$stringToSign = $endpoint . '?' . $argsCopy;
152+
153+
$urlPath = $stringToSign . '&api_signature=' . $this->getSignature($stringToSign);
154+
155+
/** @var Promise $promise */
156+
$promise = $this->getRequestClient()->deleteAsync(substr($urlPath, 1), $this->getCommonRequestParams());
157+
158+
return $promise->then(
159+
// $onFulfilled
160+
function ($response) {
161+
return $this->parseResponse($response);
162+
},
163+
// $onRejected
164+
function ($reason) {
165+
if (get_class($reason) == "GuzzleHttp\Exception\ConnectException") {
166+
return $this->customGenericErrorResponse('connect_exception');
167+
} else {
168+
return $this->customGenericErrorResponse('g_1');
169+
}
170+
}
171+
);
172+
}
173+
137174
/**
138175
* Get Signature
139176
*
@@ -170,7 +207,7 @@ private function sanitizeApiBaseUrl($apiBaseUrl)
170207
}
171208

172209
/**
173-
* Parse response of GET / POST requests
210+
* Parse response of GET / POST / DELETE requests.
174211
*
175212
* @param object $response response obj of HTTP request
176213
*
@@ -268,7 +305,7 @@ private function copyAndSanitizeArgs(array $arguments)
268305

269306
}
270307

271-
/**
308+
/**
272309
* gives nested query string
273310
*
274311
* @return string
@@ -344,7 +381,7 @@ private function getRequestClient()
344381
}
345382

346383
/**
347-
* returns common params for GET & POST requests
384+
* returns common params for GET, POST & DELETE requests
348385
*
349386
* @return array
350387
*

src/Services/Base.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,23 @@ protected function getRecoveryOwnerAddress(array $params)
143143
return $this->getValueForKey($params, "recovery_owner_address");
144144
}
145145

146+
/**
147+
* getWebhookId from params array
148+
*
149+
* @param array $params request object which would fire API calls
150+
*
151+
* @return string
152+
*/
153+
protected function getWebhookId(array $params)
154+
{
155+
return $this->getValueForKey($params, "webhook_id");
156+
}
157+
146158
/**
147159
* Get Value for Given Key
148160
*
149161
* @param array $params request object which would fire API calls
150-
*x
162+
*
151163
* @throws InvalidArgumentException, BadMethodCallException
152164
*
153165
* @return string

src/Services/Manifest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class Manifest
5050
/** @var BaseTokens object which has methods to fire API's belonging to Base Tokens module */
5151
public $baseTokens;
5252

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

5457
/**
5558
* Constructor
@@ -92,6 +95,8 @@ public function __construct($params)
9295

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

98+
$this->webhooks = new Webhooks($requestObj);
99+
95100
}
96101

97102
}

src/Services/Webhooks.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Webhooks class
4+
*/
5+
6+
namespace OST;
7+
8+
use OST\Base;
9+
10+
/**
11+
* Class encapsulating methods to interact with API's for Webhook module
12+
*/
13+
class Webhooks extends Base
14+
{
15+
16+
const PREFIX = '/webhooks';
17+
18+
/**
19+
* Create a new webhook.
20+
*
21+
* @param array $params params for creating a new webhook
22+
*
23+
* @return object
24+
*
25+
*/
26+
public function create(array $params = array())
27+
{
28+
return $this->requestObj->post($this->getPrefix() . '/', $params);
29+
}
30+
31+
/**
32+
* Update a webhook.
33+
*
34+
* @param array $params params for updating a webhook
35+
*
36+
* @return object
37+
*
38+
*/
39+
public function update(array $params = array())
40+
{
41+
return $this->requestObj->post($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
42+
}
43+
44+
/**
45+
* Get a webhook.
46+
*
47+
* @param array $params params for fetching webhook
48+
*
49+
* @return object
50+
*
51+
*/
52+
public function get(array $params = array())
53+
{
54+
return $this->requestObj->get($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
55+
}
56+
57+
/**
58+
* List all webhooks.
59+
*
60+
* @param array $params params for fetching webhooks list
61+
*
62+
* @return object
63+
*
64+
*/
65+
public function getList(array $params = array())
66+
{
67+
return $this->requestObj->get($this->getPrefix() . '/', $params);
68+
}
69+
70+
71+
/**
72+
* Delete a webhook.
73+
*
74+
* @param array $params params for deleting a webhook.
75+
*
76+
* @return object
77+
*
78+
*/
79+
public function delete(array $params = array())
80+
{
81+
return $this->requestObj->delete($this->getPrefix() . '/' . $this->getWebhookId($params) . '/', $params);
82+
}
83+
84+
/**
85+
* Verify webhook request signature.
86+
*
87+
* @param array $params params for verifying a webhook signature.
88+
*
89+
* @return object
90+
*
91+
*/
92+
public function verifySignature(array $params = array())
93+
{
94+
$version = $params["version"];
95+
$webhookSecret = $params["webhook_secret"];
96+
$stringifiedData = $params["stringified_data"];
97+
$requestTimestamp = $params["request_timestamp"];
98+
$signature = $params["signature"];
99+
100+
$signatureParams = $requestTimestamp.".".$version.".".$stringifiedData;
101+
$signatureToBeVerified = hash_hmac('sha256', $signatureParams, $webhookSecret);
102+
103+
if(strcmp($signatureToBeVerified, $signature) == 0)
104+
{
105+
return true;
106+
} else {
107+
return false;
108+
}
109+
}
110+
}
111+
112+
113+
114+
115+
116+
117+
118+

0 commit comments

Comments
 (0)