Skip to content

Commit

Permalink
Wrapping webhooks functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
iodine-http committed Feb 19, 2021
1 parent fb01d59 commit b489436
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Service/Webhooks.php
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);
}
}
173 changes: 173 additions & 0 deletions tests/Service/WebhooksTest.php
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);
}
}
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":[]}
1 change: 1 addition & 0 deletions tests/data-fixtures/webhooks/register-webhooks-fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":null,"status":"400 Bad Request","error_message":["bad input data"]}
1 change: 1 addition & 0 deletions tests/data-fixtures/webhooks/register-webhooks-ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":"success","status":"200 Ok","error_message":[]}
1 change: 1 addition & 0 deletions tests/data-fixtures/webhooks/trigger-webhook-fail.json
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}
1 change: 1 addition & 0 deletions tests/data-fixtures/webhooks/trigger-webhook-ok.json
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"}

0 comments on commit b489436

Please sign in to comment.