Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Adding support for send documents
Browse files Browse the repository at this point in the history
  • Loading branch information
frangeris committed Dec 3, 2015
1 parent 9dc71c8 commit c208b23
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions classes/RightSignature.php
Expand Up @@ -108,6 +108,19 @@ public function documents()
// return Document::list($this->_client);
}

/**
* This method is for sending a once-off document that has not been setup as a Template.
*
* @param string $path Path of the document to send
* @param array $args Arguments to use as payload
*
* @return array
*/
public function sendDocument($path, $args)
{
return Document::send($this->_client, $path, $args);
}

// ----------------------------------------
// Signer Links

Expand Down
42 changes: 42 additions & 0 deletions classes/RightSignature/Document.php
Expand Up @@ -45,4 +45,46 @@ public static function documentDetails($client, $documentGuid)

return new self($array);
}

/**
* This method is for sending a once-off document that has not been setup as a Template.
*
* @param string $path Absolute path of the document
* @param array $payload Payload of the request
*
* @return array XML response parsed to array
*
* @throws Exception Missing required key
*/
public static function send($client, $path, $payload)
{
// validation of required arguments
foreach (['action', 'type', 'recipients'] as $argument) {
ArrayHelpers::ensureIsSet($payload, $argument);
}

$info = pathinfo($path);
$payload['document_data'] = [
'type' => $payload['type'],
'value' => $path,
];

// change the underlying document (url, base64)
if ('base64' == $payload['type']) {
$payload['document_data']['filename'] = $info['basename'];

// get the resource content
$resource = fopen($path, 'r');
$content = fread($resource, filesize($path));
fclose($resource);

$payload['document_data']['value'] = base64_encode($content);
}

// make the request
$payload = XmlHelpers::toXml(['document' => $payload]);
$response = $client->post('/api/documents.xml', $payload);

return XmlHelpers::toArray($response);
}
}
44 changes: 44 additions & 0 deletions tests/RightSignature/DocumentTest.php
Expand Up @@ -109,4 +109,48 @@ public function testDocumentDetails()
$this->assertEquals('support@rightsignature.com', $document->recipients[0]->email);
$this->assertEquals('Document emailed to John Bellingham (john.b@rightsignature.com)', $document->audit_trail[0]->message);
}

public function testSendDocument()
{
$response = <<<EOS
<?xml version="1.0" encoding="UTF-8"?>
<document>
<status>sent</status>
<guid>2VMW88J3424MPEYF9DU6VY</guid>
</document>
EOS;

$payload = [
'subject' => '- email subject -',
'action' => 'send',
'type' => 'base64',
'recipients' => [
'recipient' => [
[
'is_sender' => true,
'role' => 'cc',
],
[
'name' => 'Signer 1',
'email' => 'pjafwcyv@sharklasers.com',
'role' => 'signer',
],
],
],
];

$client = \Mockery::mock('client');
$client->shouldReceive('post')
->withAnyArgs()
->andReturn($response);

$file = fopen('sample-file.pdf', 'w');
fwrite($file, '- test document to sign -');
fclose($file);

$document = Document::send($client, 'sample-file.pdf', $payload);
@unlink('sample-file.pdf');

$this->assertEquals('sent', $document['status']);
}
}

0 comments on commit c208b23

Please sign in to comment.