Validation

Refactoring
This commit is contained in:
drigato 2014-06-20 16:31:26 -04:00
parent 01bf2883e5
commit 1e1dc1dd03
2 changed files with 71 additions and 51 deletions

View File

@ -55,7 +55,7 @@ class BillingController extends Zend_Controller_Action {
$result = $this->makeRequest($credentials["url"], $client_query_string); $result = $this->makeRequest($credentials["url"], $client_query_string);
if ($result["result"] == "error") { if ($result["result"] == "error") {
$this->view->errorMessage = "An error occurred and we could not upgrade your account. Please contact support for help"; $this->setErrorMessage();
$this->view->form = $form; $this->view->form = $form;
} else { } else {
//$result = $this->makeRequest($credentials["url"], $upgrade_query_string); //$result = $this->makeRequest($credentials["url"], $upgrade_query_string);
@ -70,10 +70,27 @@ class BillingController extends Zend_Controller_Action {
} }
} }
private function setErrorMessage($msg=null)
{
if (!is_null($msg)) {
$this->view->errorMessage = $msg;
} else {
$this->view->errorMessage = "An error occurred and we could not update your account. Please contact support for help.";
}
}
private function setSuccessMessage($msg=null)
{
if (!is_null($msg)) {
$this->view->successMessage = $msg;
} else {
$this->view->successMessage = "Your account has been updated.";
}
}
private static function getAPICredentials() private static function getAPICredentials()
{ {
return array( return array(
"access_key" => $_SERVER["WHMCS_ACCESS_KEY"],
"username" => $_SERVER["WHMCS_USERNAME"], "username" => $_SERVER["WHMCS_USERNAME"],
"password" => $_SERVER["WHMCS_PASSWORD"], "password" => $_SERVER["WHMCS_PASSWORD"],
"url" => "https://account.sourcefabric.com/includes/api.php?accesskey=".$_SERVER["WHMCS_ACCESS_KEY"], "url" => "https://account.sourcefabric.com/includes/api.php?accesskey=".$_SERVER["WHMCS_ACCESS_KEY"],
@ -98,28 +115,37 @@ class BillingController extends Zend_Controller_Action {
$form = new Application_Form_BillingClient(); $form = new Application_Form_BillingClient();
if ($request->isPost()) { if ($request->isPost()) {
$formData = $request->getPost(); $formData = $request->getPost();
if ($form->isValid($formData)) {
$accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; $credentials = self::getAPICredentials();
$username = $_SERVER["WHMCS_USERNAME"];
$password = $_SERVER["WHMCS_PASSWORD"]; $postfields = array();
$url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($credentials["password"]);
$postfields = array(); $postfields["action"] = "updateclient";
$postfields["username"] = $username; //$postfields["clientid"] = Application_Model_Preference::GetClientId();
$postfields["password"] = md5($password); $postfields["clientid"] = 18460000000;
$postfields["action"] = "updateclient"; $postfields["responsetype"] = "json";
//$postfields["clientid"] = Application_Model_Preference::GetClientId(); $postfields = array_merge($postfields, $formData);
$postfields["clientid"] = 1846; unset($postfields["password2verify"]);
$postfields = array_merge($postfields, $formData); unset($postfields["submit"]);
unset($postfields["password2verify"]);
unset($postfields["submit"]); $query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; $result = $this->makeRequest($credentials["url"], $query_string);
$result = $this->makeRequest($url, $query_string); if ($result["result"] == "error") {
$form = new Application_Form_BillingClient(); $this->setErrorMessage();
$this->view->form = $form; } else {
$form = new Application_Form_BillingClient();
$this->setSuccessMessage();
}
$this->view->form = $form;
} else {
$this->view->form = $form;
}
} else { } else {
$this->view->form = $form; $this->view->form = $form;
} }
@ -127,14 +153,11 @@ class BillingController extends Zend_Controller_Action {
public function invoicesAction() public function invoicesAction()
{ {
$accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; $credentials = self::getAPICredentials();
$username = $_SERVER["WHMCS_USERNAME"];
$password = $_SERVER["WHMCS_PASSWORD"];
$url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey;
$postfields = array(); $postfields = array();
$postfields["username"] = $username; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($password); $postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "getinvoices"; $postfields["action"] = "getinvoices";
$postfields["responsetype"] = "json"; $postfields["responsetype"] = "json";
$postfields["userid"] = 1846; $postfields["userid"] = 1846;
@ -143,7 +166,7 @@ class BillingController extends Zend_Controller_Action {
$query_string = ""; $query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$result = self::makeRequest($url, $query_string); $result = self::makeRequest($credentials["url"], $query_string);
$this->view->invoices = $result["invoices"]["invoice"]; $this->view->invoices = $result["invoices"]["invoice"];
} }
@ -157,14 +180,11 @@ class BillingController extends Zend_Controller_Action {
//TODO: this does not return a service id. why? //TODO: this does not return a service id. why?
private static function getClientInstanceId() private static function getClientInstanceId()
{ {
$accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; $credentials = self::getAPICredentials();
$username = $_SERVER["WHMCS_USERNAME"];
$password = $_SERVER["WHMCS_PASSWORD"];
$url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey;
$postfields = array(); $postfields = array();
$postfields["username"] = $username; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($password); $postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "getclientsproducts"; $postfields["action"] = "getclientsproducts";
$postfields["responsetype"] = "json"; $postfields["responsetype"] = "json";
$postfields["clientid"] = 1846; $postfields["clientid"] = 1846;
@ -173,42 +193,37 @@ class BillingController extends Zend_Controller_Action {
$query_string = ""; $query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$result = self::makeRequest($url, $query_string); $result = self::makeRequest($credentials["url"], $query_string);
Logging::info($result); Logging::info($result);
} }
public static function getProducts() public static function getProducts()
{ {
$accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; $credentials = self::getAPICredentials();
$username = $_SERVER["WHMCS_USERNAME"];
$password = $_SERVER["WHMCS_PASSWORD"];
$url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey;
$postfields = array(); $postfields = array();
$postfields["username"] = $username; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($password); $postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "getproducts"; $postfields["action"] = "getproducts";
$postfields["responsetype"] = "json"; $postfields["responsetype"] = "json";
//gid is the Airtime product group id on whmcs
$postfields["gid"] = "15"; $postfields["gid"] = "15";
$query_string = ""; $query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$result = self::makeRequest($url, $query_string); $result = self::makeRequest($credentials["url"], $query_string);
return $result["products"]["product"]; return $result["products"]["product"];
} }
public static function getClientDetails() public static function getClientDetails()
{ {
try { try {
$accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; $credentials = self::getAPICredentials();
$username = $_SERVER["WHMCS_USERNAME"];
$password = $_SERVER["WHMCS_PASSWORD"];
$url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey;
$postfields = array(); $postfields = array();
$postfields["username"] = $username; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($password); $postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "getclientsdetails"; $postfields["action"] = "getclientsdetails";
$postfields["stats"] = true; $postfields["stats"] = true;
//$postfields["clientid"] = Application_Model_Preference::GetClientId(); //$postfields["clientid"] = Application_Model_Preference::GetClientId();
@ -218,7 +233,7 @@ class BillingController extends Zend_Controller_Action {
$query_string = ""; $query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$arr = self::makeRequest($url, $query_string); $arr = self::makeRequest($credentials["url"], $query_string);
return $arr["client"]; return $arr["client"];
} catch (Exception $e) { } catch (Exception $e) {
Logging::info($e->getMessage()); Logging::info($e->getMessage());

View File

@ -1,3 +1,8 @@
<div class="ui-widget ui-widget-content block-shadow clearfix padded-strong"> <div class="ui-widget ui-widget-content block-shadow clearfix padded-strong">
<?php if (isset($this->errorMessage)) {?>
<div class="errors"><?php echo $this->errorMessage ?></div>
<?php } else if (isset($this->successMessage)) {?>
<div class="success"><?php echo $this->successMessage ?></div>
<?php }?>
<?php echo $this->form ?> <?php echo $this->form ?>
</div> </div>