diff --git a/CHANGELOG.md b/CHANGELOG.md index e92d1501..51fc6814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # Change Log + +## [4.58.0](https://github.com/plivo/plivo-php/tree/v4.58.0) (2023-10-18) +**Feature - Verify Caller Id API support** +-API support for verifying, updating, getting and deleting caller IDs. + ## [4.57.1](https://github.com/plivo/plivo-php/tree/v4.57.1) (2023-10-18) **Bug fix - Dial XML element** - Fixed constructor method signature for Dial XML element diff --git a/src/Plivo/Resources/VerifyCallerId/InitiateVerifyResponse.php b/src/Plivo/Resources/VerifyCallerId/InitiateVerifyResponse.php new file mode 100644 index 00000000..75288fc3 --- /dev/null +++ b/src/Plivo/Resources/VerifyCallerId/InitiateVerifyResponse.php @@ -0,0 +1,34 @@ +verificationUuid = $verificationUuid; + } + + public function getVerificationUuid() + { + return $this->verificationUuid; + } + + +} diff --git a/src/Plivo/Resources/VerifyCallerId/ListVerifiedCallerIdResponse.php b/src/Plivo/Resources/VerifyCallerId/ListVerifiedCallerIdResponse.php new file mode 100644 index 00000000..1bd3f338 --- /dev/null +++ b/src/Plivo/Resources/VerifyCallerId/ListVerifiedCallerIdResponse.php @@ -0,0 +1,45 @@ +meta = $meta; + $this->objects = $objects; + } + + public function getMeta() + { + return $this->meta; + } + + public function getObjects() + { + return $this->objects; + } + + +} diff --git a/src/Plivo/Resources/VerifyCallerId/Verify.php b/src/Plivo/Resources/VerifyCallerId/Verify.php new file mode 100644 index 00000000..0a567ffd --- /dev/null +++ b/src/Plivo/Resources/VerifyCallerId/Verify.php @@ -0,0 +1,80 @@ +alias = $alias; + $this->country = $country; + $this->createdAt = $createdAt; + $this->modifiedAt = $modifiedAt; + $this->phoneNumber = $phoneNumber; + $this->subaccount = $subaccount; + $this->verificationUuid = $verificationUuid; + } + + public function getAlias(): string + { + return $this->alias; + } + + public function getCountry(): string + { + return $this->country; + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getModifiedAt(): string + { + return $this->modifiedAt; + } + + public function getPhoneNumber(): string + { + return $this->phoneNumber; + } + + public function getSubaccount(): string + { + return $this->subaccount; + } + + public function getVerificationUuid(): string + { + return $this->verificationUuid; + } + +} diff --git a/src/Plivo/Resources/VerifyCallerId/VerifyCallerIdResponse.php b/src/Plivo/Resources/VerifyCallerId/VerifyCallerIdResponse.php new file mode 100644 index 00000000..483c55ed --- /dev/null +++ b/src/Plivo/Resources/VerifyCallerId/VerifyCallerIdResponse.php @@ -0,0 +1,80 @@ +alias = $alias; + $this->channel = $channel; + $this->country = $country; + $this->createdAt = $createdAt; + $this->phoneNumber = $phoneNumber; + $this->subaccount = $subaccount; + $this->verificationUuid = $verificationUuid; + } + + public function getAlias(): string + { + return $this->alias; + } + + public function getChannel(): string + { + return $this->channel; + } + + public function getCountry(): string + { + return $this->country; + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getPhoneNumber(): string + { + return $this->phoneNumber; + } + + public function getSubaccount(): string + { + return $this->subaccount; + } + + public function getVerificationUuid(): string + { + return $this->verificationUuid; + } + +} diff --git a/src/Plivo/Resources/VerifyCallerId/VerifyInterface.php b/src/Plivo/Resources/VerifyCallerId/VerifyInterface.php new file mode 100644 index 00000000..03ba6ce9 --- /dev/null +++ b/src/Plivo/Resources/VerifyCallerId/VerifyInterface.php @@ -0,0 +1,247 @@ +pathParams = [ + 'authId' => $authId + ]; + $this->uri = "Account/".$authId."/VerifiedCallerId"; + } + + public function initiate($phoneNumber, array $optionalArgs = []){ + + $mandatoryArgs = [ + 'phone_number' => $phoneNumber, + ]; + + if (ArrayOperations::checkNull($mandatoryArgs) or empty($phoneNumber)) { + throw new PlivoValidationException( + "phoneNumber is mandatory and cannot be empty"); + } + + $response = $this->client->update( + $this->uri .'/', + array_merge($mandatoryArgs, $optionalArgs) + ); + + $responseContents = $response->getContent(); + + if(array_key_exists("error",$responseContents)){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + + ); + } else { + return new InitiateVerifyResponse( + $responseContents['api_id'], + $responseContents['message'], + $responseContents['verification_uuid'], + $response->getStatusCode() + ); + } + + } + + public function verify($verificationUuid, $otp){ + + $mandatoryArgs = [ + 'otp' => $otp, + ]; + + if (ArrayOperations::checkNull([$verificationUuid]) or empty($verificationUuid)) { + throw new PlivoValidationException( + "verification uuid is mandatory and cannot be empty"); + } + + if (ArrayOperations::checkNull($mandatoryArgs) or empty($otp)) { + throw new PlivoValidationException( + "otp is mandatory and cannot be empty"); + } + + $response = $this->client->update( + $this->uri . '/Verification/'. $verificationUuid.'/', + $mandatoryArgs + ); + + $responseContents = $response->getContent(); + + if(array_key_exists("error",$responseContents)){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + + ); + } else { + return new VerifyCallerIdResponse( + $responseContents['api_id'], + $responseContents['alias'], + $responseContents['channel'], + $responseContents['country'], + $responseContents['created_at'], + $responseContents['phone_number'], + $responseContents['subaccount'] ?? null, + $responseContents['verification_uuid'], + $response->getStatusCode() + ); + } + + } + + public function updateVerifiedCallerId($phoneNumber, array $optionalArgs = []){ + + if (ArrayOperations::checkNull([$phoneNumber]) or empty($phoneNumber)) { + throw new PlivoValidationException( + "phoneNumber is mandatory and cannot be empty"); + } + + $response = $this->client->update( + $this->uri . '/'. $phoneNumber.'/', + $optionalArgs + ); + + $responseContents = $response->getContent(); + + if(array_key_exists("error",$responseContents)){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + + ); + } else { + return new Verify( + $responseContents['api_id'], + $responseContents['alias'], + $responseContents['country'], + $responseContents['created_at'], + $responseContents['modified_at'], + $responseContents['phone_number'], + $responseContents['subaccount'], + $responseContents['verification_uuid'], + $response->getStatusCode() + ); + } + + } + + public function getVerifiedCallerId($phoneNumber){ + if (ArrayOperations::checkNull([$phoneNumber]) or empty($phoneNumber)) { + throw + new PlivoValidationException( + 'phoneNumber is mandatory and cannot be empty'); + } + + $response = $this->client->fetch( + $this->uri . '/'. $phoneNumber.'/', + [] + ); + + $responseContents = $response->getContent(); + if(array_key_exists("error",$responseContents)){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + + ); + } else { + return new Verify( + $responseContents['api_id'], + $responseContents['alias'], + $responseContents['country'], + $responseContents['created_at'], + $responseContents['modified_at'], + $responseContents['phone_number'], + $responseContents['subaccount'], + $responseContents['verification_uuid'], + $response->getStatusCode() + ); + } + + } + + public function listVerifiedCallerIds(array $optionalArgs = []){ + + $response = $this->client->fetch( + $this->uri .'/', + $optionalArgs + ); + + $responseContents = $response->getContent(); + + if(array_key_exists("error",$responseContents)){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + ); + } else { + return new ListVerifiedCallerIdResponse( + $responseContents['api_id'], + $responseContents['meta'], + $responseContents['objects'], + $response->getStatusCode() + ); + } + + } + + public function deleteVerifiedCallerId($phoneNumber){ + + if (ArrayOperations::checkNull([$phoneNumber]) or empty($phoneNumber)) { + throw new PlivoValidationException( + 'phoneNumber is mandatory and cannot be empty'); + } + + + $response = $this->client->delete( + $this->uri . '/'. $phoneNumber.'/', + [] + ); + + $responseContents = $response->getContent(); + + if(array_key_exists("error",$responseContents) && strlen($responseContents['error']) > 0 ){ + throw new PlivoResponseException( + $responseContents['error'], + 0, + null, + $response->getContent(), + $response->getStatusCode() + ); + } + + } + +} \ No newline at end of file diff --git a/src/Plivo/RestClient.php b/src/Plivo/RestClient.php index 48d6342d..df494568 100644 --- a/src/Plivo/RestClient.php +++ b/src/Plivo/RestClient.php @@ -33,6 +33,7 @@ use Plivo\Resources\Profile\ProfileInterface; use Plivo\Resources\Token\TokenInterface; use Plivo\Resources\Zentrunk\ZentrunkInterface; +use Plivo\Resources\VerifyCallerId\VerifyInterface; /** * Class RestClient @@ -64,6 +65,7 @@ * @property HostedMessagingNumberInterface hostedMessagingNumber Interface to handle all HostedMessagingNumber related api calls * @property ZentrunkInterface Zentrunk Interface to handle all Zentrunk Call related api * @property MaskingSessionInterface Masking session Interface to handle all session related api calls + * @property VerifyInterface verify Interface to handle all verify caller ID related api calls * */ class RestClient @@ -223,6 +225,11 @@ class RestClient */ protected $_maskingSession; + /** + * @var VerifyInterface + */ + protected $_verifyCallerID; + /** * RestClient constructor. * @param string|null $authId @@ -593,4 +600,15 @@ public function getZentrunkCalls() } return $this->_zentrunkCall; } + + /** + * @return VerifyInterface + */ + protected function getVerifyCallerId() + { + if (!$this->_verifyCallerID) { + $this->_verifyCallerID = new VerifyInterface($this->client, $this->client->getAuthId()); + } + return $this->_verifyCallerID; + } } diff --git a/src/Plivo/Version.php b/src/Plivo/Version.php index 58a73614..6ad4b3e9 100644 --- a/src/Plivo/Version.php +++ b/src/Plivo/Version.php @@ -20,7 +20,7 @@ class Version /** * @const int PHP helper library minor version number */ - const MINOR = 57; + const MINOR = 58; /** * @const int PHP helper library patch number diff --git a/tests/Mocks/initiateVerifyResponse.json b/tests/Mocks/initiateVerifyResponse.json new file mode 100644 index 00000000..2b85e95b --- /dev/null +++ b/tests/Mocks/initiateVerifyResponse.json @@ -0,0 +1,5 @@ +{ + "api_id": "b1e04bbb-9cb3-435e-946a-aea5e4f4709e", + "message": "Verification code is sent to number +919999999999 which is valid for 15 minutes", + "verification_uuid": "74df1f8f-056d-4c9c-8fc5-11615fbb23a0" +} \ No newline at end of file diff --git a/tests/Mocks/listVerifiedCallerIdResponse.json b/tests/Mocks/listVerifiedCallerIdResponse.json new file mode 100644 index 00000000..b68ae0ba --- /dev/null +++ b/tests/Mocks/listVerifiedCallerIdResponse.json @@ -0,0 +1,22 @@ +{ + "api_id": "5449144c-ff78-4da3-b6a2-9b22abdb7bd4", + "meta": { + "limit": 20, + "next": null, + "offset": 0, + "previous": null, + "total_count": 1 + }, + "objects": [ + { + "alias": "test", + "country": "IN", + "created_at": "2023-09-26T07:58:10.345732Z", + "modified_at": "2023-09-26T07:58:10.345732Z", + "phone_number": "+919999999999", + "resource_uri": "/v1/Account/authID/VerifiedCallerId/919999999999", + "subaccount": "subaccount", + "verification_uuid": "30fe8cc9-be93-4315-afa8-7fe26d05bb01" + } + ] +} \ No newline at end of file diff --git a/tests/Mocks/updateVerifiedCallerIdResponse.json b/tests/Mocks/updateVerifiedCallerIdResponse.json new file mode 100644 index 00000000..fdaddb47 --- /dev/null +++ b/tests/Mocks/updateVerifiedCallerIdResponse.json @@ -0,0 +1,10 @@ +{ + "alias": "test", + "api_id": "35c999a4-f1ad-4af7-9cc6-c8ccac7fd283", + "country": "US", + "created_at": "2023-10-13T11:46:58.334899Z", + "modified_at": "2023-10-13T11:46:58.334899Z", + "phone_number": "+919999999999", + "subaccount": "", + "verification_uuid": "605c75f2-02b6-4cb8-883d-69cf37b21e5a" +} \ No newline at end of file diff --git a/tests/Mocks/verifyCallerIdResponse.json b/tests/Mocks/verifyCallerIdResponse.json new file mode 100644 index 00000000..2db982e5 --- /dev/null +++ b/tests/Mocks/verifyCallerIdResponse.json @@ -0,0 +1,9 @@ +{ + "alias": "QA Test", + "api_id": "910cc1e1-c5fc-4f58-b9de-aa30e1cfd241", + "channel": "call", + "country": "US", + "created_at": "2023-10-13T11:46:58.334898885Z", + "phone_number": "+919999999999", + "verification_uuid": "605c75f2-02b6-4cb8-883d-69cf37b21e5a" +} \ No newline at end of file diff --git a/tests/Resources/VerifyTest.php b/tests/Resources/VerifyTest.php new file mode 100644 index 00000000..df8900cf --- /dev/null +++ b/tests/Resources/VerifyTest.php @@ -0,0 +1,90 @@ + "+919999999999" + ]); + $body = file_get_contents(__DIR__ . '/../Mocks/initiateVerifyResponse.json'); + + $this->mock(new PlivoResponse($request,200, $body)); + $actual = $this->client->verifyCallerId->initiate("+919999999999"); + $this->assertRequest($request); + self::assertNotNull($actual); + } + + public function testVerify() + { + $verificationUuid = '605c75f2-02b6-4cb8-883d-69cf37b21e5a'; + $otp = "999999"; + $request = new PlivoRequest( + 'POST', + 'Account/MAXXXXXXXXXXXXXXXXXX/VerifiedCallerId/'.$verificationUuid.'/', + [ + "otp" => "999999" + ]); + $body = file_get_contents(__DIR__ . '/../Mocks/verifyCallerIdResponse.json'); + + $this->mock(new PlivoResponse($request,200, $body)); + $actual = $this->client->verifyCallerId->verify($verificationUuid,$otp); + self::assertNotNull($actual); + } + + public function testUpdateVerifiedCallerId() + { + $phoneNumber = "+919999999999"; + $request = new PlivoRequest( + 'POST', + 'Account/MAXXXXXXXXXXXXXXXXXX/VerifiedCallerId/'.$phoneNumber.'/', + [ + "alias" => "test-2" + ]); + $body = file_get_contents(__DIR__ . '/../Mocks/updateVerifiedCallerIdResponse.json'); + + $this->mock(new PlivoResponse($request,200, $body)); + $actual = $this->client->verifyCallerId->updateVerifiedCallerId($phoneNumber,["alias" => "test"]); + self::assertNotNull($actual); + } + + public function testGetVerifiedCallerId() + { + $phoneNumber = "+919999999999"; + $request = new PlivoRequest( + 'GET', + 'Account/MAXXXXXXXXXXXXXXXXXX/VerifiedCallerId/'.$phoneNumber.'/'); + $body = file_get_contents(__DIR__ . '/../Mocks/updateVerifiedCallerIdResponse.json'); + $this->mock(new PlivoResponse($request,200, $body)); + $actual = $this->client->verifyCallerId->getVerifiedCallerId($phoneNumber); + self::assertNotNull($actual); + } + + public function testListVerifiedCallerId() + { + $request = new PlivoRequest( + 'GET', + 'Account/MAXXXXXXXXXXXXXXXXXX/VerifiedCallerId/'); + $body = file_get_contents(__DIR__ . '/../Mocks/listVerifiedCallerIdResponse.json'); + $this->mock(new PlivoResponse($request,200, $body)); + $actual = $this->client->verifyCallerId->listVerifiedCallerIds(); + $this->assertRequest($request); + self::assertNotNull($actual); + } + +}