File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/WeFact/WeFactAPI.php
<?php
namespace App\KommaApp\WeFact;
class WeFactAPI
{
const DEBTOR_CONTROLLER = 'debtor';
const PRODUCT_CONTROLLER = 'product';
const SUBSCRIPTION_CONTROLLER = 'subscription';
const GROUP_CONTROLLER = 'group';
const INVOICE_CONTROLLER = 'invoice';
private $url;
private $apiKey;
function __construct(){
$this->url = config('services.weFact.url');
$this->apiKey = config('services.weFact.key');
}
/**
* @param $controller
* @param $action
* @param $params
* @return array|mixed
*/
public function sendRequest($controller, $action, $params){
if(is_array($params)){
$params['api_key'] = $this->apiKey;
$params['controller'] = $controller;
$params['action'] = $action;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $this->url);
// Never skip SSL verification for production mode.
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,'10');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$curlResp = curl_exec($ch);
$curlError = curl_error($ch);
if ($curlError != ''){
$result = array(
'controller' => 'invalid',
'action' => 'invalid',
'status' => 'error',
'date' => date('c'),
'errors' => array($curlError)
);
}
elseif(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 403)
{
$result = array(
'controller' => 'invalid',
'action' => 'invalid',
'status' => 'error',
'date' => date('c'),
'errors' => array($curlResp)
);
}
else
{
$result = json_decode($curlResp, true);
}
return $result;
}
/**
* Convert the weFact stringed price to a cent-based integer
*
* @param string $price
* @return int
*/
public static function convertWeFactPrice(string $price): int
{
$price = (float) $price; // Convert from string to float
$price *= 100; // Convert to cents
$price = (int) $price; // Convert to integer
return $price;
}
/**
* Convert the incl vat price in cent to price excl in euros
*
* @param int $price
* @param int $taxPercentage
* @return float
*/
public static function convertPriceInclToExcl(int $price, int $taxPercentage): float
{
$priceExclInCent = (int) round($price / (100 + $taxPercentage) * 100);
return $priceExclInCent / 100;
}
/**
* Convert WeFact excluding vat price directly into including vat price
*
* @param string $price
* @param int $taxPercentage
* @return int
*/
public static function convertPriceExclToIncl(string $price, int $taxPercentage)
{
$priceExcl = self::convertWeFactPrice($price);
$priceIncl = (int) round( $priceExcl / 100 * (100 + $taxPercentage));
return $priceIncl;
}
}