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

Un-hardcode product ids
This commit is contained in:
drigato 2015-07-30 09:39:50 -04:00
parent 4ed87de183
commit d9e2ba0ed3
2 changed files with 91 additions and 25 deletions

View File

@ -32,10 +32,10 @@ class Billing
{
//Making this static to cache the products during a single HTTP request.
//This saves us roundtrips to WHMCS if getProducts() is called multiple times.
static $result = array();
if (!empty($result))
static $products = array();
if (!empty($products))
{
return $result["products"]["product"];
return $products;
}
$credentials = self::getAPICredentials();
@ -56,8 +56,9 @@ class Billing
$products = $result["products"]["product"];
//Blacklist all free plans
//Hide the promo plans - we will tell the user if they are eligible for a promo plan
foreach ($products as $k => $p) {
if ($p["paytype"] === "free")
if ($p["paytype"] === "free" || strpos($p["name"], "Awesome August 2015") !== false)
{
unset($products[$k]);
}
@ -327,14 +328,91 @@ class Billing
$result = Billing::makeRequest($credentials["url"], $query_string);
}
/**
* Returns an array of the current Airtime Pro plan IDs.
* This excludes any old, free, promotional, or custom plans.
*/
public static function getCurrentPaidProductIds()
{
$products = self::getProducts();
$productIds = array();
foreach ($products as $k => $p) {
array_push($productIds, $p["pid"]);
}
return $productIds;
}
/**
* Returns an array of the Awesome August 2015 Promotional plans
*/
public static function getAwesomeAugustPromoProducts()
{
$credentials = self::getAPICredentials();
$postfields = array();
$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($credentials["url"], $query_string);
$promoProducts = $result["products"]["product"];
foreach ($promoProducts as $k => $p) {
if (strpos($p["name"], "Awesome August 2015") === false) {
unset($promoProducts[$k]);
}
}
return $promoProducts;
}
/**
* Returns the eligible promo plan ID that corresponds to the regular paid plan.
*
* i.e. if the client wants to upgrade to the Plus plan this function returns the
* plan id of the Awesome August 2015 Plus plan.
*/
public static function getEligibleAwesomeAugustPromoPlanId($productName)
{
$promoPlans = self::getAwesomeAugustPromoProducts();
$promoPlanId = "";
foreach($promoPlans as $k => $p) {
if (strpos($p["name"], $productName) !== false) {
$promoPlanId = $p["pid"];
break;
}
}
return $promoPlanId;
}
public static function getProductName($productId)
{
$products = self::getProducts();
$productName = "";
foreach($products as $k => $p) {
if ($p["pid"] == $productId) {
$productName = $p["name"];
break;
}
}
return $productName;
}
public static function isClientEligibleForPromo($newProductId, $newProductBillingCycle)
{
$currentPaidPlanProductIds = array(
"25",
"26",
"27",
"28"
);
// use this to check if client is upgrading from an old plan
$currentPaidPlanProductIds = self::getCurrentPaidProductIds();
$currentPlanProduct = self::getClientCurrentAirtimeProduct();
$currentPlanProductId = $currentPlanProduct["pid"];

View File

@ -48,29 +48,17 @@ class BillingController extends Zend_Controller_Action {
$request = $this->getRequest();
$form = new Application_Form_BillingUpgradeDowngrade();
if ($request->isPost()) {
// for testing
$doUpgrade = false;
$formData = $request->getPost();
if ($doUpgrade && $form->isValid($formData)) {
// for testing
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
);
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) {
$formData["newproductid"] = $promoPlanIdMap[$formData["newproductid"]];
$newProductName = Billing::getProductName($formData["newproductid"]);
$formData["newproductid"] = Billing::getEligibleAwesomeAugustPromoPlanId($newProductName);
}
$credentials = Billing::getAPICredentials();