Skip to content

Commit

Permalink
Merge pull request #45 from team-telnyx/VerifyAPI-updates
Browse files Browse the repository at this point in the history
Updated Verify API
  • Loading branch information
d-telnyx authored Dec 15, 2020
2 parents 9235157 + cb4fa84 commit 2869d5c
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 162 deletions.
152 changes: 152 additions & 0 deletions examples/verify_sms_demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
require_once(__DIR__ . '/../init.php');

// Please fetch your API key from here https://portal.telnyx.com/#/app/api-keys
\Telnyx\Telnyx::setApiKey('######');

?>
<style>
.code {
background: #ddd;
border: 1px solid #333;
padding: 20px;
border-radius: 3px;
}
</style>
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'send_verification':

// Create a Verification profile
$verify_profile = \Telnyx\VerifyProfile::create(["name" => "Test Profile"]);

// Trigger a verification request and send SMS
$verification = \Telnyx\Verification::create([
'verify_profile_id' => $verify_profile['id'],
'phone_number' => $_POST['phone'],
'type' => 'sms'
]);
?>
<h3>Verification was sent to: <?php echo $_POST['phone'];?></h3>
<form method="post" action="">

<input type="hidden" name="action" value="check_verification">
<input type="hidden" name="verification_id" value="<?php echo $verification['id']; ?>">

<button type="submit">Check Verification Status</button>
<pre class="code">
// Retrieve the status of the verification
$verification = \Telnyx\Verification::retrieve('<?php echo $verification['id']; ?>');
</pre>
</form>
<?php
break;

case 'check_verification':

// Retrieve the status of the verification
$verification = \Telnyx\Verification::retrieve($_POST['verification_id']);

?>
<h3>Verification Status for ID: <?php echo $_POST['verification_id'];?></h3>
<pre><?php print_r($verification); ?></pre>

<form method="post" action="">

<input type="hidden" name="action" value="check_verification">
<input type="hidden" name="verification_id" value="<?php echo $_POST['verification_id']; ?>">

<button type="submit">Check Verification Status</button>
<pre class="code">
// Retrieve the status of the verification
$verification = \Telnyx\Verification::retrieve('<?php echo $_POST['verification_id']; ?>');
</pre>
</form>


<h3>Submit Verification Code</h3>

<form method="post" action="">

<input type="hidden" name="action" value="submit_verification_code">
<input type="hidden" name="verification_id" value="<?php echo $_POST['verification_id']; ?>">

<input id="verify-code-text" type="text" name="verification_code" placeholder="000000" oninput="update_verification_code()">

<button type="submit">Submit Verification Code</button>
<pre class="code">
// Submit verificaiton code
$verify_status = \Telnyx\Verification::submit_verification('<?php echo $verification['phone_number']; ?>', '<span id="verify-code">000000</span>');
</pre>
</form>
<script>
function update_verification_code() {
var textbox = document.getElementById("verify-code-text");
var span = document.getElementById("verify-code");
span.innerHTML = textbox.value;
}
</script>
<?php
break;

case 'submit_verification_code':

// Retrieve the status of the verification
$verification = \Telnyx\Verification::retrieve($_POST['verification_id']);

// Submit verification code here
$verify_status = \Telnyx\Verification::submit_verification($verification['phone_number'], $_POST['verification_code']);
?>
<h3>Submitted Verification Code: <?php echo $_POST['verification_code']; ?></h3>

<pre><?php print_r($verify_status); ?></pre>

<form method="post" action="">

<input type="hidden" name="action" value="check_verification">
<input type="hidden" name="verification_id" value="<?php echo $_POST['verification_id']; ?>">

<button type="submit">Check Verification Status</button>
<pre class="code">
// Retrieve the status of the verification
$verification = \Telnyx\Verification::retrieve('<?php echo $_POST['verification_id']; ?>');
</pre>
</form>
<?php
break;
}
}
else {
?>
<h1>Telnyx Verify Demo</h1>
<p>Hi and welcome to the Telnyx Verify API demo.</p>
<form method="post" action="">

<input type="hidden" name="action" value="send_verification">

<p><label>Enter a phone number. Please remember to include <a target="_blank" href="https://support.telnyx.com/en/articles/1130706-sip-connection-number-formats">country code</a>:</label></p>
<input id="phone-number-text" type="text" name="phone" placeholder="+15557770000" oninput="update_phone()">
<button type="submit">Send Verification Code to Phone</button>
<pre class="code">
// Create a Verification profile
$verify_profile = VerifyProfile::create(["name" => "Test Profile"]);

// Trigger a verification request and send SMS
$verification = Verification::create([
'verify_profile_id' => $verify_profile['id'],
'phone_number' => '<span id="phone-number-code">+15557770000</span>',
'type' => 'sms'
]);
</pre>
</form>
<script>
function update_phone() {
var textbox = document.getElementById("phone-number-text");
var span = document.getElementById("phone-number-code");
span.innerHTML = textbox.value;
}
</script>
<?php
}

3 changes: 3 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
require __DIR__ . '/lib/OtaUpdate.php';
require __DIR__ . '/lib/MobileOperatorNetwork.php';
require __DIR__ . '/lib/Balance.php';
require __DIR__ . '/lib/VerifyProfile.php';
require __DIR__ . '/lib/Verification.php';
require __DIR__ . '/lib/VerifyVerification.php';

// Telnyx API: Connections
require __DIR__ . '/lib/Connection.php';
Expand Down
20 changes: 20 additions & 0 deletions lib/ApiOperations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ public function delete($params = null, $opts = null)
$this->refreshFrom($response, $opts);
return $this;
}

