SAAS-973: Airtime Billing page - Add support for August promotion plans

Backend side pretty much done
This commit is contained in:
drigato 2015-07-29 22:24:17 -04:00
parent 4d9fb27554
commit 4ed87de183
3 changed files with 103 additions and 29 deletions

View File

@ -1,5 +1,7 @@
<?php <?php
define("AIRTIME_PRO_FREE_TRIAL_PLAN_ID", 34);
class Billing class Billing
{ {
public static function getAPICredentials() public static function getAPICredentials()
@ -55,7 +57,6 @@ class Billing
//Blacklist all free plans //Blacklist all free plans
foreach ($products as $k => $p) { foreach ($products as $k => $p) {
Logging::info($p);
if ($p["paytype"] === "free") if ($p["paytype"] === "free")
{ {
unset($products[$k]); unset($products[$k]);
@ -326,4 +327,46 @@ class Billing
$result = Billing::makeRequest($credentials["url"], $query_string); $result = Billing::makeRequest($credentials["url"], $query_string);
} }
public static function isClientEligibleForPromo($newProductId, $newProductBillingCycle)
{
$currentPaidPlanProductIds = array(
"25",
"26",
"27",
"28"
);
$currentPlanProduct = self::getClientCurrentAirtimeProduct();
$currentPlanProductId = $currentPlanProduct["pid"];
$currentPlanBillingCycle = strtolower($currentPlanProduct["billingcycle"]);
// if client is on trial plan, YES
if ($currentPlanProductId == AIRTIME_PRO_FREE_TRIAL_PLAN_ID) {
return true;
}
// if client is currently on monthly or old plan AND (upgrading OR upgrading/downgrading to annual plan), YES
if ($currentPlanBillingCycle == "monthly" || $currentPlanBillingCycle == "free account") {
// is the client changing billing cycle to annual?
if ($newProductBillingCycle == "annually") {
return true;
}
// Is the client staying on monthly and upgrading?
// This won't hold true if the client is on an old plan because the old
// plan ids are higher than the current plan ids.
if ($newProductBillingCycle == "monthly" && $newProductId > $currentPlanProductId) {
return true;
}
// Is the client staying on monthly and upgrading from an old plan?
if ($newProductBillingCycle == "monthly" && !in_array($currentPlanProductId, $currentPaidPlanProductIds)
&& in_array($newProductId, $currentPaidPlanProductIds)) {
return true;
}
}
return false;
}
} }

View File

@ -30,10 +30,12 @@ class BillingController extends Zend_Controller_Action {
throw new Exception("Must POST data to promoEligibilityCheckAction."); throw new Exception("Must POST data to promoEligibilityCheckAction.");
} }
$data = $request->getPost(); $data = $request->getPost();
Logging::info($data);
$eligible = Billing::isClientEligibleForPromo(
$data["newproductid"], $data["newproductbillingcycle"]);
//Set the return JSON value //Set the return JSON value
$this->_helper->json(array("result"=>"ok")); $this->_helper->json(array("result"=>$eligible));
} }
public function upgradeAction() public function upgradeAction()
@ -46,24 +48,42 @@ class BillingController extends Zend_Controller_Action {
$request = $this->getRequest(); $request = $this->getRequest();
$form = new Application_Form_BillingUpgradeDowngrade(); $form = new Application_Form_BillingUpgradeDowngrade();
if ($request->isPost()) { if ($request->isPost()) {
// for testing
$doUpgrade = false; $doUpgrade = false;
$formData = $request->getPost(); $formData = $request->getPost();
Logging::info($formData);
if ($doUpgrade && $form->isValid($formData)) { if ($doUpgrade && $form->isValid($formData)) {
// for testing
Logging::info("---upgrading----"); Logging::info("---upgrading----");
// Maps current paid plans to the promo plan ids
// TODO: update these to the correct values
$promoPlanIdMap = array(
"25" => "50", //hobbyist
"26" => "51", //starter
"27" => "52", //plus
"28" => "53" //premium
);
// Check if client is eligible for promo and update the new product id if so
$eligibleForPromo = Billing::isClientEligibleForPromo(
$formData["newproductid"], $formData["newproductbillingcycle"]);
if ($eligibleForPromo) {
$formData["newproductid"] = $promoPlanIdMap[$formData["newproductid"]];
}
$credentials = Billing::getAPICredentials(); $credentials = Billing::getAPICredentials();
//Check if VAT should be applied or not to this invoice. //Check if VAT should be applied or not to this invoice.
if (in_array("7", $formData["customfields"])) { if (in_array("7", $formData["customfields"])) {
$apply_vat = Billing::checkIfVatShouldBeApplied($formData["customfields"]["7"], $formData["country"]); $apply_vat = Billing::checkIfVatShouldBeApplied($formData["customfields"]["7"], $formData["country"]);
} else { } else {
$apply_vat = false; $apply_vat = false;
} }
$placeAnUpgradeOrder = true; $placeAnUpgradeOrder = true;
$currentPlanProduct = Billing::getClientCurrentAirtimeProduct(); $currentPlanProduct = Billing::getClientCurrentAirtimeProduct();
$currentPlanProductId = $currentPlanProduct["pid"]; $currentPlanProductId = $currentPlanProduct["pid"];
$currentPlanProductBillingCycle = strtolower($currentPlanProduct["billingcycle"]); $currentPlanProductBillingCycle = strtolower($currentPlanProduct["billingcycle"]);
@ -76,29 +96,29 @@ class BillingController extends Zend_Controller_Action {
{ {
$placeAnUpgradeOrder = false; $placeAnUpgradeOrder = false;
} }
$postfields = array(); $postfields = array();
$postfields["username"] = $credentials["username"]; $postfields["username"] = $credentials["username"];
$postfields["password"] = md5($credentials["password"]); $postfields["password"] = md5($credentials["password"]);
$postfields["action"] = "upgradeproduct"; $postfields["action"] = "upgradeproduct";
$postfields["clientid"] = Application_Model_Preference::GetClientId(); $postfields["clientid"] = Application_Model_Preference::GetClientId();
$postfields["serviceid"] = Billing::getClientInstanceId(); $postfields["serviceid"] = Billing::getClientInstanceId();
$postfields["type"] = "product"; $postfields["type"] = "product";
$postfields["newproductid"] = $formData["newproductid"]; $postfields["newproductid"] = $formData["newproductid"];
$postfields["newproductbillingcycle"] = $formData["newproductbillingcycle"]; $postfields["newproductbillingcycle"] = $formData["newproductbillingcycle"];
$postfields["paymentmethod"] = $formData["paymentmethod"]; $postfields["paymentmethod"] = $formData["paymentmethod"];
$postfields["responsetype"] = "json"; $postfields["responsetype"] = "json";
$upgrade_query_string = ""; $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 //update client info
$clientfields = array(); $clientfields = array();
$clientfields["username"] = $credentials["username"]; $clientfields["username"] = $credentials["username"];
$clientfields["password"] = md5($credentials["password"]); $clientfields["password"] = md5($credentials["password"]);
$clientfields["action"] = "updateclient"; $clientfields["action"] = "updateclient";
$clientfields["clientid"] = Application_Model_Preference::GetClientId(); $clientfields["clientid"] = Application_Model_Preference::GetClientId();
$clientfields["customfields"] = base64_encode(serialize($formData["customfields"])); $clientfields["customfields"] = base64_encode(serialize($formData["customfields"]));
unset($formData["customfields"]); unset($formData["customfields"]);
@ -111,8 +131,8 @@ class BillingController extends Zend_Controller_Action {
unset($clientfields["password2verify"]); unset($clientfields["password2verify"]);
unset($clientfields["submit"]); unset($clientfields["submit"]);
$client_query_string = ""; $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 //Update the client details in WHMCS first
$result = Billing::makeRequest($credentials["url"], $client_query_string); $result = Billing::makeRequest($credentials["url"], $client_query_string);
Logging::info($result); Logging::info($result);
@ -121,29 +141,28 @@ class BillingController extends Zend_Controller_Action {
$this->view->form = $form; $this->view->form = $form;
return; return;
} }
//If there were no changes to the plan or billing cycle, we just redirect you to the //If there were no changes to the plan or billing cycle, we just redirect you to the
//invoices screen and show a message. //invoices screen and show a message.
if (!$placeAnUpgradeOrder) if (!$placeAnUpgradeOrder) {
{
$this->_redirect('billing/invoices?planupdated'); $this->_redirect('billing/invoices?planupdated');
return; return;
} }
//Then place an upgrade order in WHMCS //Then place an upgrade order in WHMCS
$result = Billing::makeRequest($credentials["url"], $upgrade_query_string); $result = Billing::makeRequest($credentials["url"], $upgrade_query_string);
if ($result["result"] == "error") { 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->setErrorMessage();
$this->view->form = $form; $this->view->form = $form;
} else { } else {
Logging::info($_SERVER['HTTP_HOST']. "Account plan upgrade request:"); Logging::info($_SERVER['HTTP_HOST'] . "Account plan upgrade request:");
Logging::info($result); Logging::info($result);
// Disable the view and the layout here, squashes an error. // Disable the view and the layout here, squashes an error.
$this->view->layout()->disableLayout(); $this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true); $this->_helper->viewRenderer->setNoRender(true);
if ($apply_vat) { if ($apply_vat) {
Billing::addVatToInvoice($result["invoiceid"]); Billing::addVatToInvoice($result["invoiceid"]);
} }

View File

@ -122,14 +122,23 @@ function configureByCountry(countryCode)
function promoEligibilityCheck() function promoEligibilityCheck()
{ {
var newproductid = $("input[type='radio'][name='newproductid']:checked"); var newproductid = $("input[type='radio'][name='newproductid']:checked").val();
var newproductbillingcycle = $("input[type='radio'][name='newproductbillingcycle']:checked");
console.log(newproductid.val()); // newproductid can be undefined if the client is currently on an old plan
console.log(newproductbillingcycle.val()); // and they just change the billing cycle value without selecting a new plan type.
// In this case, let's not check if they are eligible for the promo because
// they won't be able to upgrade without selecting a new plan first.
if (newproductid === undefined) {
return;
}
var newproductbillingcycle = $("input[type='radio'][name='newproductbillingcycle']:checked").val();
$.post("/billing/promo-eligibility-check", {"newproductid": newproductid, $.post("/billing/promo-eligibility-check", {"newproductid": newproductid,
"newproductbillingcycle": newproductbillingcycle}) "newproductbillingcycle": newproductbillingcycle})
.success(function(data) { .success(function(data) {
console.log(data); if (data.result == true) {
$("#promo-plan-eligible").show();
}
}); });
} }
@ -137,7 +146,7 @@ $(document).ready(function() {
configureByCountry($("#country").val()); configureByCountry($("#country").val());
recalculateTotals(); recalculateTotals();
$("input[name='newproductid']").change(function() { $("input[name='newproductid']").change(function() {
validatePlan(); validatePlan();
recalculateTotals(); recalculateTotals();
@ -289,6 +298,9 @@ echo($currentProduct["name"]);
<div id="billingcycle_disclaimer"> <div id="billingcycle_disclaimer">
Save 15% on annual plans (Hobbyist plan excluded). Save 15% on annual plans (Hobbyist plan excluded).
</div> </div>
<div id="promo-plan-eligible" style="display:none;">
Congratulations, you are eligible for our Awesome Promotional Plan!
</div>
<div class="clearfix"></div> <div class="clearfix"></div>
<div id="subtotal_box"> <div id="subtotal_box">
<b>Subtotal:</b><br> <b>Subtotal:</b><br>