Merge branch 'saas-showbuilder' of github.com:sourcefabric/airtime into saas-showbuilder

This commit is contained in:
Duncan Sommerville 2015-08-25 17:09:17 -04:00
commit afa3b47e42
19 changed files with 422 additions and 104 deletions

View file

@ -10,6 +10,7 @@ class BillingController extends Zend_Controller_Action {
//Two of the actions in this controller return JSON because they're used for AJAX:
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('vat-validator', 'json')
->addActionContext('promo-eligibility-check', 'json')
->addActionContext('is-country-in-eu', 'json')
->initContext();
}
@ -19,6 +20,33 @@ class BillingController extends Zend_Controller_Action {
$this->_redirect('billing/upgrade');
}
public function promoEligibilityCheckAction()
{
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
if (!$request->isPost()) {
throw new Exception("Must POST data to promoEligibilityCheckAction.");
}
$data = $request->getPost();
$current_namespace = new Zend_Session_Namespace('csrf_namespace');
$observed_csrf_token = $this->_getParam('csrf_token');
$expected_csrf_token = $current_namespace->authtoken;
if($observed_csrf_token == $expected_csrf_token) {
$eligible = Billing::isClientEligibleForPromo(
$data["newproductid"], $data["newproductbillingcycle"]);
//Set the return JSON value
$this->_helper->json(array("result"=>$eligible));
} else {
$this->getResponse()->setHttpResponseCode(403);
$this->_helper->json(array("result"=>false, "error"=>"CSRF token did not match."));
}
}
public function upgradeAction()
{
@ -31,21 +59,32 @@ class BillingController extends Zend_Controller_Action {
$request = $this->getRequest();
$form = new Application_Form_BillingUpgradeDowngrade();
if ($request->isPost()) {
$formData = $request->getPost();
if ($form->isValid($formData)) {
// Check if client is eligible for promo and update the new product id if so
$eligibleForPromo = Billing::isClientEligibleForPromo(
$formData["newproductid"], $formData["newproductbillingcycle"]);
if ($eligibleForPromo) {
$newProductName = Billing::getProductName($formData["newproductid"]);
$formData["newproductid"] = Billing::getEligibleAwesomeAugustPromoPlanId($newProductName);
}
$credentials = Billing::getAPICredentials();
//Check if VAT should be applied or not to this invoice.
if (in_array("7", $formData["customfields"])) {
$apply_vat = Billing::checkIfVatShouldBeApplied($formData["customfields"]["7"], $formData["country"]);
} else {
$apply_vat = false;
}
$placeAnUpgradeOrder = true;
$currentPlanProduct = Billing::getClientCurrentAirtimeProduct();
$currentPlanProductId = $currentPlanProduct["pid"];
$currentPlanProductBillingCycle = strtolower($currentPlanProduct["billingcycle"]);
@ -54,33 +93,33 @@ class BillingController extends Zend_Controller_Action {
//and it freaks out and does the wrong thing if we do it via the API
//so we have to do avoid that.
if (($currentPlanProductId == $formData["newproductid"]) &&
($currentPlanProductBillingCycle == $formData["newproductbillingcycle"]))
{
($currentPlanProductBillingCycle == $formData["newproductbillingcycle"])
) {
$placeAnUpgradeOrder = false;
}
$postfields = array();
$postfields["username"] = $credentials["username"];
$postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "upgradeproduct";
$postfields["clientid"] = Application_Model_Preference::GetClientId();
$postfields["serviceid"] = Billing::getClientInstanceId();
$postfields["type"] = "product";
$postfields["newproductid"] = $formData["newproductid"];
$postfields["newproductbillingcycle"] = $formData["newproductbillingcycle"];
$postfields["paymentmethod"] = $formData["paymentmethod"];
$postfields["responsetype"] = "json";
$upgrade_query_string = "";
foreach ($postfields AS $k=>$v) $upgrade_query_string .= "$k=".urlencode($v)."&";
foreach ($postfields AS $k => $v) $upgrade_query_string .= "$k=" . urlencode($v) . "&";
//update client info
$clientfields = array();
$clientfields["username"] = $credentials["username"];
$clientfields["password"] = md5($credentials["password"]);
$clientfields["action"] = "updateclient";
$clientfields["action"] = "updateclient";
$clientfields["clientid"] = Application_Model_Preference::GetClientId();
$clientfields["customfields"] = base64_encode(serialize($formData["customfields"]));
unset($formData["customfields"]);
@ -93,8 +132,8 @@ class BillingController extends Zend_Controller_Action {
unset($clientfields["password2verify"]);
unset($clientfields["submit"]);
$client_query_string = "";
foreach ($clientfields AS $k=>$v) $client_query_string .= "$k=".urlencode($v)."&";
foreach ($clientfields AS $k => $v) $client_query_string .= "$k=" . urlencode($v) . "&";
//Update the client details in WHMCS first
$result = Billing::makeRequest($credentials["url"], $client_query_string);
Logging::info($result);
@ -103,33 +142,39 @@ class BillingController extends Zend_Controller_Action {
$this->view->form = $form;
return;
}
//If there were no changes to the plan or billing cycle, we just redirect you to the
//invoices screen and show a message.
if (!$placeAnUpgradeOrder)
{
if (!$placeAnUpgradeOrder) {
$this->_redirect('billing/invoices?planupdated');
return;
}
//Then place an upgrade order in WHMCS
$result = Billing::makeRequest($credentials["url"], $upgrade_query_string);
if ($result["result"] == "error") {
Logging::info($_SERVER['HTTP_HOST']." - Account upgrade failed. - ".$result["message"]);
Logging::info($_SERVER['HTTP_HOST'] . " - Account upgrade failed. - " . $result["message"]);
$this->setErrorMessage();
$this->view->form = $form;
} else {
Logging::info($_SERVER['HTTP_HOST']. "Account plan upgrade request:");
Logging::info($_SERVER['HTTP_HOST'] . "Account plan upgrade request:");
Logging::info($result);
// Disable the view and the layout here, squashes an error.
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
if ($apply_vat) {
Billing::addVatToInvoice($result["invoiceid"]);
}
self::viewInvoice($result["invoiceid"]);
// there may not be an invoice created if the client is downgrading
if (!empty($result["invoiceid"])) {
self::viewInvoice($result["invoiceid"]);
} else {
$this->_redirect('billing/invoices?planupdated');
return;
}
}
} else {
$this->view->form = $form;