Jump to content

[PHP] WHMCS Hook to Automatically Add to PSA


MaxITGarrett

Recommended Posts

For those of you who would like to use WHMCS (Automated web hosting platform) but wish to use Pulseway PSA for your main ticketing software and for managing your consulting business if you are an MSP like us, I had to make a hook for WHMCS which takes the client details and automatically makes a client account for them within PSA and grants them access to the ticketing system, using the same password for WHMCS. This vastly simplifies things for the client, and conforms the tickets into one system.

Once this file is created, it needs to be placed into the following directory with any name:

\includes\hooks\AddClientToPSA.php

YOU WILL NEED TO QUERY SOME VALUES YOURSELF THROUGH THE API TO MAKE THE HOOK VALID (mentioned as comments) (if you need help with this, I can provide code)

<?php
    add_hook('ClientAdd', 1, function($vars) {

        // Setup the curl request for PSA
        $curl = curl_init();

        // Set the endpoint
        curl_setopt($curl, CURLOPT_URL, "https://psa.pulseway.com/api/token");

        // POST Request
        curl_setopt($curl, CURLOPT_POST, true);

        // Credentials for Authorization
        curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=password&username=username&password=userpassword&tenant=companyname");

        // Proper header information
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

        // Return the transfer as a string
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // Get the response
        $response = json_decode(curl_exec($curl));

        // Close the curl
        curl_close($curl);

        // Extract the token
        $token = $response->access_token;

        // Now to create a new company
        $curl = curl_init();
        
        // Set the endpoint
        curl_setopt($curl, CURLOPT_URL, "https://psa.pulseway.com/api/crm/accounts");

        // POST Request
        curl_setopt($curl, CURLOPT_POST, true);

        // Gather current datetime
        $date = new DateTime();

        // Gather a company name, if specified
        $companyName = $vars['companyname'];

        // If they don't have a company name, make it their first name and last name
        if (empty($companyName)) {
            $companyName = $vars['firstname'] . $vars['lastname'];
        }

        // Build the JSON to go with the request
        $account = json_encode(array(
            "AccountCode" => (string)$vars['userid'],
            "AccountTypeId" => 12345,           // UNIQUE TO YOU
            "AccountName" => $companyName,
            "Description" => "From WHMCS.",
            "Website" => null,
            "BusinessTypeId" => 0,
            "ServiceTypeId" => 123,             // UNIQUE TO YOU
            "CurrencyId" => 1,
            "SalesTaxItemId" => null,
            "AccountManagerId" => 12345,        // UNIQUE TO YOU
            "IsActive" => true,
            "IsBilling" => true,
            "AcquiredDate" => $date->format('Y-m-d\TH:i:s.u'),
            "CreditLimit" => null,
            "NetDays" => null,
            "Locations" => [array(
                "LocationName" => "Main",
                "IsActive" => true,
                "IsMain" => true,
                "Addresses" => [array(
                    "Address1" => $vars['address1'],
                    "Address2" => $vars['address2'],
                    "City" => $vars['city'],
                    "State" => $vars['state'],
                    "ZipCode" => $vars['postcode'],
                    "Phone" => $vars['phonenumber'],
                    "Latitude" => null,
                    "Longitude" => null,
                    "EmailAddress" => $vars['email'],
                    "Fax" => null,
                    "AddressTypeId" => 1
                )]
            )]
        ));

        // Add the JSON to the request
        curl_setopt($curl, CURLOPT_POSTFIELDS, $account);

        // Set the header, with the authorization token
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Authorization: Bearer " . $token,
            "Content-Type: application/json",
            "Accept: application/json"
        ));

        // Return the transfer as a string
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // Get the response
        $response = curl_exec($curl);

        // Close the curl
        curl_close($curl);

        // Get the ID of the new account
        $response_json = json_decode($response);
        $account_id = $response_json->{'Id'};

        // Create a new Contact now
        $curl = curl_init();
        
        // Set the endpoint
        curl_setopt($curl, CURLOPT_URL, "https://psa.pulseway.com/api/Import/contacts");

        // POST Request
        curl_setopt($curl, CURLOPT_POST, true);

        // Build the JSON to go with the request
        $contact = json_encode(array(
            "AccountName" => $companyName,
            "LocationName" => "Main",
            "FirstName" => $vars['firstname'],
            "LastName" => $vars['lastname'],
            "Email" => $vars['email'],
            "Phone" => $vars['phonenumber'],
            "JobTitle" => "Client",
            "Poc" => true,
            "IsClientPortal" => true,
            "PortalUsername" => $vars['email'],
            "PortalPassword" => $vars['password'],
            "PortalSecurityLevel" => "External User"
        ));

        // To conform with Pulseway API format
        $contact = '[' . $contact . ']';

        // Add the JSON to the request
        curl_setopt($curl, CURLOPT_POSTFIELDS, $contact);

        // Set the header, with the authorization key
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Authorization: Bearer " . $token,
            "Content-Type: application/json",
            "Accept: application/json"
        ));

        // Return the transfer as a string
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // Close the curl
        curl_close($curl);
    });
?>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...