Skip to content

Commit d711124

Browse files
committed
Update the following:
1) Allow facade to accepts objects 2) Change default Minor Version 3) Add vendor support 4) Add parse support for IPPFault 5) Fix Platform Service Call 6) Update _Samples
1 parent 176ed55 commit d711124

18 files changed

+486
-35
lines changed

src/Core/Configuration/BaseUrl.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace QuickBooksOnline\API\Core\Configuration;
33

4+
use QuickBooksOnline\API\Core\CoreConstants;
5+
6+
47
/**
58
* Base Urls for QBO, QBD and IPP
69
* -----------------
@@ -35,7 +38,7 @@ public function __construct($Qbo=null, $Ipp=null)
3538
{
3639
$this->Ipp = $Ipp;
3740
}else {
38-
$this->Ipp = "https://appcenter.intuit.com/api/";
41+
$this->Ipp = CoreConstants::IPP_BASEURL;
3942
}
4043
}
4144
}

src/Core/Configuration/LocalConfigReader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function ReadConfigurationFromFile($filePath, $OAuthOption = CoreC
9696
}
9797
}
9898

99-
public static function ReadConfigurationFromParameters($OAuthConfig, $baseUrl, $defaultLoggingLocation = CoreConstants::DEFAULT_LOGGINGLOCATION, $minorVersion = 3)
99+
public static function ReadConfigurationFromParameters($OAuthConfig, $baseUrl, $defaultLoggingLocation = CoreConstants::DEFAULT_LOGGINGLOCATION, $minorVersion = CoreConstants::DEFAULT_SDK_MINOR_VERSION)
100100
{
101101
$ippConfig = new IppConfiguration();
102102
try {

src/Core/CoreConstants.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
class CoreConstants
88
{
9+
//Set the default minor version to 8
10+
const DEFAULT_SDK_MINOR_VERSION = "8";
911
const DEFAULT_LOGGINGLOCATION = "/tmp/IdsLogs";
1012

1113
const PHP_CLASS_PREFIX = 'IPP';
@@ -157,6 +159,8 @@ class CoreConstants
157159
*/
158160
const QBO_BASEURL = "https://quickbooks.api.intuit.com/";
159161

162+
const IPP_BASEURL = "https://appcenter.intuit.com/api/";
163+
160164
/**
161165
* Id Parameter Name.
162166
* @var string Id
@@ -239,7 +243,7 @@ class CoreConstants
239243
* The Request source header value.
240244
* @var string REQUESTSOURCEHEADER
241245
*/
242-
const USERAGENT = "V3PHPSDK3.2.0";
246+
const USERAGENT = "V3PHPSDK3.2.6";
243247

244248
public static function getType($string, $return=1)
245249
{

src/Core/HttpClients/FaultHandler.php

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace QuickBooksOnline\API\Core\HttpClients;
33

4+
use QuickBooksOnline\API\Data\IPPFault;
45
/**
56
* Handles the 3xx, 4xx and 5xx response status code in the response and handles them.
67
* This class use the pecl OAuth Extension. Rewrite it to be native cURL request @Hao
@@ -28,6 +29,17 @@ class FaultHandler
2829
* The OAuth error message will store here.
2930
*/
3031
private $oAuthHelperError;
32+
33+
34+
//Intuit Error helper information
35+
private $intuitErrorType;
36+
37+
private $intuitErrorCode;
38+
39+
private $intuitErrorMessage;
40+
41+
private $intuitErrorDetail;
42+
3143
/**
3244
* Initializes a new instance of the FaultHandler class.
3345
* @param ServiceContext context
@@ -82,4 +94,27 @@ public function getResponseBody()
8294
{
8395
return $this->responseBody;
8496
}
97+
98+
public function getIntuitErrorType(){
99+
return $this->intuitErrorType;
100+
}
101+
102+
public function getIntuitErrorCode(){
103+
return $this->intuitErrorCode;
104+
}
105+
106+
public function getIntuitErrorMessage(){
107+
return $this->intuitErrorMessage;
108+
}
109+
110+
public function getIntuitErrorDetail(){
111+
return $this->intuitErrorDetail;
112+
}
113+
public function parseResponse($message){
114+
$xmlObj = simplexml_load_string($message);
115+
$this->intuitErrorType = (string)$xmlObj->Fault->attributes()['type'];
116+
$this->intuitErrorCode = (string)$xmlObj->Fault->Error->attributes()['code'];
117+
$this->intuitErrorMessage = (string)$xmlObj->Fault->Error->Message;
118+
$this->intuitErrorDetail = (string)$xmlObj->Fault->Error->Detail;
119+
}
85120
}

src/Core/HttpClients/IntuitResponse.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode
3232
$this->faultHandler = new FaultHandler();
3333
$this->faultHandler->setHttpStatusCode($this->httpResponseCode);
3434
$this->faultHandler->setResponseBody($this->body);
35+
$this->faultHandler->parseResponse($this->body);
3536
//Manually set the error message
3637
$this->faultHandler->setOAuthHelperError("Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)");
3738
}

src/Core/HttpClients/SyncRestHandler.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,21 @@ public function sendRequest($requestParameters, $requestBody, $oauthRequestUri)
208208
}
209209
} else {
210210
// IPP call
211-
$httpHeaders = array('user-agent' => CoreConstants::USERAGENT);
211+
$httpHeaders = array(
212+
'Authorization' => $AuthorizationHeader,
213+
'host' => parse_url($requestUri, PHP_URL_HOST),
214+
'user-agent' => CoreConstants::USERAGENT
215+
);
216+
// Log Request Body to a file
217+
$this->RequestLogging->LogPlatformRequests($requestBody, $requestUri, $httpHeaders, true);
218+
219+
if ($requestBody && $this->RequestCompressor) {
220+
$this->RequestCompressor->Compress($httpHeaders, $requestBody);
221+
}
222+
223+
if ($this->ResponseCompressor) {
224+
$this->ResponseCompressor->PrepareDecompress($httpHeaders);
225+
}
212226
}
213227

214228
$intuitResponse = $this->curlHttpClient->makeAPICall($requestUri, $HttpMethod, $httpHeaders, $requestBody, null, false);
@@ -283,7 +297,7 @@ private function CallRestService($requestParameters, $requestBody, $oauthRequest
283297
$this->context->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Getting the response from service.");
284298

285299
// Call the service and get response.
286-
list($httpWebResponseCode, $httpWebResponseBody) = $request->GetResponse($requestParameters, $requestBody, $oauthRequestUri);
300+
list($httpWebResponseCode, $httpWebResponseBody) = $request->sendRequest($requestParameters, $requestBody, $oauthRequestUri);
287301

288302
$this->context->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Got the response from service.");
289303

src/Core/ServiceContext.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ public static function ConfigureFromPassedArray(array $settings)
180180
$baseURL = $settings['baseUrl'];
181181
$checkedBaseURL = ServiceContext::checkAndAddBaseURLSlash($baseURL);
182182
$serviceType = CoreConstants::IntuitServicesTypeQBO;
183-
//Set the default minor version to 4
184-
$IppConfiguration = LocalConfigReader::ReadConfigurationFromParameters($OAuthConfig, $checkedBaseURL, CoreConstants::DEFAULT_LOGGINGLOCATION, "4");
183+
$IppConfiguration = LocalConfigReader::ReadConfigurationFromParameters($OAuthConfig, $checkedBaseURL, CoreConstants::DEFAULT_LOGGINGLOCATION, CoreConstants::DEFAULT_SDK_MINOR_VERSION);
185184
$serviceContextInstance = new ServiceContext($QBORealmID, $serviceType, $OAuthConfig, $IppConfiguration);
186185
return $serviceContextInstance;
187186
}
@@ -210,7 +209,8 @@ public function getBaseURL()
210209
if ($this->serviceType === CoreConstants::IntuitServicesTypeQBO) {
211210
$baseurl = $this->IppConfiguration->BaseUrl->Qbo . implode(CoreConstants::SLASH_CHAR, array(CoreConstants::VERSION)) . CoreConstants::SLASH_CHAR;
212211
}
213-
else {
212+
else if($this->serviceType === CoreConstants::IntuitServicesTypeIPP){
213+
$this->IppConfiguration->BaseUrl->Ipp = CoreConstants::IPP_BASEURL;
214214
$baseurl = $this->IppConfiguration->BaseUrl->Ipp;
215215
}
216216
} catch (\Exception $e) {

src/Data/IPPVendor.php

+15
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ public function __construct($keyValInitializers=array(), $verbose=false)
205205
* @var float
206206
*/
207207
public $Balance;
208+
209+
/**
210+
* @Definition
211+
Product: ALL
212+
Description: The Bill Rate for that vendor.
213+
Filterable: QBW
214+
Sortable: QBW
215+
216+
* @xmlType element
217+
* @xmlNamespace http://schema.intuit.com/finance/v3
218+
* @xmlMinOccurs 0
219+
* @xmlName BillRate
220+
* @var float
221+
*/
222+
public $BillRate;
208223
/**
209224
* @Definition Specifies the date of the Open Balance.
210225
Non QB-writable.

src/Facades/FacadeClassMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function IPPReferenceTypeNameEntity(){
3434
//IPPBill
3535
'PayerRef','VendorRef','APAccountRef',
3636
//IPPAccount
37-
'ParentRef',
37+
'ParentRef', 'VendorTypeRef','TermRef', 'PrefillAccountRef',
3838
//IPPCustomer
3939
'RootCustomerRef', 'CustomerTypeRef' ,'TaxGroupCodeRef', 'JobTypeRef',
4040
//IPPBillPayment

src/Facades/FacadeHelper.php

+99-16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class FacadeHelper{
2626
public static function reflectArrayToObject($classNameOrKeyName, $data, $throwException = TRUE) {
2727
if(!isset($classNameOrKeyName)){ throw new \Exception("The Class Name or Key Name cannot be NULL when generating Objects.");}
2828
if(!isset($data) || empty($data)){ throw new \Exception("The passed data cannot be NULL.");}
29+
if(is_object($data)){
30+
if(!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass($classNameOrKeyName,$data)){
31+
throw new \Exception("The assigned object is not an instance of required object:{" . $classNameOrKeyName . "}.");
32+
}else{
33+
return $data;
34+
}
35+
}
2936
//Get reflection class of FacadeHelper
3037
$trimedData = FacadeHelper::trimSpacesForArrayKeys($data);
3138
//Any key in the Ignored List will not be processed
@@ -66,22 +73,37 @@ public static function reflectArrayToObject($classNameOrKeyName, $data, $throwEx
6673
// }as $val
6774
$obj = FacadeHelper::reflectArrayToObject($key, $val, $throwException);
6875
FacadeHelper::assignValue($currentObj, $key, $obj);
69-
}
70-
else if(FacadeHelper::isRecurrsiveArray($val)){
76+
} else if(FacadeHelper::isArrayOfObj($val)){
77+
// Array of LineItem object, for example
78+
foreach($val as $valobj){
79+
if(!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass($key,$valobj)){
80+
throw new \Exception("The assigned object is not an instance of required object:{" . $key . "}.");
81+
}
82+
}
83+
FacadeHelper::assignValue($currentObj, $key, $val);
84+
} else{
7185
//The array is a recursive array. It can be an Line or LinkedTxn
7286
//Example:
7387
// Line": [{ ....}, {...}]
7488
//For each element in the array, it is a line
89+
//or It can be an mix of lines and Objects
90+
//Line": [{ ....}, $obj1, {...}]
7591
$list = array();
7692
foreach ($val as $index => $element) {
77-
$obj = FacadeHelper::reflectArrayToObject($key, $element, $throwException);
78-
array_push($list, $obj);
79-
}
93+
if(is_object($element)){
94+
if(!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass($key,$element)){
95+
throw new \Exception("The assigned object is not an instance of required object:{" . $key . "}.");
96+
}
97+
array_push($list, $element);
98+
}else if(is_array($element)){
99+
$obj = FacadeHelper::reflectArrayToObject($key, $element, $throwException);
100+
array_push($list, $obj);
101+
}else{
102+
throw new \Exception("Format Error. Expect an element of an array or an object.");
103+
}
104+
}
80105
FacadeHelper::assignValue($currentObj, $key, $list);
81-
}else{
82-
throw new \Exception("Internal Error. The Passed Array is neither associated array or recursive array.");
83-
}
84-
106+
}
85107
}else{
86108
//Even the value is a key, the key can be an Enum type or a wrapper
87109
if(FacadeHelper::isKeyInComplexList($key)){
@@ -95,9 +117,14 @@ public static function reflectArrayToObject($classNameOrKeyName, $data, $throwEx
95117
$createdObj = FacadeHelper::getEnumType($enumTypeClassName, $val);
96118
FacadeHelper::assignValue($currentObj, $key, $createdObj);
97119
}
98-
//It is a simple type
99120
else
100121
{
122+
//If it is an object
123+
if(is_object($val)){
124+
if(!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass($key,$val)){
125+
throw new \Exception("The assigned object is not an instance of required object:{" . $key . "}.");
126+
}
127+
}
101128
FacadeHelper::assignValue($currentObj, $key, $val);
102129
}
103130
}
@@ -176,6 +203,13 @@ public static function isKeyEnumType($key){
176203
public static function getEnumType($clazz, $val){
177204
if(!isset($val)) throw new \Exception("Passed param for Enum can't be null.");
178205
if(class_exists($clazz)){
206+
if(is_object($val)){
207+
if($val instanceof $clazz){
208+
return $val;
209+
}else{
210+
throw new \Exception("The assigned obj to the enum class Type:{" . $clazz . "} is not matched.");
211+
}
212+
}
179213
$enumObj = new $clazz();
180214
//If $val is string
181215
if(is_array($val) && !empty($val)){
@@ -212,12 +246,20 @@ public static function getClassMethod($className, $methodName){
212246
/**
213247
* Construct an IPPReferenceType based on passed Array or String.
214248
* If it is passed as an array, handle it.
215-
* If it is passed an a String. Construct an array and put the String on the value
249+
* If it is passed as a String. Construct an array and put the String on the value
250+
* If it is passed as an obj, compare it with IPPReferenceType and return the object
216251
* @param $data
217-
* It can either be an array or a String
252+
* It can either be an array or a String, or obj
218253
*/
219254
public static function getIPPReferenceTypeBasedOnArray($data){
220255
$trimedDataArray = FacadeHelper::trimSpacesForArrayKeys($data);
256+
if(is_object($trimedDataArray)){
257+
if($trimedDataArray instanceof IPPReferenceType){
258+
return $trimedDataArray;
259+
}else{
260+
throw new \Exception("The assigned obj to IPPReferenceType is not matched with IPPReferenceType.");
261+
}
262+
}
221263
//THe ReferenceDataType should only contain at most Two elements
222264
if(is_array($trimedDataArray)){
223265
if(sizeof($trimedDataArray) >= 3){
@@ -249,12 +291,20 @@ public static function getIPPReferenceTypeBasedOnArray($data){
249291
/**
250292
* If passed params is array, the first element of Array is used in IPPid.
251293
* If passed params is not an array, the the value is used for Ippid.
294+
* If passed params is an obj, the the value is simply returned.
252295
* @param $data
253296
* It can either be an array or a numeric representation
254297
*/
255298
public static function getIPPId($data){
256299
//Convert an IPPId based on the Data
257300
if(!isset($data)) throw new \Exception("Passed param for IPPid can't be null");
301+
if(is_object($data)){
302+
if($data instanceof IPPid){
303+
return $data;
304+
}else{
305+
throw new \Exception("The assigned obj to IPPid is not matched with IPPid.");
306+
}
307+
}
258308
if(is_array($data)){
259309
$firstElementValue = reset($data);
260310
}else if(is_numeric($data)){
@@ -305,11 +355,19 @@ public static function trimSpacesForArrayKeys($data){
305355
if(!isset($data) || empty($data)) return $data;
306356
if(is_array($data))
307357
{
308-
$trimedKeys = array_map('trim', array_keys($data));
309-
$trimedResult = array_combine($trimedKeys, $data);
310-
return $trimedResult;
358+
if(FacadeHelper::isArrayOfObj($data)){
359+
return $data;
360+
}else{
361+
$trimedKeys = array_map('trim', array_keys($data));
362+
$trimedResult = array_combine($trimedKeys, $data);
363+
return $trimedResult;
364+
}
311365
}else{
312-
return trim($data);
366+
if(is_object($data)){
367+
return $data;
368+
}else{
369+
return trim($data);
370+
}
313371
}
314372
}
315373

@@ -379,4 +437,29 @@ private static function assignValue($targetObject, $key, $value){
379437
}
380438
}
381439

440+
private static function checkIfTheObjectIsAnInstanceOfTheClass($className, $object){
441+
if($object instanceof $className){
442+
return true;
443+
}else{
444+
$className = FacadeHelper::decorateKeyWithNameSpaceAndPrefix($className);
445+
if($object instanceof $className){
446+
return true;
447+
}
448+
}
449+
return false;
450+
}
451+
452+
private static function isArrayOfObj($data){
453+
if(is_array($data)){
454+
foreach($data as $dataMemeber){
455+
if(!is_object($dataMemeber)){
456+
return false;
457+
}
458+
}
459+
return true;
460+
}
461+
462+
return false;
463+
}
464+
382465
}

0 commit comments

Comments
 (0)