License Plugin for Boxbilling – How to

Well , BoxBilling is a billing and automation software being popular nowadays as it is free .They also have commercial licenses,if you are intend to add more than 5 products to the system.The first look an feel is like you are dealing with a hosting automation and management software like HostBill , WHMCS .but BoxBilling has a long run before it can catch up with those softwares.Boxbilling is like a well-written software by a person instead of being a supported software by a company or a group or a community,thus no expected support when you compare it to other market leading softwares.Their documentation page is not organized enough with poor content . Their plugin page never works and nobody seem to care about this.And the nightmare!Their software is encoded!!!Imagine how hard it would be to extend this software for your needs.

But after all , I was looking for a software with simple installation and minimal configuration to get my website up and running fast and BoxBilling was emphatically the best option.

Today I will not try to compare BoxBilling with other softwares in the market ,but explaining how you can write your own plugin for licenses in BoxBilling which you may need someday .

BoxBilling default license plugin was designed to generate license keys and validate them with an IP address if required,But my goal was a little bit different . I had licenses already generated and stored in database.I was looking for a plugin to retrieve it and deliver it to my clients . Here i will show you to do so in easiest way .

Again looking into BoxBilling documentation page for licenses would not do the trick so I decided to take a gander into the default plugin .

this is the default plugin :


<?php
/**
 * Default license generation script
 */
class License_Default
{
 /**
 * License generation script
 *
 * @param Model_ServiceLicense $service
 * @param Model_ClientOrder $order
 * @param array $config
 * @return string
 */
 public function generate(Model_ServiceLicense $service, Model_ClientOrder $order, array $config)
 {
 $length = isset($config['length']) ? $config['length'] : 25;
 $prefix = isset($config['prefix']) ? $config['prefix'] : NULL;

 $character_array = array_merge(range('A', 'Z'), range(1, 9));
 $size = count($character_array) - 1;
 $string = '';
 for($i = 1; $i < $length; $i++) {
 $string .= ($i % 5 == 0) ? '-' : $character_array[rand(0, $size)];
 }
 return $prefix . $string;
 }

 /**
 * This method is optional.
 * Aditional validation rules can be applied to license validation logic.
 * Method is called after "expiration, ip, version, path, hostname validations are passed"
 *
 * Should throw "LogicException" if validation fails
 *
 * @param Model_ServiceLicense $service
 * @param array $data
 * @return array - list of params to be attached to response message
 * @throws LogicException
 */
 /*
 public function validate(Model_ServiceLicense $service, array $data)
 {
 if(!$validation_rule) {
 throw new LogicException('Some validation rule did not pass', 1020);
 }

 return array(
 'key' => 'value',
 'key2' => 'value2'
 );
 }
 */
}

As you can see the main function for generating is “generate()” function which gets “Model_ServiceLicense” , “Model_ClientOrder” and an array for setting(such validating IP) arguments.In our case we wont use any other settings for our plugin because we just need to get license from a database table and return it to the user .
First of all we get rid of the second function . And we just simply put our code in the generate() function . The final code is something like this :

<?php
/**
 * Default license generation script
 */
class License_TESTLICENSE
{
 public function generate(Model_ServiceLicense $service, Model_ClientOrder $order)
 {
 $con=mysqli_connect("localhost","DB_USER","DB_PASS","DB");
 if (mysqli_connect_errno())
 {
 $errxprox = rand()." Error-Contact Support - plugin:ERx01";
 return $errxprox;
 }
 else
 {
 $result = mysqli_query($con,"SELECT code FROM TABLENAME Where status='0' LIMIT 1");
 if (mysqli_num_rows($result) == 0)
 {
 $errxpro = rand()." Error-Contact Support - plugin:ERx02";
 return $errxpro;
 }
 else
 {
 while($row = mysqli_fetch_array($result))
 {
 $generated = $row['code'];
 }
 mysqli_query($con,"UPDATE TABELNAME SET status=1 WHERE code='".$generated."'");
 return $generated;
 mysqli_close($con);
 }
 }

 }
}

In this code we first make a connection to database and if there is no error we will pick a license which has a 0 (unused) status.After picking that one we will change it to status 1 (used) and return the value of license to BoxBilling.
The only thing you should be careful with is in line 5 : you should name your license plugin in the class as :

class License_LICENSENAME

which in our case is “TestLicense”.

You should also save this file as “LICENSENAME.php”(in our case:”TestLicense.php”)  and put it in :

BoxBillingHome/bb-library/License 

folder.

That is it !

You have your new plugin .

 

This entry was posted by Matin on Sunday, June 29th, 2014 at 7:53 pm and is filed under Tutorials, Web/HTTP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.