Integration with woocomerce

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Integration with woocomerce

Post by Apprentice AHK » 06 Apr 2018, 05:58

Hi
I am interested in integrating my AHK scripts to the woocommerce software license manager

they have web api listed at
https://woosoftwarelicense.com/document ... i-methods/

I have basic AutoHotKey scripting skills.

Initially I would just like to be able to check for a valid license every couple of months.

Cheers
Kevin

gregster
Posts: 8999
Joined: 30 Sep 2013, 06:48

Re: Integration with woocomerce

Post by gregster » 06 Apr 2018, 08:36

Hi Kevin,
so you want to secure your (compiled) AHK scripts with this? Are you aware that it is not very complicated to extract the source code from a standard AHK-exe file and strip such license checks from it? (code compressors might make it more difficult but also might raise the likelihood of false positives from anti-virus software)

Anyway... I think, the provided code example is not unambigous or complete (they are using functions whose defintions are not shown). If they are using a URL Query String , the command URLDownloadToFile (or its user function cousin URLDownloadToVar) , used with the constructed URL string could be already enough to use the API.

But without the "[a]dditional code examples [..] included within the plugin download" it is just a guess, based on the vague description they provide. For handling the JSON response of the API call, there are a few functions available on this forum. I usually use Coco's JSON lib for parsing such API responses.

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 07 Apr 2018, 09:33

Hi Gregstar

Thanks for the input.
I had found this for a code obfuscator
https://github.com/davidmalia/autohotkey-obfuscator

AV false positives I will have to live with
Unless there is a better way... I will get the extension setup.

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 07 Apr 2018, 11:41

Right, more reading...
Found AutoHotkey_H v2 Alpha and working my way through that

User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Integration with woocomerce

Post by Cuadrix » 07 Apr 2018, 20:42

For "obfuscating" the code, use AHK source code Encryptor v1.6 By FeiYue:
https://autohotkey.com/boards/viewtopic ... or#p204668

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 11 Apr 2018, 04:49

Right, more reading...
Found AutoHotkey_H v2 Alpha and working my way through that... this is oo coding... wow
back to version1

User avatar
epete
Posts: 81
Joined: 02 Dec 2020, 10:03

Re: Integration with woocomerce

Post by epete » 07 Jul 2021, 13:43

For anyone that may need it, here is a WooCommerce Software License API testing utility I developed.

Code: Select all

;--------------------------------------------------------------------------------------------------------------------
; WooCommerce Software Licence API test
; https://woosoftwarelicense.com/documentation/api-methods/
; AutoHotKey - https://www.autohotkey.com/ (free and open source software) 
;
; activate
; This method allows a license key to be synchronized with your software for activation. 
;  woo_sl_action='activate'
;  licence_key
;  product_unique_id
;  domain
;
; deactivate
; This method facilitate a licence key de-activation. Once de-activated a key is released from being set as used status, and it can be integrated with another domain / hash. 
;  woo_sl_action='deactivate'
;  licence_key
;  product_unique_id
;  domain
;
; status-check
; The status-check API method allow a certain software key to be verified for being still active, valid, assigned to a certain domain.
;  woo_sl_action='status-check'
;  licence_key
;  product_unique_id
;  domain
;
; plugin_update
; The plugin_update API method return basic details regarding software update. The following arguments are required for a call:
;  woo_sl_action='plugin_update'
;  licence_key
;  product_unique_id
;  domain
;  version
;--------------------------------------------------------------------------------------------------------------------

Global slURL             :="http://YourDomainWhereSoftwareManagement.com/index.php"
Global woo_sl_action     :=""
Global licence_key       :=""
Global product_unique_id :=""
Global domain            :=""
Global version           :=""
Global Actions           :="activate|deactivate|status-check|plugin_update|theme_update|plugin_information|code_version|key_delete"
Global Action#           :=3
Global ResponseMsg       :=""

