Skip to content

Commit

Permalink
Re-add the removed files.
Browse files Browse the repository at this point in the history
  • Loading branch information
sburina committed Feb 23, 2022
1 parent ca08ddc commit 384ce2d
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
111 changes: 111 additions & 0 deletions config/whmcs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| WHMCS URL
|--------------------------------------------------------------------------
|
| Main URL to your WHMCS
|
*/
'url' => env('WHMCS_URL', 'http://localhost/whmcs'),

/*
|--------------------------------------------------------------------------
| API Credentials
|--------------------------------------------------------------------------
|
| Prior to WHMCS verison 7.2, authentication was validated based on admin
| login credentials, and not API Authentication Credentials. This method of
| authentication is still supported for backwards compatibility but may be
| deprecated in a future version of WHMCS.
|
| Supported auth types': "api", "password"
|
| @see https://developers.whmcs.com/api/authentication/
|
*/
'auth' => [
'type' => env('WHMCS_AUTH_TYPE', 'api'),

'api' => [
'identifier' => env('WHMCS_API_ID', ''),
'secret' => env('WHMCS_API_SECRET', ''),
],

'password' => [
'username' => env('WHMCS_ADMIN_USERNAME', ''),
'password' => env('WHMCS_ADMIN_PASSWORD', ''),
],
],

/*
|--------------------------------------------------------------------------
| API Credentials
|--------------------------------------------------------------------------
|
| an access key can be configured to allow IP restrictions to be bypassed.
| It works by defining a secret key/passphrase in the WHMCS configuration.php
| file which is then passed into all API calls. To configure it, add a line
| as follows to your configuration.php file in the root WHMCS directory.
|
| @see https://developers.whmcs.com/api/access-control/
|
*/
'api_access_key' => env('WHMCS_API_ACCESS_KEY', ''),

/*
|--------------------------------------------------------------------------
| AutoAuth
|--------------------------------------------------------------------------
| Auth Key to automatically login the user to WHMCS if already loogged in
| to this app. Option "goto" allows you to redirect user to a specific WHMCS
| page after successful login.
|
| @see https://docs.whmcs.com/AutoAuth
|
*/
'autoauth' => [
'key' => env('WHMCS_AUTOAUTH_KEY'),
'goto' => 'clientarea.php?action=products',
],

/*
|--------------------------------------------------------------------------
| Session key to store WHMCS user record
|--------------------------------------------------------------------------
|
| After successful validation, we store the retrieved WHMCS user record to
| the following session key:
|
*/
'session_key' => env('WHMCS_SESSION_USER_KEY', 'user'),

/*
|--------------------------------------------------------------------------
| Convert numbers from strings to floats in results
|--------------------------------------------------------------------------
|
| WHMCS API returns numbers (prices, etc..) as strings in its JSON results.
| This option will reformat the response so all the numbers with two decimals
| will be converted to floats in the resulting array. If you just need to
| display the results, leave the option turned off.
|
| Default: false
|
*/
'use_floats' => false,

/*
|--------------------------------------------------------------------------
| Return the results as associative arrays
|--------------------------------------------------------------------------
|
| true: get result as an associative array.
| false: get the result as a stdClass object.
|
*/
'result_as_array' => true,
];
67 changes: 67 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Sburina\Whmcs;

use Exception;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ConnectException;

class Client
{
/**
* @var GuzzleClient
*/
protected $client;

/**
* Client constructor.
*/
public function __construct()
{
$options = [
'timeout' => 30,
'debug' => false,
'http_errors' => true,
'base_uri' => rtrim(config('whmcs.url'), '/').'/includes/api.php',
'verify' => false,
'headers' => [
'User-Agent' => 'sburina/laravel-whmcs-up',
],
];

if (!empty(config('whmcs.api_access_key'))) $options['query'] = ['accesskey' => config('whmcs.api_access_key')];

$this->client = new GuzzleClient($options);
}

/**
* @param array $data
*
* @return array
*/
public function post($data)
{
try {
$fp = array_merge(
config('whmcs.auth.'.config('whmcs.auth.type')),
['responsetype' => 'json'],
$data
);

$response = $this->client->post('', ['form_params' => $fp]);
$contents = $response->getBody()->getContents();

if (config('whmcs.use_floats', false)) {
$contents = preg_replace('/":"(-?\d+\.\d\d)"/', '":\1', $contents);
}

return json_decode($contents, config('whmcs.result_as_array', true));
} catch (ConnectException $e) {

return ['result' => 'error', 'message' => $e->getMessage()];
} catch (Exception $e) {

return ['result' => 'error', 'message' => $e->getMessage()];
}
}
}

0 comments on commit 384ce2d

Please sign in to comment.