Skip to content

Commit

Permalink
Merge pull request #16 from veryfi/feature/PLAT-5158-Add-Tags
Browse files Browse the repository at this point in the history
Add tags
  • Loading branch information
Kaevan89 authored Jun 9, 2023
2 parents d06971e + 869b47f commit 9068e01
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": ">=7.4",
"ext-curl": "*",
"ext-json": "*"
"ext-json": "*",
"ext-mbstring": "*"
},

"require-dev": {
Expand Down
81 changes: 77 additions & 4 deletions src/veryfi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Client
*/
public string $base_url;
/**
* Api version to use Veryfi by default 'v7'
* Api version to use Veryfi by default 'v8'
*
* @var string
*/
Expand Down Expand Up @@ -167,7 +167,8 @@ private function generate_signature(array $payload_params,
$payload = "$payload,$key:$value";
}
$temporary_signature = hash_hmac('sha256', $payload, $this->client_secret, true);
return trim(utf8_decode(base64_encode($temporary_signature)));
$base64_signature = base64_encode($temporary_signature);
return trim(mb_convert_encoding($base64_signature, 'ISO-8859-1'));
}

/**
Expand All @@ -180,9 +181,13 @@ private function generate_signature(array $payload_params,
*/
private function request(string $http_verb,
string $endpoint_name,
array $request_arguments): string
array $request_arguments,
bool $force_v7 = false): string
{
$api_url = "$this->extend_url$endpoint_name";
if ($force_v7) {
$api_url = str_replace("v8","v7", $api_url);
}
$time_stamp = (string) (time() * 1000);
$signature = $this->generate_signature($request_arguments, $time_stamp);
$this->headers['X-Veryfi-Request-Timestamp'] = $time_stamp;
Expand Down Expand Up @@ -443,7 +448,75 @@ public static function verify_signature(array $payload_params,
}
$payload = "{{$payload}}";
$temporary_signature = hash_hmac('SHA256', $payload, $client_secret, true);
$signature = trim(utf8_decode(base64_encode($temporary_signature)));
$signature = trim(mb_convert_encoding(base64_encode($temporary_signature), 'ISO-8859-1'));
return $signature == $client_signature;
}

/**
* Add a new tag on an existing document
*
* @param int $document_id ID of the document you'd like to add a Tag
* @param string $tag line item object to add
* @return string Added tag data
*/
public function add_tag(int $document_id,
string $tag): string
{
$endpoint_name = "/documents/$document_id/tags/";
$request_arguments = array('name' => $tag);
return $this->request('PUT', $endpoint_name, $request_arguments);
}

/**
* Unlink all tags assigned to a specific document.
*
* @param int $document_id ID of the document you'd like to delete their tags
* @return string A JSON response.
*/
public function delete_tags(int $document_id): string
{
$endpoint_name = "/documents/$document_id/tags/";
$request_arguments = array();
return $this->request('DELETE', $endpoint_name, $request_arguments);
}

/**
* Get list of tags.
*
* @return string A JSON with list of tags.
*/
public function get_tags(): string
{
$endpoint_name = '/tags/';
$request_arguments = array();
return $this->request('GET', $endpoint_name, $request_arguments, true);
}

/**
* Retrieve list of tags by document ID.
*
* @param int $document_id ID of the document you'd like to retrieve tags.
* @return string A JSON with list of tags from the Document.
*/
public function get_document_tags(int $document_id): string
{
$endpoint_name = "/documents/$document_id/tags/";
$request_arguments = array('id' => $document_id);
return $this->request('GET', $endpoint_name, $request_arguments);
}

/**
* Unlink tag assigned to a specific document.
*
* @param int $document_id ID of the document you'd like to delete its tag
* @param int $tag_id ID of the tag you'd like to delete
* @return string A JSON response.
*/
public function delete_tag(int $document_id,
int $tag_id): string
{
$endpoint_name = "/documents/$document_id/tags/$tag_id/";
$request_arguments = array();
return $this->request('DELETE', $endpoint_name, $request_arguments);
}
}
148 changes: 131 additions & 17 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

