<?
/*
* Copyright (c) 2007, DreamTime.net Inc.
* All rights reserved.
* Email: sales@diamondcard.us
* Phone: 1-303-997-0900
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the DreamTime.net Inc. nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY DreamTime.net Inc. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DreamTime.net Inc. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


// -- adjust these values
// Authorization key
$AUTH_CODE = 'ABCD1234567890EFGH';

$SERVER = 'https://www.diamondcard.us';
$URI = 'api';
$PROXY = $SERVER.'/exec/api-dispatcher';
$WSDL = $SERVER.'/download/api/dc-api.wsdl';

?> 
<html> 
<head> 
    <title>Diamondcard.us API Example</title> 
</head> 
<h1>Diamondcard.us API Example</h1>
<center><a href="example.php?sec=did">order DIDs</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="example.php?sec=acc_add">add account</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="example.php?sec=acc_upd">update account</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="example.php?sec=acc">manage accounts</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="example.php?sec=did_man">manage DIDs</a></center>
<BR>
<?

if ($_REQUEST['sec'] == 'did') {
?>
<table cellpadding="3" border="1">
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="countries"><input type="hidden" name="sec" value="did">
            DID Type: <select name="didType"><option value="PPN">Regular</option><option value="TFN">Toll-Free</option></select>
            <input type="submit" name="Go" value="Show countries">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="states"><input type="hidden" name="sec" value="did">
            Country: <select name="country"><option value="US">United States</option><option value="CA">Canada</option></select>
            <input type="submit" name="Go" value="Show states">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="didlist"><input type="hidden" name="sec" value="did">
            DID Type: <select name="didType"><option value="PPN">Regular</option><option value="TFN">Toll-Free</option></select>
            Country: <input type="text" size="2" name="country">
            State: <input type="text" size="2" name="state">
            <input type="submit" name="Go" value="Show cities/prices">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="order"><input type="hidden" name="sec" value="did">
            DID Code: <input type="text" size="13" name="didCode">
            Quantity: <input type="text" size="2" name="quantity">
            Duration: <select name="duration"><option value="1">1 month</option><option value="3">3 months</option><option value="6">6 months</option><option value="12">1 year</option></select>
            Unique Key: <input type="text" size="13" name="uniqueKey">
            <input type="submit" name="Go" value="Order DID">
            </form>
        </td>
        </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="order_info"><input type="hidden" name="sec" value="did">
            Order ID: <input type="text" size="5" name="OrderId">
            <input type="submit" name="Go" value="Show Order details">
            </form>
        </td>
    </tr>
</table>
<?
}
else if ($_REQUEST['sec'] == 'acc_add') {
?>
<table cellpadding="3" border="1">
      <tr>
        <td align="center"> <b>Add Account</b>
            <form action="example.php" method="post"><input type="hidden" name="action" value="acc_add"><input type="hidden" name="sec" value="acc_add">
                        <table border="0">
                          <tr>
                            <td>UserName:</td><td><input type="text" size="10" name="Username"></td>
                            <td>Password:</td><td><input type="text" size="10" name="Password"></td>
                          </tr>
                          <tr>
                            <td>Amount:</td><td><input type="text" size="5" name="Amount"><select name="Currency"><option value="USD">USD</option><option value="EUR">EUR</option></select></td>
                            <td>Unlimited balance:</td><td><select name="UnlimBalance"><option value="">No</option><option value="unlim">Yes</option></select></td>
                          </tr>
                          <tr>
                            <td>Firstname:</td><td><input type="text" size="10" name="FirstName"></td>
                            <td>Lastname:</td><td><input type="text" size="10" name="LastName"></td>
                          </tr>
                          <tr>
                            <td>Email:</td><td><input type="text" size="10" name="Email"></td>
                            <td>Phone:</td><td><input type="text" size="10" name="Phone"></td>
                          </tr>
                          <tr>                          
                            <td colspan="4">Do not send notification email to new account <input type="checkbox" name="NotifyAcc" value="1"></td>
                          </tr>
                          <tr>
                            <td colspan="4" align="center"><input type="submit" name="Go" value="Add account"></td>
                          </tr>
            </form>
        </td>
    </tr>
</table>
<?
}
else if ($_REQUEST['sec'] == 'acc') {
?>
<table cellpadding="3" border="1">
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="acc_info"><input type="hidden" name="sec" value="acc">
            Account Id: <input type="text" size="5" name="AccId"> <input type="submit" name="Go" value="Account details">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="acc_recharge"><input type="hidden" name="sec" value="acc">
            Account Id: <input type="text" size="5" name="AccId">
            Amount: <input type="text" size="5" name="Amount"><select name="Currency"><option value="USD">USD</option><option value="EUR">EUR</option></select>
            <input type="submit" name="Go" value="Recharge account">
            </form>
        </td>
    </tr>
</table>
<?
}
else if ($_REQUEST['sec'] == 'acc_upd') {
?>
<table cellpadding="3" border="1">
      <tr>
                <td align="center"> <b>Update Account</b>
                        <form action="example.php" method="post"><input type="hidden" name="action" value="acc_update"><input type="hidden" name="sec" value="acc_upd">
                        <table border="0">
                          <tr>
                            <td>Account Id:</td><td><input type="text" size="10" name="AccId"></td>
                          </tr>
                          <tr>
                            <td>Status</td><td><select name="Status"><option value="">do not change</option><option value="ON">Turn ON</option><option value="OFF">Turn OFF</option></select></td>
                          </tr>
                          <tr>
                            <td>SpeedDial:</td><td colspan="3"><input type="text" size="60" name="SpeedDial">Code and number pairs separated by coma. CODE1_NUMBER1,CODE2_NUMBER2. 'OFF' value will remove all speed dial numbers</td>
                          </tr>
                          <tr>
                            <td>Pinless:</td><td colspan="3"><input type="text" size="60" name="Pinless"> Pinless numbers separated by coma. 'OFF' value will remove all pinless numbers.</td>
                          </tr>
                          <tr>
                            <td>Caller ID:</td><td colspan="3"><select name="CallerIdSwitch"><option value="">do not change</option><option value="ON">Turn ON</option><option value="OFF">Turn OFF</option></select> Number:<input type="text" size="13" name="CallerId"></td>
                          </tr>
                          <tr>
                            <td colspan="4" align="center"><input type="submit" name="Go" value="Update account"></td>
                          </tr>
            </table>
                        </form>
        </td>
        </tr>
</table>
<?
}
else if ($_REQUEST['sec'] == 'did_man') {
?>
<table cellpadding="3" border="1">
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="did_info"><input type="hidden" name="sec" value="did_man">
            Number: <input type="text" size="12" name="Number"> <input type="submit" name="Go" value="DID details">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="did_assign"><input type="hidden" name="sec" value="did_man">
            Number: <input type="text" size="12" name="Number"> 
            Account Id: <input type="text" size="5" name="AccId">
            Exp. date(mm/dd/yyyy): <input type="text" size="10" name="ExpDate">
            <input type="submit" name="Go" value="Assign DID">
            </form>
        </td>
    </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="did_update"><input type="hidden" name="sec" value="did_man">
                        Number: <input type="text" size="12" name="Number">
                        Account Id: <input type="text" size="5" name="AccId">
                        Redirect: <input type="text" size="12" name="Redirect">
                        AutoRenew: <select name="AutoRenew"><option value="">do not change</option><option value="ON">Turn ON</option><option value="OFF">Turn OFF</option></select>
            <input type="submit" name="Go" value="Update DID">
            </form>
        </td>
        </tr>
      <tr>
        <td>
            <form action="example.php" method="post"><input type="hidden" name="action" value="did_renew"><input type="hidden" name="sec" value="did_man">
                        Number: <input type="text" size="12" name="Number">
                        Duration: <select name="RenewPeriod"><option value="1">1 month</option><option value="3">3 months</option><option value="6">6 months</option><option value="12">1 year</option></select>
                        Unique Key: <input type="text" size="13" name="uniqueKey">
                        <input type="submit" name="Go" value="Renew DID">
            </form>
        </td>
        </tr>
</table>
<?
}




if ($_REQUEST['action']) {
  print "<h3> Result: </h3>";
}

                    // Load countries list
if ($_REQUEST['action'] == 'countries') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'DidType' => $_REQUEST['didType']);
                    // Do sending
  $res = sendRequest('did_listcountries', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "Country code,Country name<BR>";
    print implode("<br>", $res->Countries)."<BR>";
  }
}
                    // Load states list
else if ($_REQUEST['action'] == 'states') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'Country' => $_REQUEST['country']);
                    // Do sending
  $res = sendRequest('did_liststates', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "State Code<BR>";
    print implode("<br>", $res->States)."<BR>";
  }
}
                    // Load DID cicites with prices
else if ($_REQUEST['action'] == 'didlist') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'DidType' => $_REQUEST['didType'],
           'Country' => $_REQUEST['country'],
           'State' => $_REQUEST['state']);
                    // Do sending
  $res = sendRequest('did_list', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "DidCode,City,Prefix,Setup Price,Monthly Price<BR>";
    print implode("<br>", $res->List)."<BR>";
  }
}
                    // Order DID
else if ($_REQUEST['action'] == 'order') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'DidCode' => $_REQUEST['didCode'],
           'Quantity' => $_REQUEST['quantity'],
           'UniqKey' => $_REQUEST['uniqueKey'],
           'Duration' => $_REQUEST['duration']);
                    // Do sending
  $res = sendRequest('order_did', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "DID ordered.<BR>";
    print "Status: ".$res->Status."<BR>";
    print "OrderId: ".$res->OrderId."<BR>";
    if ($res->Numbers) {
      print "Assigned numbers: ".$res->Numbers."\n";
    }
  }
}

else if ($_REQUEST['action'] == 'order_info') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'OrderId' => $_REQUEST['OrderId']);
                    // Do sending
  $res = sendRequest('order_info', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "DID order info.<BR>";
    print "Status: ".$res->Status."<BR>";
    if ($res->Numbers) {
      print "Assigned numbers: ".$res->Numbers."\n";
    }
  }
}

else if ($_REQUEST['action'] == 'acc_add') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'UserName' => $_REQUEST['Username'],
           'Password' => $_REQUEST['Password'],
                   'BalanceType' => $_REQUEST['UnlimBalance'],
           'Amount' => $_REQUEST['Amount'],
                   'Currency' => $_REQUEST['Currency'],
                   'FirstName' => $_REQUEST['FirstName'],
                   'LastName' => $_REQUEST['LastName'],
                   'Email' => $_REQUEST['Email'],
                   'NoMail' => $_REQUEST['NotifyAcc'],
                   'DayPhone' => $_REQUEST['Phone']);
                    // Do sending
  $res = sendRequest('acc_add', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "Account added.<BR>";
    print "New account Id: ".$res->AccId."<BR>";
    print "New account Pin Code: ".$res->PinCode."<BR>";
  }
}

else if ($_REQUEST['action'] == 'acc_info') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'AccId' => $_REQUEST['AccId']);
                    // Do sending
  $res = sendRequest('acc_info', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "Account <b>".$_REQUEST['AccId']."</b> info<BR>";
    print "Status: ".$res->Status."<BR>";
    print "PinCode: ".$res->PinCode."<BR>";
    if ($res->Balanceype == 'unlim') {
      print "Unlimited balance. Curent value: ".$res->Balance." ".$res->Currency."<BR>";
    }
    else {
      print "Balance ".$res->Balance." ".$res->Currency."<BR>";
    }
    print "<BR>";
    print "FirstName: ".$res->FirstName."<BR>";
    print "LastName: ".$res->LastName."<BR>";
    print "Email: ".$res->Email."<BR>";
    print "DayPhone: ".$res->DayPhone."<BR>";
    print "<BR>";
    print "SpeedDial numbers: ".$res->SpeedDial."<BR>";
    print "Pinless  numbers: ".$res->Pinless."<BR>";
    print "CallerId: ".$res->CallerId."<BR>";
  }
}
else if ($_REQUEST['action'] == 'acc_recharge') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'AccId' => $_REQUEST['AccId'],
           'Amount' => $_REQUEST['Amount'],
                   'Currency' => $_REQUEST['Currency']);
                    // Do sending
  $res = sendRequest('acc_recharge', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "Account recharged.<BR>";
    print "New Balance: ".$res->Balance." ".$res->Currency."<BR>";
  }
}
else if ($_REQUEST['action'] == 'acc_update') {
  $callerId='';
  if ($_REQUEST['CallerIdSwitch'] == 'ON' ){
    $callerId = 'ON_'.$_REQUEST['CallerId'];
  }
  else if ($_REQUEST['CallerIdSwitch'] == 'OFF' ){
    $callerId = 'OFF';
  }
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'AccId' => $_REQUEST['AccId'],
           'Status' => $_REQUEST['Status'],
           'SpeedDial' => $_REQUEST['SpeedDial'],
                   'Pinless' => $_REQUEST['Pinless'],
           'CallerId' => $callerId);
                    // Do sending
  $res = sendRequest('acc_update', $params);
  if ($res->ErrMsg == 'OK') {         // Success
    print "Account updated";
  }
  else {                // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
}

else if ($_REQUEST['action'] == 'did_info') {    
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
                   'Number' => $_REQUEST['Number']);
                                        // Do sending
  $res = sendRequest('did_info', $params);
  if ($res->ErrMsg) {                   // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
  else {                                   // Success
    print "DID ".$_REQUEST['Number']." info.<BR>";
    print "Expiration date: ".$res->ExpDate."<BR>";
    print "Redirect rule: ".$res->Redirect."<BR>";
    print "AutoRenew: ".$res->AutoRenew."<BR>";
    if ($res->SubAccId) {
      print "<br>";
      print "Assigned to sub account: ".$res->SubAccId."<BR>";
      print "Sub account expiration date: ".$res->SubExpDate."<BR>";
      print "Sub account autoRenew: ".$res->SubAutoRenew."<BR>";
      if ($res->SubRedirect == 'ASSIGNED') {
        print "Sub account assigned it to his sub account<BR>";
      } else {
        print "Sub account redirect rule:".$res->SubRedirect."<BR>";
      }
    }
  }
}

else if ($_REQUEST['action'] == 'did_assign') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'Number' => $_REQUEST['Number'],
           'SubAccId' => $_REQUEST['AccId'],
           'ExpDate' => $_REQUEST['ExpDate']);
                    // Do sending
  $res = sendRequest('did_assign', $params);
  if ($res->ErrMsg == 'OK') {         // Success
    print "DID assigned";
  }
  else {                // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
}

else if ($_REQUEST['action'] == 'did_update') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'Number' => $_REQUEST['Number'],
           'SubAccId' => $_REQUEST['AccId'],
           'Redirect' => $_REQUEST['Redirect'],
           'AutoRenew' => $_REQUEST['AutoRenew']);
                    // Do sending
  $res = sendRequest('did_update', $params);
  if ($res->ErrMsg == 'OK') {         // Success
    print "DID updated";
  }
  else {                // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
}
else if ($_REQUEST['action'] == 'did_renew') {
  $params = array ('AuthKey' => $AUTH_CODE,             // Authorization key
           'Number' => $_REQUEST['Number'],
           'RenewPeriod' => $_REQUEST['RenewPeriod'],
           'UniqKey' => $_REQUEST['uniqueKey']);
                    // Do sending
  $res = sendRequest('did_renew', $params);
  if ($res->ErrMsg == 'OK') {         // Success
    print "DID renewed";
  }
  else {                // Fail
    print "<font color='red'>Error: ".$res->ErrMsg."</font><br>";   // Error message
  }
}


//
// sendRequest - send request using NuSOAP lib
//
function sendRequest($func,$params) {
  global $WSDL;
  $client = new soapclient($WSDL);
  
  try {
    $result = $client->$func(array('inParams' => $params));
  } catch ( Exception $e) {
    $result->out->ErrMsg = $e->getMessage();
  }

//  echo 'Result: <BR>';
//  echo print_r($result);
  return $result->out;
}

?>

<br> 
</body> 
</html>