Gui, API:+AlwaysOnTop -DPIScale
Gui, API:Color, 0x99CCFF
Gui, API:Add, Text, x025 y025 w080 h20 +0x200 , API Action:
Gui, API:Add, DropDownList, x100 y025 w120 AltSubmit Choose3 vAction# gCheckAction, %Actions%
Gui, API:Add, Text, x025 y050 w080 h20 +0x200 , URL:
Gui, API:Add, Edit, x100 y050 w250 h20 vslURL , %slURL%
Gui, API:Add, Text, x025 y075 w080 h20 +0x200 , Product ID:
Gui, API:Add, Edit, x100 y075 w250 h20 vproduct_unique_id , %product_unique_id%
Gui, API:Add, Text, x025 y100 w080 h20 +0x200 , Licence Key:
Gui, API:Add, Edit, x100 y100 w250 h20 vlicence_key , %licence_key%
Gui, API:Add, Text, x025 y125 w080 h20 +0x200 , Domain:
Gui, API:Add, Edit, x100 y125 w250 h20 vdomain , %domain%
Gui, API:Add, Text, x025 y150 w080 h20 +0x200 , Version:
Gui, API:Add, Edit, x100 y150 w250 h20 vversion , %Version%
GuiControl, API:Disable, version
Gui, API:Add, Edit, x025 y175 w500 h220 vResponseMsg , %ResponseMsg%
Gui, API:Add, Button, x225 y405 w80 h30 Default gAPIaction, GO
Gui, API:show, x100 y100 w550 h450, WooCommerce Software Licence API calls
return

CheckAction(){
GuiControlGet, Action#, API:
Switch Action#
  {
  Case 4, 5, 6: ; plugin_update, theme_update, plugin_information
    GuiControl, API:Enable,  licence_key
    GuiControl, API:Enable,  domain
    GuiControl, API:Enable,  version
  Case 7:       ; code_version
    GuiControl, API:Disable, licence_key
    GuiControl, API:Disable, domain
    GuiControl, API:Disable, version
  Case 8:       ; key_delete
    GuiControl, API:Enable,  licence_key
    GuiControl, API:Disable, domain
    GuiControl, API:Disable, version
  Default:
    GuiControl, API:Enable,  licence_key
    GuiControl, API:Enable,  domain
    GuiControl, API:Disable, version 
  }
}

APIaction(){
  GuiControlGet, apiURL,  API:
  GuiControlGet, Action#, API:
  Switch Action#
    {
    Case (1): woo_sl_action := "activate"
    Case (2): woo_sl_action := "deactivate"
    Case (3): woo_sl_action := "status-check"
    Case (4): woo_sl_action := "plugin_update"
    Case (5): woo_sl_action := "theme_update"
    Case (6): woo_sl_action := "plugin_information"
    Case (7): woo_sl_action := "code_version"
    Case (8): woo_sl_action := "key_delete"
    }
  GuiControlGet, licence_key,       API:
  GuiControlGet, product_unique_id, API:
  GuiControlGet, domain,            API:
  GuiControlGet, version,           API:
  URL := slURL "?woo_sl_action=" woo_sl_action "&product_unique_id=" product_unique_id 
  Switch woo_sl_action
    {
    Case "activate", "deactivate", "status-check":
      URL := URL "&licence_key=" licence_key "&domain=" domain
    Case "plugin_update", "theme_update", "plugin_information":
      URL := URL "&licence_key=" licence_key "&version=" version "&domain=" domain 
    Case "code_version":
    Case "key_delete":
      URL := URL "&licence_key=" licence_key
    }
  ResponseMsg := % URL 
  ;MsgBox, % "URL:" URL
  lObj := URLDownloadtoString(URL)
  ;MsgBox, % "LicenceObj: " lObj
  if (lObj = "" or lObj = "[]") 
    ResponseMsg := ResponseMsg "`n`nNo response to API call"
  else
    FormatResponseMsg(lObj)
  GuiControl, API:, ResponseMsg, %ResponseMsg% 
  return

APIGuiEscape:
APIGuiClose:
  Gui, API:Destroy
  ExitApp
return
}

ExitApp

