44
55namespace Vonage \NumberVerification ;
66
7+ use GuzzleHttp \Psr7 \Request ;
8+ use Psr \Http \Client \ClientExceptionInterface ;
79use Vonage \Client \APIClient ;
810use Vonage \Client \APIResource ;
11+ use Vonage \Client \Credentials \CredentialsInterface ;
12+ use Vonage \Client \Credentials \Gnp ;
13+ use Vonage \Client \Exception \Credentials ;
14+ use Vonage \Client \Exception \Exception ;
15+ use Vonage \Webhook \Factory ;
916
1017class Client implements APIClient
1118{
@@ -18,8 +25,81 @@ public function getAPIResource(): APIResource
1825 return $ this ->api ;
1926 }
2027
21- public function verify (): bool
28+ /**
29+ * You are expected to call this code when you are consuming the webhook that has been
30+ * received from the frontend call being made
31+ *
32+ * @param string $phoneNumber
33+ * @return bool
34+ * @throws ClientExceptionInterface
35+ * @throws Exception
36+ */
37+ public function verifyNumber (string $ phoneNumber ): bool
2238 {
23- return false ;
39+ $ webhook = Factory::createFromGlobals ();
40+
41+ if (!isset ($ webhook ['code ' ])) {
42+ throw new Exception ('Required field code not found in webhook ' );
43+ };
44+
45+ /** @var Gnp $credentials */
46+ $ credentials = $ this ->getAPIResource ()->getClient ()->getCredentials ();
47+ $ credentials ->setCode ($ webhook ['code ' ]);
48+ $ credentials ->setState ($ webhook ['state ' ]);
49+
50+ $ phoneNumberKey = 'phoneNumber ' ;
51+
52+ if ($ this ->isHashedPhoneNumber ($ phoneNumber )) {
53+ $ phoneNumberKey = 'hashedPhoneNumber ' ;
54+ }
55+
56+ // This request will now contain a valid CAMARA token
57+ $ response = $ this ->getAPIResource ()->create ([
58+ $ phoneNumberKey => $ phoneNumber
59+ ]);
60+
61+ return $ response ['devicePhoneNumberVerified ' ];
62+ }
63+
64+ public function isHashedPhoneNumber (string $ phoneNumber ): bool
65+ {
66+ return (strlen ($ phoneNumber ) >= 13 );
67+ }
68+
69+ /**
70+ * This method is the start of the process of Number Verification
71+ * It builds the correct Front End Auth request for OIDC CAMARA request
72+ *
73+ * @param string $phoneNumber
74+ * @param string $redirectUrl
75+ * @param string $state
76+ * @return string
77+ * @throws Credentials
78+ */
79+ public function buildFrontEndUrl (string $ phoneNumber , string $ redirectUrl , string $ state = '' ): string
80+ {
81+ /** @var Gnp $credentials */
82+ $ credentials = $ this ->getAPIResource ()->getClient ()->getCredentials ();
83+ $ this ->enforceCredentials ($ credentials );
84+
85+ $ applicationId = $ credentials ->getApplication ();
86+
87+ $ query = http_build_query ([
88+ 'client_id ' => $ applicationId ,
89+ 'redirect_uri ' => $ redirectUrl ,
90+ 'state ' => $ state ,
91+ 'scope ' => 'openid dpv:FraudPreventionAndDetection#number-verification-verify-read ' ,
92+ 'response_type ' => 'code ' ,
93+ 'login_hint ' => $ phoneNumber
94+ ]);
95+
96+ return 'https://oidc.idp.vonage.com/oauth2/auth ' . $ query ;
97+ }
98+
99+ protected function enforceCredentials (CredentialsInterface $ credentials ): void
100+ {
101+ if (!$ credentials instanceof Gnp) {
102+ throw new Credentials ('You can only use GNP Credentials with the Number Verification API ' );
103+ }
24104 }
25105}
0 commit comments