Articles on: Developer

Updating Custom Code for Billing Cycles

Before Clientexec 6.2.0, the billing cycle values used to be the amount of months of the respective billing cycle for product configurations, addons, and recurring fees, except for Domain products configuration which the billing cycle values used to be the amount of years of the respective billing cycle.

Example: 1 year value was 12 for Hosting, General and SSL products configurations, addons, and recurring fees, but was 1 for Domain product configuration.

Starting from Clientexec 6.2.0, the billing cycles now have their own id, independent of their amount of months or years. More details can be found here.

However, for compatibility reasons, the ids of the default billing cycles included with the installer are using as id the same number as their respective amount of months.

Example: 1 year value is 12 for Hosting, General, SSL, and even Domain products configurations, addons, and recurring fees.

Take into account that the ids can be any value, so, if the 1 Year billing cycle is deleted and then recreated, the id will no longer be 12, and can be any value like 121, etc

If having custom plugins or code, that is making use of billing cycles, it should continue working for the default billing cycles provided.

However, if the code is related to the Domain products configuration, their billing cycles used to be in years, but now the ids of the default billing cycles included with the installer are using as id the same number as their respective amount of months, so that custom code wi need to be reviewed to make sure it uses the right values.

Depending on what the custom code is doing for the domain, it will require to replace some values like 1 with 12, 2 with 24, 3 with 36, etc. But not everything needs to be replaced, so review the logic of the custom code with care.

In order to start using custom billing cycles, or if one of the default billing cycles provided by the installer has been removed and later added back (causing it to use a totally different id than the default one provided by the installer), then the custom code will need to be updated to properly work with the new billing cycles system.

The following PHP code will allow to get the full list of the available billing cycles and create an array with them all, having as key the respective billing cycle id, and as value an array having the billing cycle name, time_unit and amount_of_units:

//Billing Cycles
$billingcycles = array();
include_once 'modules/billing/models/BillingCycleGateway.php';
$gateway = new BillingCycleGateway();
$iterator = $gateway->getBillingCycles(array(), array('order_value', 'ASC'));
while ($cycle = $iterator->fetch()) {
  //time_unit can be 'd' for days. 'w' for weeks, 'm' for months, and 'y' for years
  $billingcycles[$cycle->id] = array(
  'name' => $this->user->lang($cycle->name),
  'time_unit' => $cycle->time_unit,
  'amount_of_units' => $cycle->amount_of_units
  );
}
//Billing Cycles


With that code, assuming the value of the billing cycle id is already set in a variable like $billing_cycle_id, it is possible to get the information of that billing cycle, like this:

$billing_cycle_name = $billingcycles[$billing_cycle_id]['name'];

//time_unit can be 'd' for days. 'w' for weeks, 'm' for months, and 'y' for years
$billing_cycle_time_unit = $billingcycles[$billing_cycle_id]['time_unit'];

$billing_cycle_amount_of_units = $billingcycles[$billing_cycle_id]['amount_of_units'];


If needed, it is also possible to create an array having as key a combination of amount_of_units and time_unit, and as value the id of the billing cycle. That way, it can be easy to get the id for a give billing cycle:

//Billing Cycles
$billingcyclesids = array();
include_once 'modules/billing/models/BillingCycleGateway.php';
$gateway = new BillingCycleGateway();
$iterator = $gateway->getBillingCycles(array(), array('order_value', 'ASC'));
while ($cycle = $iterator->fetch()) {
  $billingcyclesids[$cycle->amount_of_units.$cycle->time_unit] = $cycle->id;
}
//Billing Cycles


With that code, it is possible to get the id of a billing cycle, like this:

//1 Day billing cycle
$id_1_day = $billingcyclesids['1d'];

//2 Days billing cycle
$id_2_days = $billingcyclesids['2d'];

//1 Week billing cycle
$id_1_week = $billingcyclesids['1w'];

//2 Weeks billing cycle
$id_2_weeks = $billingcyclesids['2w'];

//1 Month billing cycle
$id_1_month = $billingcyclesids['1m'];

//2 Months billing cycle
$id_2_months = $billingcyclesids['2m'];

//1 Year billing cycle
$id_1_year = $billingcyclesids['1y'];

//2 Years billing cycle
$id_2_years = $billingcyclesids['2y'];

Updated on: 27/02/2023

Was this article helpful?

Share your feedback

Cancel

Thank you!