FormatResponseMsg(lObj){
  lObj  := LTrim(lObj, OmitChars := " `[")
  lObj  := RTrim(lObj, OmitChars := " `]")
  lr := JsonToAHK(lObj)
  ;lObj  := JsonToAHK(URLDownloadtoVar(URL))
  ResponseMsg := ResponseMsg "`n`nReturned: " lObj "`n`nStatus: " lr.status "`nStatus Code: " lr.status_code "`nMessage: " lr.message 
  Switch Action#
    {
    Case 1, 3:   ; activate, status-check
    ResponseMsg := ResponseMsg "`nLicence Status: " lr.licence_status "`nLicence Start: " lr.licence_start "`nLicence Expire: " lr.licence_expire
    Case 2:      ; deactivate
    Case 4, 5:   ; plugin_update, theme_update
    ResponseMsg := ResponseMsg "`nNew Version: " lr.message.new_version "`nDate: " lr.message.date "`nPackage: " lr.message.package "`nUpgrade Notice: " lr.message.upgrade_notice "`nAuthor: " lr.message.author "`nTested: " lr.message.tested "`nHomepage: " lr.message.homepage "`nURL: " lr.message.url
    Case 6:      ; plugin_information
    ResponseMsg := ResponseMsg "`nName: " lr.message.name "`nVersion: " lr.message.version "`nLast Update: " lr.message.last_updated "`nPackage: " lr.message.package "`nDownload Link: " lr.message.download_link "`nUpgrade Notice: " lr.message.upgrade_notice "`nAuthor: " lr.message.author "`nTested: " lr.message.tested "`nRequires: " lr.message.requires "`nHomepage: " lr.message.homepage "`nDescription: " lr.message.sections.description "`nBanners Low: " lr.message.sections.banners.low "`nBanners High: " lr.message.sections.banners.high 
    Case 7:      ; code_version
    ResponseMsg := ResponseMsg "`nName: " lr.message.name "`nVersion: " lr.message.version "`nLast Update: " lr.message.last_updated "`nUpgrade Notice: " lr.message.upgrade_notice "`nAuthor: " lr.message.author "`nTested: " lr.message.tested "`nRequires: " lr.message.requires "`nHomepage: " lr.message.homepage "`nDescription: " lr.message.sections.description  "`nBanners Low: " lr.message.sections.banners.low "`nBanners High: " lr.message.sections.banners.high 
    Case 8:      ; key_delete
    }
}

