From 1e1dc1dd03daabaa6a9dd6654a0eccab8e93df13 Mon Sep 17 00:00:00 2001 From: drigato Date: Fri, 20 Jun 2014 16:31:26 -0400 Subject: [PATCH] Validation Refactoring --- .../controllers/BillingController.php | 117 ++++++++++-------- .../views/scripts/billing/client.phtml | 5 + 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/airtime_mvc/application/controllers/BillingController.php b/airtime_mvc/application/controllers/BillingController.php index 91a06b80d..50af88b03 100644 --- a/airtime_mvc/application/controllers/BillingController.php +++ b/airtime_mvc/application/controllers/BillingController.php @@ -55,7 +55,7 @@ class BillingController extends Zend_Controller_Action { $result = $this->makeRequest($credentials["url"], $client_query_string); 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; } else { //$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() { return array( - "access_key" => $_SERVER["WHMCS_ACCESS_KEY"], "username" => $_SERVER["WHMCS_USERNAME"], "password" => $_SERVER["WHMCS_PASSWORD"], "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(); if ($request->isPost()) { $formData = $request->getPost(); + if ($form->isValid($formData)) { - $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; - $username = $_SERVER["WHMCS_USERNAME"]; - $password = $_SERVER["WHMCS_PASSWORD"]; - $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; - - $postfields = array(); - $postfields["username"] = $username; - $postfields["password"] = md5($password); - $postfields["action"] = "updateclient"; - //$postfields["clientid"] = Application_Model_Preference::GetClientId(); - $postfields["clientid"] = 1846; - $postfields = array_merge($postfields, $formData); - unset($postfields["password2verify"]); - unset($postfields["submit"]); - - $query_string = ""; - foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; - - $result = $this->makeRequest($url, $query_string); - $form = new Application_Form_BillingClient(); - $this->view->form = $form; + $credentials = self::getAPICredentials(); + + $postfields = array(); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); + $postfields["action"] = "updateclient"; + //$postfields["clientid"] = Application_Model_Preference::GetClientId(); + $postfields["clientid"] = 18460000000; + $postfields["responsetype"] = "json"; + $postfields = array_merge($postfields, $formData); + unset($postfields["password2verify"]); + unset($postfields["submit"]); + + $query_string = ""; + foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; + + $result = $this->makeRequest($credentials["url"], $query_string); + + if ($result["result"] == "error") { + $this->setErrorMessage(); + } else { + $form = new Application_Form_BillingClient(); + $this->setSuccessMessage(); + } + + $this->view->form = $form; + } else { + $this->view->form = $form; + } } else { $this->view->form = $form; } @@ -127,14 +153,11 @@ class BillingController extends Zend_Controller_Action { public function invoicesAction() { - $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; - $username = $_SERVER["WHMCS_USERNAME"]; - $password = $_SERVER["WHMCS_PASSWORD"]; - $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; + $credentials = self::getAPICredentials(); $postfields = array(); - $postfields["username"] = $username; - $postfields["password"] = md5($password); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); $postfields["action"] = "getinvoices"; $postfields["responsetype"] = "json"; $postfields["userid"] = 1846; @@ -143,7 +166,7 @@ class BillingController extends Zend_Controller_Action { $query_string = ""; 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"]; } @@ -157,14 +180,11 @@ class BillingController extends Zend_Controller_Action { //TODO: this does not return a service id. why? private static function getClientInstanceId() { - $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; - $username = $_SERVER["WHMCS_USERNAME"]; - $password = $_SERVER["WHMCS_PASSWORD"]; - $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; + $credentials = self::getAPICredentials(); $postfields = array(); - $postfields["username"] = $username; - $postfields["password"] = md5($password); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); $postfields["action"] = "getclientsproducts"; $postfields["responsetype"] = "json"; $postfields["clientid"] = 1846; @@ -173,42 +193,37 @@ class BillingController extends Zend_Controller_Action { $query_string = ""; 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); } public static function getProducts() { - $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; - $username = $_SERVER["WHMCS_USERNAME"]; - $password = $_SERVER["WHMCS_PASSWORD"]; - $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; + $credentials = self::getAPICredentials(); $postfields = array(); - $postfields["username"] = $username; - $postfields["password"] = md5($password); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); $postfields["action"] = "getproducts"; $postfields["responsetype"] = "json"; + //gid is the Airtime product group id on whmcs $postfields["gid"] = "15"; $query_string = ""; 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"]; } public static function getClientDetails() { try { - $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; - $username = $_SERVER["WHMCS_USERNAME"]; - $password = $_SERVER["WHMCS_PASSWORD"]; - $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; + $credentials = self::getAPICredentials(); $postfields = array(); - $postfields["username"] = $username; - $postfields["password"] = md5($password); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); $postfields["action"] = "getclientsdetails"; $postfields["stats"] = true; //$postfields["clientid"] = Application_Model_Preference::GetClientId(); @@ -218,7 +233,7 @@ class BillingController extends Zend_Controller_Action { $query_string = ""; 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"]; } catch (Exception $e) { Logging::info($e->getMessage()); diff --git a/airtime_mvc/application/views/scripts/billing/client.phtml b/airtime_mvc/application/views/scripts/billing/client.phtml index d3850cfc2..980288c81 100644 --- a/airtime_mvc/application/views/scripts/billing/client.phtml +++ b/airtime_mvc/application/views/scripts/billing/client.phtml @@ -1,3 +1,8 @@
+errorMessage)) {?> +
errorMessage ?>
+successMessage)) {?> +
successMessage ?>
+ form ?>
\ No newline at end of file