-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb01d59
commit b489436
Showing
7 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gandung\Tokopedia\Service; | ||
|
||
/** | ||
* @author Paulus Gandung Prakosa <[email protected]> | ||
*/ | ||
class Webhooks extends Resource | ||
{ | ||
/** | ||
* List all registered webhooks. | ||
* | ||
* @return string | ||
*/ | ||
public function listRegisteredWebhooks() | ||
{ | ||
$endpoint = sprintf( | ||
'/v1/fs/%d', | ||
$this->getFulfillmentServiceID() | ||
); | ||
$response = $this->call('GET', $endpoint); | ||
|
||
return $this->getContents($response); | ||
} | ||
|
||
/** | ||
* Register list of url webhooks. | ||
* | ||
* @param array $data | ||
* @return string | ||
*/ | ||
public function registerWebhooks(array $data) | ||
{ | ||
$endpoint = sprintf( | ||
'/v1/fs/%d/register', | ||
$this->getFulfillmentServiceID() | ||
); | ||
$response = $this->call('POST', $endpoint, $data); | ||
|
||
return $this->getContents($response); | ||
} | ||
|
||
/** | ||
* Manually trigger webhook. | ||
* | ||
* @param array $data | ||
* @return string | ||
*/ | ||
public function triggerWebhook(array $data) | ||
{ | ||
$endpoint = sprintf( | ||
'/v1/fs/%d/trigger', | ||
$this->getFulfillmentServiceID() | ||
); | ||
$response = $this->call('POST', $endpoint, $data); | ||
|
||
return $this->getContents($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gandung\Tokopedia\Tests\Service; | ||
|
||
use Gandung\Tokopedia\Service\Webhooks; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\Psr7\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function file_get_contents; | ||
use function json_decode; | ||
|
||
/** | ||
* @author Paulus Gandung Prakosa <[email protected]> | ||
*/ | ||
class WebhooksTest extends TestCase | ||
{ | ||
use ServiceTestTrait; | ||
|
||
public function testCanGetRegisteredWebhooksWithSuccessResponse() | ||
{ | ||
$webhooks = new Webhooks($this->getAuthorization()); | ||
$mockHandler = new MockHandler(); | ||
$httpClient = new Client(['handler' => $mockHandler]); | ||
$contents = file_get_contents( | ||
__DIR__ . '/../data-fixtures/webhooks/list-registered-webhooks-ok.json' | ||
); | ||
|
||
$mockHandler->append(new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
$contents | ||
)); | ||
|
||
$webhooks->setHttpClient($httpClient); | ||
|
||
$deserialized = json_decode($contents, true); | ||
$response = json_decode($webhooks->listRegisteredWebhooks(), true); | ||
|
||
$this->assertEquals($deserialized, $response); | ||
} | ||
|
||
public function testCanRegisterWebhooksWithSuccessResponse() | ||
{ | ||
$webhooks = new Webhooks($this->getAuthorization()); | ||
$mockHandler = new MockHandler(); | ||
$httpClient = new Client(['handler' => $mockHandler]); | ||
$contents = file_get_contents( | ||
__DIR__ . '/../data-fixtures/webhooks/register-webhooks-ok.json' | ||
); | ||
|
||
$mockHandler->append(new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
$contents | ||
)); | ||
|
||
$webhooks->setHttpClient($httpClient); | ||
|
||
$response = $webhooks->registerWebhooks([ | ||
'fs_id' => 31337, | ||
'order_notification_url' => 'http://example.com/api/v1/order/notification', | ||
'order_cancellation_url' => 'http://example.com/api/v1/order/cancellation', | ||
'order_status_url' => 'http://example.com/api/v1/order/status', | ||
'order_request_cancellation_url' => 'http://example.com/api/v1/order/req/cancel', | ||
'chat_notification_url' => 'http://example.com/api/v1/chat/notification', | ||
'product_creation_url' => 'http://example.com/api/v1/product/create', | ||
'webhook_secret' => uniqid() | ||
]); | ||
|
||
$deserialized = json_decode($contents, true); | ||
$response = json_decode($response, true); | ||
|
||
$this->assertEquals($deserialized, $response); | ||
} | ||
|
||
public function testCanRegisterWebhooksWithFailedResponse() | ||
{ | ||
$webhooks = new Webhooks($this->getAuthorization()); | ||
$mockHandler = new MockHandler(); | ||
$httpClient = new Client(['handler' => $mockHandler]); | ||
$contents = file_get_contents( | ||
__DIR__ . '/../data-fixtures/webhooks/register-webhooks-fail.json' | ||
); | ||
|
||
$mockHandler->append(new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
$contents | ||
)); | ||
|
||
$webhooks->setHttpClient($httpClient); | ||
|
||
$response = $webhooks->registerWebhooks([ | ||
'fs_id' => 31337, | ||
'order_notification_url' => 'http://example.com/api/v1/order/notification', | ||
'order_cancellation_url' => 'http://example.com/api/v1/order/cancellation', | ||
'order_status_url' => 'http://example.com/api/v1/order/status', | ||
'order_request_cancellation_url' => 'http://example.com/api/v1/order/req/cancel', | ||
'chat_notification_url' => 'http://example.com/api/v1/chat/notification', | ||
'product_creation_url' => 'http://example.com/api/v1/product/create', | ||
'webhook_secret' => uniqid() | ||
]); | ||
|
||
$deserialized = json_decode($contents, true); | ||
$response = json_decode($response, true); | ||
|
||
$this->assertEquals($deserialized, $response); | ||
} | ||
|
||
public function testCanTriggerWebhookManuallyWithSuccessResponse() | ||
{ | ||
$webhooks = new Webhooks($this->getAuthorization()); | ||
$mockHandler = new MockHandler(); | ||
$httpClient = new Client(['handler' => $mockHandler]); | ||
$contents = file_get_contents( | ||
__DIR__ . '/../data-fixtures/webhooks/trigger-webhook-ok.json' | ||
); | ||
|
||
$mockHandler->append(new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
$contents | ||
)); | ||
|
||
$webhooks->setHttpClient($httpClient); | ||
|
||
$response = $webhooks->triggerWebhook([ | ||
'type' => 'order_notification', | ||
'order_id' => 576694264, | ||
'url' => 'http://example.com/api/v1/order/notification', | ||
'is_encrypted' => true | ||
]); | ||
|
||
$deserialized = json_decode($contents, true); | ||
$response = json_decode($response, true); | ||
|
||
$this->assertEquals($deserialized, $response); | ||
} | ||
|
||
public function testCanTriggerWebhookManuallyWithFailedResponse() | ||
{ | ||
$webhooks = new Webhooks($this->getAuthorization()); | ||
$mockHandler = new MockHandler(); | ||
$httpClient = new Client(['handler' => $mockHandler]); | ||
$contents = file_get_contents( | ||
__DIR__ . '/../data-fixtures/webhooks/trigger-webhook-fail.json' | ||
); | ||
|
||
$mockHandler->append(new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
$contents | ||
)); | ||
|
||
$webhooks->setHttpClient($httpClient); | ||
|
||
$response = $webhooks->triggerWebhook([ | ||
'type' => 'order_notification', | ||
'order_id' => 576694264, | ||
'url' => 'http://example.com/api/v1/order/notification', | ||
'is_encrypted' => true | ||
]); | ||
|
||
$deserialized = json_decode($contents, true); | ||
$response = json_decode($response, true); | ||
|
||
$this->assertEquals($deserialized, $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data":{"fs_id":13004,"order_notification_url":"http://yourstore.com/v1/order/notification","order_cancellation_url":"http://yourstore.com/v1/order/cancellation","order_status_url":"http://yourstore.com/v1/order/status","order_request_cancellation_url":"http://yourstore.com/v1/order/request_cancel","chat_notification_url":"http://yourstore.com/v1/chat/notification","webhook_secret":"webhook_secret"},"status":"200 Ok","error_message":[]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data":null,"status":"400 Bad Request","error_message":["bad input data"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data":"success","status":"200 Ok","error_message":[]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"header":{"process_time":0.007438284,"messages":"Our server encounters an error, please try again later","reason":"There Are Error From Ext Service","error_code":"ORD_API_006"},"data":null} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"header":{"process_time":0.042723386,"messages":"Your request has been processed successfully"},"data":"success"} |