/**
* @param string $id The ID of the resource to delete.
* @param array|null $params
* @param array|string|null $opts
*
* @return \Telnyx\ApiResource The updated resource.
*/
public static function remove($id, $params = null, $opts = null)
{
self::_validateParams($params);

$url = static::resourceUrl($id);

list($response, $opts) = static::_staticRequest('delete', $url, $params, $opts);
$obj = \Telnyx\Util\Util::convertToTelnyxObject($response->json, $opts);
$obj->setLastResponse($response);

return $obj;
}
}
3 changes: 3 additions & 0 deletions lib/Util/ObjectTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ObjectTypes
\Telnyx\OtaUpdate::OBJECT_NAME => \Telnyx\OtaUpdate::class,
\Telnyx\MobileOperatorNetwork::OBJECT_NAME => \Telnyx\MobileOperatorNetwork::class,
\Telnyx\Balance::OBJECT_NAME => \Telnyx\Balance::class,
\Telnyx\VerifyProfile::OBJECT_NAME => \Telnyx\VerifyProfile::class,
\Telnyx\Verification::OBJECT_NAME => \Telnyx\Verification::class,
\Telnyx\VerifyVerification::OBJECT_NAME => \Telnyx\VerifyVerification::class,

// Telnyx API: Connections
\Telnyx\Connection::OBJECT_NAME => \Telnyx\Connection::class,
Expand Down
26 changes: 8 additions & 18 deletions lib/TwoFactorVerify.php → lib/Verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,19 @@
namespace Telnyx;

/**
* Class TwoFactorProfile
* Class Verification
*
* @package Telnyx
*/
class TwoFactorVerify extends ApiResource
class Verification extends ApiResource
{
const OBJECT_NAME = "twofa_verification";
const OBJECT_NAME = "verification";

use ApiOperations\Create;
use ApiOperations\Retrieve;

/**
* @return string The endpoint associated with this singleton class.
*/
public static function classUrl()
{
// Use a custom URL for this resource
// NOTE: This endpoint is special because object name is "twofa_verification" and endpoint is "2fa_verifications"
return "/v2/2fa_verifications";
}

/**
* Retrieve a 2FA verification by phone number.
* Retrieve a verification by phone number
*
* @param string $phone_number
* @param array|string|null $options
Expand All @@ -34,15 +24,15 @@ public static function classUrl()
*/
public static function retrieve_by_phone_number($phone_number, $options = null)
{
$url = '/v2/2fa_verifications/by_tn/' . urlencode($phone_number);
$url = '/v2/verifications/by_phone_number/' . urlencode($phone_number);

list($response, $opts) = static::_staticRequest('get', $url, null, $options);
$obj = \Telnyx\Util\Util::convertToTelnyxObject($response->json, $opts);
return $obj;
}

/**
* Submit a 2FA verification code
* Submit a verification code
*
* @param string $phone_number
* @param string $verification_code
Expand All @@ -54,10 +44,10 @@ public static function submit_verification($phone_number, $verification_code, $o
{
$params = ['code' => $verification_code];
self::_validateParams($params);
$url = '/v2/2fa_verifications/by_tn/' . urlencode($phone_number) . '/actions/verify';
$url = '/v2/verifications/by_phone_number/' . urlencode($phone_number) . '/actions/verify';

list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
$obj = \Telnyx\Util\Util::convertToTelnyxObject($response->json, $opts);
return $obj;
}
}
}
9 changes: 4 additions & 5 deletions lib/TwoFactorProfile.php → lib/VerifyProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace Telnyx;

/**
* Class TwoFactorProfile
* Class VerifyProfile
*
* @package Telnyx
*/
class TwoFactorProfile extends ApiResource
class VerifyProfile extends ApiResource
{
const OBJECT_NAME = "2fa_profile";
const OBJECT_NAME = "verify_profile";

use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Retrieve;
use ApiOperations\Update;
use ApiOperations\Delete;

}
}
15 changes: 15 additions & 0 deletions lib/VerifyVerification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Telnyx;

/**
* Class Verification
*
* @package Telnyx
*/
class VerifyVerification extends ApiResource
{
const OBJECT_NAME = "verify_verification";

// Note: this is only a result used by VerificationTest
}
9 changes: 8 additions & 1 deletion tests/api_resources/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public function testIsCreatable()
'post',
'/v2/addresses'
);
$resource = Address::create(['administrative_area'=>'IL']);
$resource = Address::create([
"first_name" => "Alfred",
"last_name" => "Foster",
"business_name" => "Company",
"country_code" => "US",
"locality" => "Chicago",
"street_address" => "311 W Superior Street"
]);
$this->assertInstanceOf(\Telnyx\Address::class, $resource);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/api_resources/ApiOperations/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public function testTraitDelete()
$result = $class->delete();
$this->assertInstanceOf(\Telnyx\TelnyxObject::class, $result);
}
public function testTraitRemove()
{
$result = DummyDelete::remove(self::TEST_RESOURCE_ID);
$this->assertInstanceOf(\Telnyx\TelnyxObject::class, $result);
}
}
7 changes: 6 additions & 1 deletion tests/api_resources/CredentialConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public function testIsCreatable()
'post',
'/v2/credential_connections'
);
$resource = CredentialConnection::create(["connection_name" => "Test Connection"]);
$resource = CredentialConnection::create([
'user_name' => 'test_user',
'password' => 'iloveyou',
'connection_name' => 'Test Connection',
"anchorsite_override" => "Latency"
]);
$this->assertInstanceOf(\Telnyx\CredentialConnection::class, $resource);
}

Expand Down
Loading

0 comments on commit 2869d5c

Please sign in to comment.