<?php 
require 'header.php';
 require_once __DIR__ . '/Functions/masterr/vendor/autoload.php';
    require_once __DIR__ . '/Functions/masterr/library/GooglePlay/Validator.php';
    
   function missingServiceAccountDetailsWarning()
{
    $ret = "
    <h3 class='warn'>
      Warning: You need download your Service Account Credentials JSON from the
      <a href='http://developers.google.com/console'>Google API console</a>.
    </h3>
    <p>
      Once downloaded, move them into the root directory of this repository and
      rename them 'service-account-credentials.json'.
    </p>
    <p>
      In your application, you should set the GOOGLE_APPLICATION_CREDENTIALS environment variable
      as the path to this file, but in the context of this example we will do this for you.
    </p>";
    return $ret;
}
		function checkServiceAccountCredentialsFile()
{
    // service account credentials
    $app_credentials = __DIR__ . '/google_service.json';
    return file_exists($app_credentials) ? $app_credentials : false;
}
?>
        <div class="container-fluid" width="1800px">
          <div class="card shadow mb-4">
            <?php if(isset($_SESSION['response'])) { ?>
              <div class="alert alert-<?= $_SESSION['res_type']; ?> 
                alert-dismissible text-center">
                <button class="close" type="button" data-dismiss="alert">
                &times;
                </button>
                <b class="text-center"><?=  $_SESSION['response']; ?></b>
              </div>
              <?php  } unset($_SESSION['response']);?>
            <div class="card-header py-2">
              <h6 class="m-0 font-weight-bold text-primary">LIST OF PURCHASED NUMBERS</h6>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered text-center" id="dataTable+1"  cellspacing="0">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>NUMBER</th>
                      <th>TAG</th>
                      <th>STATUS</th>
                      <th>START_DATE</th>
                      <th>EXPIRY_DATE</th>
                      <th>OPERATION</th>
                      <th>USER_ID</th>
                      <th>CANCEL</th>
                    </tr>
                  </thead>
                  <tfoot>
                    <tr>
                      <th>ID</th>
                      <th>NUMBER</th>
                      <th>TAG</th>
                      <th>STATUS</th>
                       <th>START_DATE</th>
                      <th>EXPIRY_DATE</th>
                      <th>OPERATION</th>
                      <th>USER_ID</th>
                      <th>CANCEL</th>
                    </tr>
                  </tfoot>
                  <?php 
                  $root = realpath(dirname(dirname(__FILE__)));
	$library = "$root/library";
    $path = array($library, get_include_path());
    set_include_path(implode(PATH_SEPARATOR, $path));


    use ReceiptValidator\GooglePlay\Validator as Validator;
    // google authentication
    $applicationName = 'mobisystems';
    $scope = [Google_Service_AndroidPublisher::ANDROIDPUBLISHER];
    $configLocation = 'google_service.json';
					
				 	// receipt data
    $packageName = 'com.temp_number.receive_sms.second_phone.virtual_number_whatsapp';
    
    
    $client = new \Google_Client();
    

    
    if ($credentialsFile = checkServiceAccountCredentialsFile()) {
      
    // set the location manually
    $client->setAuthConfig($credentialsFile);
   
} else if (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
     
    // use the application default credentials
     $client->useApplicationDefaultCredentials();
        
} else {
      echo $packageName;
    die;
    echo missingServiceAccountDetailsWarning();
     
    return;
}

$client->setApplicationName($applicationName);
$client->setAuthConfig($configLocation);
$client->setScopes($scope);
                    $query="SELECT tbl_number.*, tbl_purchase_number.user_purchase_date,tbl_purchase_number.p_id, tbl_purchase_number.user_expiry_date, tbl_purchase_number.user_id, tbl_purchase_number.plan FROM tbl_number LEFT JOIN tbl_purchase_number ON tbl_number.n_id = tbl_purchase_number.num_id WHERE tbl_number.status='Purchased' ORDER BY tbl_purchase_number.`user_purchase_date` DESC";
                    
                    $stmt=$Connection->prepare($query);
                    $stmt->execute();
                    $result=$stmt->get_result();
                   
                  ?>
                  <tbody>
                    <?php 
                    
                    while($row=$result->FETCH_ARRAY()) {
                         $productId = '';
                         $query12="SELECT p.product_id AS PROD FROM tbl_product_ids p
            JOIN tbl_product_userids c on p.id = c.product_id
            WHERE number_id = ".$row['n_id'];
                    
                    $stmt12=$Connection->prepare($query12);
                    $stmt12->execute();
                    $result123=$stmt12->get_result();
                   while($row1=$result123->FETCH_ARRAY()) {
                       $productId=$row1['PROD'];
                   }
                       
                  
                
                   $purchaseToken = $row['p_token'];
$purchase_status=12;
$validator = new Validator(new \Google_Service_AndroidPublisher($client));
try {
    $response = $validator->setPackageName($packageName)->setProductId($productId)->setPurchaseToken($purchaseToken)->validateSubscription();
    $purchase_status=$response->getCancelReason();
    
} catch (Exception $e) {
    $purchase_status==12;
    //echo $e->getMessage() . PHP_EOL;
}

 
                    
                    ?>
                    <tr>
                      <td><?= $row['n_id']; ?></td>
                      <td><?= $row['numbers']; ?></td>
                      <td><?= $row['tag']; ?></td>
                      <td><?= $row['status']; ?></td>
                      <td><?= $row['user_purchase_date']; ?></td>
                      <td><?= $row['user_expiry_date']; ?></td>
                      <td>
                        <a href="addNumber.php?reverse=<?=$row['n_id'];?>"><span class="fa fas fa-undo fa-2x"  data-toggle="tooltip"
                        data-placement="top" title="edit data"></span></a>
                      </td>
                      <td><?= $row['user_id']; ?></td>
                      <td><?= $purchase_status; ?></td>
                    </tr>
                  <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>

        <!-- /.container-fluid -->
        <script>
          $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();   
          });
        </script>

      </div>
      <!-- End of Main Content -->

      <!-- Footer -->
      <footer class="sticky-footer bg-white">
        <div class="container my-auto">
          <div class="copyright text-center my-auto">
            <span>Copyright &copy; SoftArena 2020</span>
          </div>
        </div>
      </footer>

      <!-- End of Footer -->
  <!-- End of Page Wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
  <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
          <a class="btn btn-primary" href="logout.html">Logout</a>
        </div>
      </div>
    </div>
  </div>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin-2.min.js"></script>

  <!-- Page level plugins -->
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="js/demo/datatables-demo.js"></script>

</body>

</html>
    