Jump to content

LicenseAddon Verify Hook


mfoland

Recommended Posts

So I'm working with the licensing addon, and attempting to figure out the hook part..

 

add_hook('LicensingAddonVerify', 1, function($vars) {
    return [
        'Hash' => 'Hash Here',
    ];
}
);

The hash actually shows in the license how it should, but I'm trying to do it since I make several softwares, and not only one, so what I'm trying to do is something like

if ($productid == "prodID number") {
add_hook('LicensingAddonVerify', 1, function($vars) {
    return [
        'Hash' => 'Hash Here',
    ];
}
);
}
else -- run get different product

It doesn't retrieve the product id at all.. What am I missing here? The hash is basically there to tell the system if a new version has been released. 

 

Thanks

Link to comment
Share on other sites

  • WHMCS Staff

Hi @mfoland,

Based on your description, it sounds like something like the following may be useful for your needs:

<?php

use WHMCS\Service\Service;

add_hook('LicensingAddonVerify', 1, function($vars) {

    try {
        $service = Service::findorFail($vars['serviceid']);
        $productID = $service->packageId;
    } catch (\Exception $e) {
        $productID = 0;
    }

    switch ((int)$productID) {
        case 0:
            return [
                'Hash' => 'error',
            ];
            break;
        case 1:
            return [
                'Hash' => '1',
            ];
            break;
        case 2:
            return [
                'Hash' => '2',
            ];
            break;
        case 3:
            return [
                'Hash' => '3',
            ];
            break;
    }
});

Naturally, in the switch you'd need to replace 1, 2, 3 with your actual Product ID's. In my example, I use 0 to return an error (Product ID not found).

This sample is provided as-is with no guarantee or warranty however, I trust it provides you with the information you require for your customisations.

The documentation I used in this example was:

https://developers.whmcs.com/hooks-reference/addon/#licensingaddonverify
https://docs.whmcs.com/classes/7.6/WHMCS/Service/Service.html
http://php.net/manual/en/control-structures.switch.php
https://docs.whmcs.com/Using_Models

I hope this helps!

Link to comment
Share on other sites

40 minutes ago, WHMCS Peter said:

Hi @mfoland,

Based on your description, it sounds like something like the following may be useful for your needs:


<?php

use WHMCS\Service\Service;

add_hook('LicensingAddonVerify', 1, function($vars) {

    try {
        $service = Service::findorFail($vars['serviceid']);
        $productID = $service->packageId;
    } catch (\Exception $e) {
        $productID = 0;
    }

    switch ((int)$productID) {
        case 0:
            return [
                'Hash' => 'error',
            ];
            break;
        case 1:
            return [
                'Hash' => '1',
            ];
            break;
        case 2:
            return [
                'Hash' => '2',
            ];
            break;
        case 3:
            return [
                'Hash' => '3',
            ];
            break;
    }
});

Naturally, in the switch you'd need to replace 1, 2, 3 with your actual Product ID's. In my example, I use 0 to return an error (Product ID not found).

This sample is provided as-is with no guarantee or warranty however, I trust it provides you with the information you require for your customisations.

The documentation I used in this example was:

https://developers.whmcs.com/hooks-reference/addon/#licensingaddonverify
https://docs.whmcs.com/classes/7.6/WHMCS/Service/Service.html
http://php.net/manual/en/control-structures.switch.php
https://docs.whmcs.com/Using_Models

I hope this helps!

@WHMCS Peter could I assign multiple cases so I can do it for multiple product ID's since some are Owned and Monthly?

Thanks for your help! 🙂

Link to comment
Share on other sites

  • WHMCS Staff

Hi @mfoland,

Sure thing! As documented in the PHP Manual, this can be achieved like so:

Let's say you wanted cases 1, 2 and 3 to do the same thing:

switch ((int)$productID) {

    case 0:
        return [
            'Hash' => 'error',
        ];
        break;
    case 1:
    case 2:
    case 3:
        return [
            'Hash' => 'MySecretHash',
        ];
        break;
}

Here's that link again for reference 🙂 - http://php.net/manual/en/control-structures.switch.php

Link to comment
Share on other sites

@WHMCS Peter

