diff --git a/samples/sign_management.php b/samples/sign_management.php index 2b39d07..349f83e 100644 --- a/samples/sign_management.php +++ b/samples/sign_management.php @@ -7,7 +7,7 @@ use Ilovepdf\SignatureManagement; $signatureRequest = new SignatureManagement("publickey", "secretkey"); -$signatureToken = "signaturetoken"; +$signatureToken = "token_requester"; $receiverToken = "receivertoken"; // Get a list of all created signature requests diff --git a/src/Ilovepdf.php b/src/Ilovepdf.php index 174ea3c..6c223cd 100644 --- a/src/Ilovepdf.php +++ b/src/Ilovepdf.php @@ -4,6 +4,7 @@ use Ilovepdf\Exceptions\DownloadException; use Ilovepdf\Exceptions\ProcessException; +use Ilovepdf\Exceptions\SignatureException; use Ilovepdf\Exceptions\StartException; use Ilovepdf\Exceptions\TaskException; use Ilovepdf\Exceptions\UploadException; @@ -45,7 +46,7 @@ class Ilovepdf */ public static $apiVersion = 'v1'; - const VERSION = 'php.1.2.3'; + const VERSION = 'php.1.2.5'; /** * @var string|null @@ -264,7 +265,7 @@ public function sendRequest(string $method, string $endpoint, array $params = [] } //signature exception if(strpos($endpoint, 'signature') !== false){ - throw new ProcessException($responseBody->error->type, $responseBody, $response->getStatusCode()); + throw new SignatureException($responseBody->error->type, $responseBody, $response->getStatusCode()); } if (isset($responseBody->error) && isset($responseBody->error->type)) { diff --git a/src/SignTask.php b/src/SignTask.php index d0859f2..744828c 100644 --- a/src/SignTask.php +++ b/src/SignTask.php @@ -337,7 +337,7 @@ public function execute() //$response = parent::sendRequest('post', 'signature', http_build_query($body, null, '&', PHP_QUERY_RFC3986)); $response = parent::sendRequest('post', 'signature', $body,false); try { - $this->result = json_decode($response->getBody()); + $this->result = json_decode($response->getBody()->getContents()); } catch(\Exception $e){ throw new ProcessException('Bad request'); diff --git a/src/SignatureManagement.php b/src/SignatureManagement.php index 930a1da..7b3d2d5 100644 --- a/src/SignatureManagement.php +++ b/src/SignatureManagement.php @@ -2,6 +2,7 @@ namespace Ilovepdf; +use Psr\Http\Message\ResponseInterface; use stdClass; use Ilovepdf\Exceptions\DownloadException; @@ -18,6 +19,7 @@ class SignatureManagement extends Ilovepdf * @param int $per_page * * @return array + * @throws \Exception */ public function getSignaturesList($page = 0, $per_page = 20): array { @@ -26,26 +28,26 @@ public function getSignaturesList($page = 0, $per_page = 20): array "per-page" => $per_page ]; $response = parent::sendRequest("get", "signature/list", $data); - $headers = $response->headers; - $return = [ - "signatures" => $response->body, + $headers = $response->getHeaders(); + return [ + "signatures" => $response->getBody()->getContents(), "pagination" => [ 'totalCount' => $headers && isset($headers['X-Pagination-Total-Count']) ? $headers['X-Pagination-Total-Count'] : '', - 'page' => $headers && isset($headers['X-Pagination-Current-Page']) ? $headers['X-Pagination-Current-Page'] - 1 : '', + 'page' => $headers && isset($headers['X-Pagination-Current-Page']) ? (int)$headers['X-Pagination-Current-Page'] - 1 : '', 'pageSize' => $headers && isset($headers['X-Pagination-Per-Page']) ? $headers['X-Pagination-Per-Page'] : '' ] ]; - return $return; } /** * @param string $signatureId * * @return stdClass + * @throws \Exception */ public function getSignatureStatus(string $signatureId): stdClass { - return parent::sendRequest("get", "signature/requesterview/{$signatureId}")->body; + return json_decode(parent::sendRequest("get", "signature/requesterview/{$signatureId}")->getBody()->getContents()); } /** @@ -95,10 +97,10 @@ public function downloadSignedFiles(string $signatureId, string $pathToSave, str */ public function fixReceiverEmail(string $receiverTokenRequester, string $newEmail): bool { - $body = [ + $body = ['form_params' => [ "email" => $newEmail - ]; - $response = parent::sendRequest("put", "signature/signer/fix-email/{$receiverTokenRequester}", $body, false, true); + ]]; + parent::sendRequest("put", "signature/signer/fix-email/{$receiverTokenRequester}", $body, false, true); //if above fails, it throws an exception return true; } @@ -111,9 +113,9 @@ public function fixReceiverEmail(string $receiverTokenRequester, string $newEmai */ public function fixSignerPhone(string $signerTokenRequester, string $newPhone): bool { - $body = [ + $body = ['form_params' => [ "phone" => $newPhone - ]; + ]]; $response = parent::sendRequest("put", "signature/signer/fix-phone/{$signerTokenRequester}", $body, false, true); //if above fails, it throws an exception return true; @@ -127,7 +129,7 @@ public function fixSignerPhone(string $signerTokenRequester, string $newPhone): public function sendReminders(string $signatureId): bool { $body = []; - $response = parent::sendRequest("post", "signature/sendReminder/{$signatureId}", $body, false, true); + parent::sendRequest("post", "signature/sendReminder/{$signatureId}", $body, false, true); //if above fails, it throws an exception return true; } @@ -136,11 +138,12 @@ public function sendReminders(string $signatureId): bool * @param string $signatureId * * @return bool + * @throws \Exception */ public function voidSignature(string $signatureId): bool { $body = []; - $response = parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true); + parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true); //if above fails, it throws an exception return true; } @@ -150,13 +153,14 @@ public function voidSignature(string $signatureId): bool * @param int $amountOfDays * * @return bool + * @throws \Exception */ public function increaseExpirationDays(string $signatureId, int $amountOfDays): bool { $body = [ "days" => $amountOfDays ]; - $response = parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true); + parent::sendRequest("put", "signature/void/{$signatureId}", $body, false, true); //if above fails, it throws an exception return true; } @@ -165,10 +169,11 @@ public function increaseExpirationDays(string $signatureId, int $amountOfDays): * @param string $receiverTokenRequester * * @return stdClass + * @throws \Exception */ public function getReceiverInfo(string $receiverTokenRequester): stdClass { - return parent::sendRequest("get", "signature/receiver/info/{$receiverTokenRequester}")->body; + return json_decode(parent::sendRequest("get", "signature/receiver/info/{$receiverTokenRequester}")->getBody()->getContents()); } /** @@ -189,15 +194,16 @@ private function getExtensionFromMime(string $mime_type) return explode("/", $mime_type)[1]; } - protected function downloadResponseToFile($response, string $pathToSave, string $filename): string + protected function downloadResponseToFile(ResponseInterface $response, string $pathToSave, string $filename): string { if (!is_dir($pathToSave)) { throw new \Exception("{$pathToSave} is not a directory"); } - $mime_type = $response->headers["Content-Type"]; - $filePath = "{$pathToSave}/{$filename}." . $this->getExtensionFromMime($mime_type); - if (!file_put_contents($filePath, $response->raw_body)) { - throw new DownloadException("Download success, but could not save the contents of the file to {$filePath}. Check permissions of the directory", 1, null, $response); + $mime_type = $response->getHeaders()["Content-Type"]; + $filePath = "{$pathToSave}/{$filename}." . $this->getExtensionFromMime($mime_type[0]); + $fileContent = $response->getBody()->getContents(); + if (!file_put_contents($filePath, $fileContent)) { + throw new DownloadException("Download success, but could not save the contents of the file to {$filePath}. Check permissions of the directory", 1, null, $fileContent); } return $filePath; }