final class ClientTest extends TestCase
{
private string $client_id = 'client_id';
private string $client_secret = 'client_secret';
private string $username = 'username';
private string $api_key = 'api_key';
private string $client_id = 'your_client_id';
private string $client_secret = 'your_client_secret';
private string $username = 'your_username';
private string $api_key = 'your_api_key';
private string $receipt_path = __DIR__ . '/resources/receipt.jpg';
private bool $mock_responses = true;

Expand All @@ -25,7 +25,7 @@ public function test_get_documents(): void

$file_path = __DIR__ . '/resources/getDocuments.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -48,7 +48,7 @@ public function test_get_document(): void

$file_path = __DIR__ . '/resources/getDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -73,7 +73,7 @@ public function test_process_document(): void

$file_path = __DIR__ .'/resources/processDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand Down Expand Up @@ -108,7 +108,7 @@ public function test_update_document(): void

$file_path = __DIR__ . '/resources/updateDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand Down Expand Up @@ -137,14 +137,14 @@ public function test_delete_document(): void

$file_path = __DIR__ . '/resources/deleteDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);

$file_path = __DIR__ .'/resources/processDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('process_document')
->willReturn($file_data);
Expand All @@ -170,7 +170,7 @@ public function test_process_document_url(): void

$file_path = __DIR__ . '/resources/processDocument.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -194,7 +194,7 @@ public function test_get_line_items(): void

$file_path = __DIR__ . '/resources/getLineItems.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -217,7 +217,7 @@ public function test_get_line_item(): void

$file_path = __DIR__ . '/resources/getLineItem.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -241,7 +241,7 @@ public function test_update_line_item(): void

$file_path = __DIR__ . '/resources/updateLineItem.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -265,7 +265,7 @@ public function test_add_line_item(): void

$file_path = __DIR__ . '/resources/addLineItem.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand Down Expand Up @@ -298,7 +298,7 @@ public function test_delete_line_item(): void

$file_path = __DIR__ . '/resources/deleteLineItem.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -320,7 +320,7 @@ public function test_delete_line_items(): void

$file_path = __DIR__ . '/resources/deleteLineItems.json';
$file = fopen($file_path, 'r');
$file_data = utf8_encode(fread($file, filesize($file_path)));
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
Expand All @@ -345,4 +345,118 @@ public function test_bad_credentials(): void
$json_response = json_decode($veryfi_client->get_documents(), true);
$this->assertEquals('fail', $json_response['status']);
}

public function test_add_tag(): void
{
$document_id = 140512586;
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/addTag.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->add_tag($document_id, 'test_tag'), true);
$this->assertEqualsIgnoringCase('test_tag', $json_response['name']);
if (!$this->mock_responses) {
$json_response = json_decode($veryfi_client->delete_tags($document_id), true);
$this->assertEmpty($json_response);
}
}

public function test_delete_tags(): void
{
$document_id = 140512586;
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/deleteTags.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->delete_tags($document_id), true);
$this->assertEmpty($json_response);
}

public function test_get_tags(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/getTags.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->get_tags(), true);
$this->assertNotEmpty($json_response);
}

public function test_get_document_tags(): void
{
$document_id = 140804210;
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/getTags.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->get_document_tags($document_id), true);
$this->assertNotEmpty($json_response);
}

public function test_delete_tag(): void
{
$document_id = 140512586;
$tag_id = 6673689;
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/deleteTags.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);
} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->delete_tag($document_id, $tag_id), true);
$this->assertEmpty($json_response);
}
}
4 changes: 4 additions & 0 deletions tests/resources/addTag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 6673474,
"name": "test_tag"
}
1 change: 1 addition & 0 deletions tests/resources/deleteTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 8 additions & 0 deletions tests/resources/getTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tags": [
{
"id": 6673474,
"name": "tag_123"
}
]
}

0 comments on commit 9068e01

Please sign in to comment.