While on the licensing topic... I have this code:

 

//--Check Addons validity
$addonarray = array();
$addonslist = explode('|',$results['addons']);
	foreach ($addonslist as $addons){
		$addondata = explode(';',$addons);
		$addonname = $addonnextduedate = $addonstatus = "";
	foreach ($addondata as $addon){
		$addonnvp = explode('=',$addon);
		if($addonnvp[0] == 'name') $addonname = $addonnvp[1];
		if($addonnvp[0] == 'nextduedate') $addonnextduedate = $addonnvp[1];
		if($addonnvp[0] == 'status') $addonstatus = $addonnvp[1];
}

$addonarray[$addonname] = array("nextduedate"=>$addonnextduedate,"status"=>$addonstatus);
$hisAddon = array("$addonname");
$arrlength = count($hisAddon);

for($x = 0; $x < $arrlength; $x++) {
    $addonFinal[] = $hisAddon[$x];
    $addonStat[]  = $addonstatus[$x];
 $hisAddon[$x] = $hisAddons;
//$hisAddons = "$hisAddon";
}}

//-- Branding Option Force 9/6/18.. Completed 9/8/2018.
$astat = str_replace("A", "Active", $addonStat[0]);
$bname = "Branding Removal";
$badd = $addonFinal[0];
	if ($badd == $bname && $astat == "Active") {
	$bReq2 = "N";
	$brandRemov2 = "Yes";
	} else {
	$bReq2 = "Y";
	$brandRemov2 = "No";
	}*/
//--Ends Force Branding
//--Do we have Support and Updates Allowed?
$lkey  = $license_key;
$key   = explode("-", $lkey);
$bstat = str_replace("A", "Active", $addonStat[0]);
$cname = "SupportAccess";
$cadd  = $addonFinal[0];
if ($cadd == $cname && $astat == "Active")
{
                $supportUpdates = "Yes";
} 
else
{
                $supportUpdates = "No";
}

What's going on, is If I suspend one of the addons they both show Not Allowed, and if I have one of them enabled, both are enabled. I was working with the license key portion to automatically say Monthly has Support Access (which would of been what the hash was for to get the updates). But why in the world would both be active or suspended, if one of them were and the other was an opposite status? This is so mind boggoling

 

Thanks!

Link to comment
Share on other sites

  • WHMCS Staff

Hi,

The first thing that stands out to me is this line:

$addonname = $addonnextduedate = $addonstatus = "";

That to me looks like it's reassigning more than 1 variable. I'd recommend stepping through your code, making sure each variable is returning exactly as you expect it to.

You can use something like die(var_dump($variable)); to achieve that.

Link to comment
Share on other sites

8 minutes ago, WHMCS Peter said:

Hi,

The first thing that stands out to me is this line:


$addonname = $addonnextduedate = $addonstatus = "";

That to me looks like it's reassigning more than 1 variable. I'd recommend stepping through your code, making sure each variable is returning exactly as you expect it to.

You can use something like die(var_dump($variable)); to achieve that.

@WHMCS Peter

Changed the lining to

