Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

[Advanced Usage] Supported Actions

hitmanpt edited this page Feb 8, 2017 · 4 revisions

This is highly unrecommended

Even though the API is ready to use some times there might by a necessity to create your own function
To perform that you need to initialize the Call class (located in WHMCS_API.Call) with the api login credentials
You need an NameValueCollection with the action that you're gonna make the call
For example

NameValueCollection reqData = new NameValueCollection()
{
  { "action", EnumUtil.GetString(APIEnums.Actions.ValidateLogin) }
};

All the already implemented actions are available as an Enum in APIEnums.Actions.ValidateLogin but you need to convert them to string as demonstrated above.
And then perform the call by just using the MakeCall function in the Call Class.
The return value of the MakeCall function is a JSON string as the function is supported a model is already built to be desirialized for example
JsonConvert.DeserializeObject<ValidateLogin.ValidateLogin>(_call.MakeCall(reqData));

Here's an example on how to implement a custom function with a supported action

using System.Web.Mvc;
using System.Collections.Specialized;
using WHMCS_API;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MVCDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly string username = "WHMCS_API_USERNAME";
        private readonly string password = "WHMCS_API_PASSWORD";
        private readonly string accessKey = "WHMCS_API_ACCESSKEY";
        private readonly string whmcsUrl = "WHMCS_USERFRONTEND_URL"; //Example: http://example.com/whmcs
        
        [HttpPost]
        public ActionResult _Login(string email, string password)
        {
           Call _call = new Call(username, password, accessKey, whmcsUrl);
           NameValueCollection reqData = new NameValueCollection()
           {
               { "action", EnumUtil.GetString(APIEnums.Actions.ValidateLogin) },
               { EnumUtil.GetString(APIEnums.ValidateLoginParams.Email), email },
               { EnumUtil.GetString(APIEnums.ValidateLoginParams.Password), password }
           };

           ValidateLogin.ValidateLogin result = JsonConvert.DeserializeObject<ValidateLogin.ValidateLogin>(_call.MakeCall(data));
           if (result.Result.ToString() == "success")
                return Json(result.ClientID.ToString(), JsonRequestBehavior.AllowGet);
           else
                return Json("Check Username or Password", JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            return View();
        }
}

Donate