URLDownloadtoString(url, encoding = "utf-8")
{
    static a := "AutoHotkey/" A_AhkVersion
    if (!DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
        return 0
    c := s := 0, o := ""
    if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
    {
        while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s > 0)
        {
            VarSetCapacity(b, s, 0)
            DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
            o .= StrGet(&b, r >> (encoding = "utf-16" || encoding = "cp1200"), encoding)
        }
        DllCall("wininet\InternetCloseHandle", "ptr", f)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", h)
    return o
}

JsonToAHK(json, rec := false){
  static doc := ComObjCreate("htmlfile")
        , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
        , JS := doc.parentWindow
  if !rec
     obj := JsonToAHK(JS.eval("(" . json . ")"), true)
  else if !IsObject(json)
     obj := json
  else if json.toString() != "[object Object]" {
     obj := []
     Loop % json.length
       obj.Push( JsonToAHK(json[A_Index - 1], true) )
  }
  else {
    obj := {}
    keys := JS.Object.keys(json)
    Loop % keys.length {
      k := keys[A_Index - 1]
      obj[k] := JsonToAHK(json[k], true)
    }
  }
Return obj
}

;---------------------------------------------------------------------------------------------------------------------
;~ //the url where the WooCommerce Software Licence plugin is being installed
;~ define('SL_APP_API_URL',      'http://YourDomainWhereSoftwareManagement.com/index.php');
;~ //the Software Unique ID as defined within product admin page
;~ define('SL_PRODUCT_ID',           'APTO');
;~ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
;~ define('SL_INSTANCE',             str_replace($protocol, "", get_bloginfo('wpurl')));
;~ ----------------------------------
;~ $args = array(
                    ;~ 'woo_sl_action'         => 'activate',
                    ;~ 'licence_key'       => $licence_key,
                    ;~ 'product_unique_id'        => SL_PRODUCT_ID,
                    ;~ 'domain'          => SL_INSTANCE
                ;~ );
;~ $request_uri    = SL_APP_API_URL . '?' . http_build_query( $args );
;~ $data           = wp_remote_get( $request_uri );
 
;~ if(is_wp_error( $data ) || $data['response']['code'] != 200)
    ;~ {
        ;~ //there was a problem establishing a connection to the API server
    ;~ }
 
;~ $data_body = json_decode($data['body']);
;~ if(isset($data_body->status))
    ;~ {
        ;~ if( $response_block->status == 'success' && ( $response_block->status_code == 's100' || $response_block->status_code == 's101' ) )
            ;~ {
                ;~ //the licence is active and the software is active
                ;~ //doing further actions like saving the licence and allow the plugin to run
 
            ;~ }
            ;~ else
            ;~ {
                ;~ //there was a problem activating the licence
            ;~ }
    ;~ }
    ;~ else
    ;~ {
        ;~ //there was a problem establishing a connection to the API server
    ;~ }


Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 13 Aug 2022, 04:03

thanks epete, this is brilliant.
I have been away for a couple of years, now getting back on it.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Integration with woocomerce

Post by BoBo » 13 Aug 2022, 04:27

epete wrote:
07 Jul 2021, 13:43
For anyone that may need it, here is a WooCommerce Software License API testing utility I developed.
Have you posted this script already in a dedicated thread within the forum's 'Scripts & Functions"-section?
I'd guess it deserves it! :thumbup: Thx for sharing it.

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Integration with woocomerce

Post by ananthuthilakan » 13 Aug 2022, 10:18

Code: Select all

url := ; "your api request url which should have the parameters like status or activate or deactivate etc"
HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.Open("GET", url, false)
HttpObj.Send()
MsgBox, % HttpObj.Responsetext ; Response in json
objt := JsonToAHK(HttpObj.Responsetext)  ; converting json to Ahk and store into variable so you could do what ever you want with the response you get 
return


JsonToAHK(json, rec := false) {
   static doc := ComObjCreate("htmlfile")
         , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
         , JS := doc.parentWindow
   if !rec
      obj := %A_ThisFunc%(JS.eval("(" . json . ")"), true)
   else if !IsObject(json)
      obj := json
   else if JS.Object.prototype.toString.call(json) == "[object Array]" {
      obj := []
      Loop % json.length
         obj.Push( %A_ThisFunc%(json[A_Index - 1], true) )
   }
   else {
      obj := {}
      keys := JS.Object.keys(json)
      Loop % keys.length {
         k := keys[A_Index - 1]
         obj[k] := %A_ThisFunc%(json[k], true)
      }
   }
   Return obj
}
return

This would be sufficient i guess

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 31 Aug 2022, 03:38

Hi Epete

I have just got my licensing domain up and working and your script works a treat, thank you :D

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 01 Sep 2022, 04:59

I dont really understand the code from Epete or the code for the client side to run in myapp.
I can make the https request from my browser to my wc site, license status is ok.

but not been able to get a client side, myapp working yet.
The code displays an error message with the option to continue or cancel on the ComObjCreate line.

My aim for when myapp is first run is to activate the licence and write / append a valid license to an ini file.
and when myapp is run, check the inifile for a valid days left to run
I also like the way lintalist offers a help page at start up and want to include that trick.

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 01 Sep 2022, 05:27

yea, the client myapp works, it was my typo causing the issue thank you for your contributions

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 01 Sep 2022, 06:21

How do I use objt?

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 01 Sep 2022, 07:07

The http json response is [{"status":"success","status_code":"s101","message":"Licence Key was Already Activated for ","licence_status":"active","licence_start":"2022-08-31","licence_expire":"2022-09-14"}]

but I am at a loss how to get these into variables for use in ahk.
from the code example above
objt := JsonToAHK(HttpObj.Responsetext) ; converting json to Ahk and store into variable so you could do what ever you want with the response you get

how do you use objt?

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Integration with woocomerce

Post by BoBo » 01 Sep 2022, 07:19

I'd guess the way you're accessing an array... bc you're not storing it within a variable but an object...

Code: Select all

objt := [{"status":"success","status_code":"s101","message":"Licence Key was Already Activated for ","licence_status":"active","licence_start":"2022-08-31","licence_expire":"2022-09-14"}]

MsgBox % objt[1].status

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 02 Sep 2022, 03:19

thank you for that insight.
so I revisited Epete's code and he strips out the [] square braces

Code: Select all

lobj := HttpObj.Responsetext
lobj  := LTrim(lObj, OmitChars := " `[")
lobj  := RTrim(lObj, OmitChars := " `]")

MsgBox, % lobj  - displays correctly
lobjt := JsonToAHK(lobj) 
MsgBox, % lobjt - displays blank
[Mod edit: [code][/code] tags added.]

Apprentice AHK
Posts: 12
Joined: 05 Mar 2018, 08:14

Re: Integration with woocomerce

Post by Apprentice AHK » 02 Sep 2022, 07:36

got it.
found this post viewtopic.php?f=76&t=75949

Post Reply

Return to “Ask for Help (v1)”