$addonarray = array();
$addonslist = explode('|',$results['addons']);
	foreach ($addonslist as $addons){
		$addondata = explode(';',$addons);
		$addonname = $addonnextduedate = $addonstatus = "";
		die(var_dump($results[status]));
	foreach ($addondata as $addon){
		$addonnvp = explode('=',$addon);
		if($addonnvp[0] == 'name') $addonname = $addonnvp[1];
		if($addonnvp[0] == 'nextduedate') $addonnextduedate = $addonnvp[1];
		if($addonnvp[0] == 'status') $addonstatus = $addonnvp[1];
}

I went to do $addonname and it gave a 0 string. For the results[status] i'm getting Active, and when I try to do $addonstatus there's no data. Not sure why it's giving an issue.

 

Here's what my Addon Array looks like:

string(117) "name=SupportAccess;nextduedate=2019-09-09;status=Active|name=Branding Removal;nextduedate=0000-00-00;status=Suspended"

 

Edited by mfoland
Added addon array
Link to comment
Share on other sites

10 minutes ago, mfoland said:

@WHMCS Peter

Changed the lining to


$addonarray = array();
$addonslist = explode('|',$results['addons']);
	foreach ($addonslist as $addons){
		$addondata = explode(';',$addons);
		$addonname = $addonnextduedate = $addonstatus = "";
		die(var_dump($results[status]));
	foreach ($addondata as $addon){
		$addonnvp = explode('=',$addon);
		if($addonnvp[0] == 'name') $addonname = $addonnvp[1];
		if($addonnvp[0] == 'nextduedate') $addonnextduedate = $addonnvp[1];
		if($addonnvp[0] == 'status') $addonstatus = $addonnvp[1];
}

I went to do $addonname and it gave a 0 string. For the results[status] i'm getting Active, and when I try to do $addonstatus there's no data. Not sure why it's giving an issue.

 

Here's what my Addon Array looks like:


string(117) "name=SupportAccess;nextduedate=2019-09-09;status=Active|name=Branding Removal;nextduedate=0000-00-00;status=Suspended"

 

Here's what the actual Array looks like when I var dump addon array..

 

Array ( [SupportAccess] => Array ( [nextduedate] => 2019-09-09 [status] => Active ) ) Array ( [SupportAccess] => Array ( [nextduedate] => 2019-09-09 [status] => Active ) [Branding Removal] => Array ( [nextduedate] => 2019-03-12 [status] => Suspended ) ) 

That part knows what it's suppose to do, based on the above script, so not sure why I would be having the issue 😞

Link to comment
Share on other sites

  • WHMCS Staff

Hi @mfoland,

I'm seeing two array keys called "SupportAccess". If one is expired but another is Active, it's possible that one is overwriting the other.

Array ( 
    [SupportAccess] => Array ( 
        [nextduedate] => 2019-09-09 
        [status] => Active 
    )
) 
Array ( 
    [SupportAccess] => Array ( 
        [nextduedate] => 2019-09-09 
        [status] => Active 
    )
    [Branding Removal] => Array ( 
        [nextduedate] => 2019-03-12 
        [status] => Suspended 
    )
)

 

Link to comment
Share on other sites

3 hours ago, WHMCS Peter said:

Hi @mfoland,

I'm seeing two array keys called "SupportAccess". If one is expired but another is Active, it's possible that one is overwriting the other.


Array ( 
    [SupportAccess] => Array ( 
        [nextduedate] => 2019-09-09 
        [status] => Active 
    )
) 
Array ( 
    [SupportAccess] => Array ( 
        [nextduedate] => 2019-09-09 
        [status] => Active 
    )
    [Branding Removal] => Array ( 
        [nextduedate] => 2019-03-12 
        [status] => Suspended 
    )
)

 

@WHMCS Peter

Array ( [SupportAccess] => Array ( [nextduedate] => 2019-03-12 [status] => Active ) )

Now I have Branding Removal gone out of the license.. but it still shows ENABLED in my script

Link to comment
Share on other sites

  • WHMCS Staff

Hi @mfoland,

At this point, you would need to step through each part of your code performing var_dump($var) along the way. By doing this, you can ensure each part of your script is working as expected.

When you reach a point where a variable is returning one thing but should in fact be another, you'll narrow down the area in which the defect lies.

I'd recommend reaching out to a qualified PHP Developer if you are still uncertain. Unfortunately, our support scope is fairly limited.

Link to comment
Share on other sites

Well here's the array... 

array(3) {
  [0]=>
  string(18) "name=SupportAccess"
  [1]=>
  string(22) "nextduedate=2019-03-12"
  [2]=>
  string(13) "status=Active"
}
array(3) {
  [0]=>
  string(21) "name=Branding Removal"
  [1]=>
  string(22) "nextduedate=0000-00-00"
  [2]=>
  string(13) "status=Active"
}

It's weird because for Active it still says Branding is required, and if suspended it's still showing required. So I'm not sure what the heck is going on. I find this REALLY ODD! I use to name my products the original product name.. so in the case of the radio CMS, it would be Monthly WaveCMS Standard or Owned WaveCMS Standard . I use to do Unbranded or Branded at the end. I'm thinking of possibly going back to that! This is really weird haha. I'm going to continue to figure this one out.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated