|
| 1 | +<?php |
| 2 | + /** |
| 3 | + * Copyright (C) 2018 Squizz PTY LTD |
| 4 | + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 5 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 6 | + * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. |
| 7 | + */ |
| 8 | + namespace squizz\api\v1\endpoint; |
| 9 | + require_once __DIR__ . '/../../../../../3rd-party/jsonmapper/JsonMapper.php'; |
| 10 | + require_once __DIR__ . '/../../../../../3rd-party/jsonmapper/JsonMapper/Exception.php'; |
| 11 | + |
| 12 | + use squizz\api\v1\APIv1Constants; |
| 13 | + use squizz\api\v1\APIv1HTTPRequest; |
| 14 | + use squizz\api\v1\APIv1OrgSession; |
| 15 | + use squizz\api\v1\endpoint\APIv1EndpointResponseESD; |
| 16 | + use EcommerceStandardsDocuments\ESDocument; |
| 17 | + use EcommerceStandardsDocuments\ESDocumentConstants; |
| 18 | + use EcommerceStandardsDocuments\ESDocumentCustomerAccountEnquiry; |
| 19 | + use \JsonMapper; |
| 20 | + |
| 21 | + /** |
| 22 | + * Class handles calling the SQUIZZ.com API endpoint to search for and retrieve records (such as invoices, sales orders, back orders, payments, credits, transactions) associated to a supplier organisation's customer account. See the full list at https://www.squizz.com/docs/squizz/Platform-API.html#section1035 |
| 23 | + * The data being retrieved is wrapped up in a Ecommerce Standards Document (ESD) that contains records storing data of a particular type |
| 24 | + */ |
| 25 | + class APIv1EndpointOrgSearchCustomerAccountRecords |
| 26 | + { |
| 27 | + /** |
| 28 | + * Calls the platform's API endpoint and searches for a connected organisation's customer account records retrieved live from their connected business system |
| 29 | + * @param apiOrgSession organisation API session |
| 30 | + * @param endpointTimeoutMilliseconds amount of milliseconds to wait after calling the the API before giving up, set a positive number |
| 31 | + * @param recordType type of record data to search for. |
| 32 | + * @param supplierOrgID unique ID of the organisation in the SQUIZZ.com platform that has supplies the customer account |
| 33 | + * @param customerAccountCode code of the account organisation's customer account. Customer account only needs to be set if the supplier organisation has assigned multiple accounts to the organisation logged into the API session (customer org) and account specific data is being obtained |
| 34 | + * @param beginDateTime earliest date time to search for records for. Date time set as milliseconds since 1/1/1970 12am UTC epoch |
| 35 | + * @param endDateTime latest date time to search for records up to.Date time set as milliseconds since 1/1/1970 12am UTC epoch |
| 36 | + * @param pageNumber page number to obtain records from |
| 37 | + * @param recordsMaxAmount maximum number of records to return |
| 38 | + * @param outstandingRecords if true then only search for records that are marked as outstanding (such as unpaid invoices) |
| 39 | + * @param searchString search text to match records on |
| 40 | + * @param keyRecordIDs comma delimited list of records unique key record ID to match on.Each Key Record ID value needs to be URI encoded |
| 41 | + * @param searchType specifies the field to search for records on, matching the record's field with the search string given |
| 42 | + * @return APIv1EndpointResponseESD response from calling the API endpoint with the obtained Ecommerce Standards Document containing customer account enquiry records |
| 43 | + */ |
| 44 | + public static function call($apiOrgSession, $endpointTimeoutMilliseconds, $recordType, $supplierOrgID, $customerAccountCode, $beginDateTime, $endDateTime, $pageNumber, $recordsMaxAmount, $outstandingRecords, $searchString, $keyRecordIDs, $searchType) |
| 45 | + { |
| 46 | + $requestHeaders = array(); |
| 47 | + $endpointResponse = new APIv1EndpointResponseESD(); |
| 48 | + $deserializeESDDocument = new ESDocumentCustomerAccountEnquiry(); |
| 49 | + $callEndpoint = true; |
| 50 | + |
| 51 | + try{ |
| 52 | + //set endpoint parameters |
| 53 | + $endpointParams ="record_type=".$recordType."&supplier_org_id=".urlencode(utf8_encode($supplierOrgID))."&customer_account_code=".urlencode(utf8_encode($customerAccountCode))."&begin_date_time=".$beginDateTime."&end_date_time=".$endDateTime."&page_number=".$pageNumber."&records_max_amount=".$recordsMaxAmount."&outstanding_records=".($outstandingRecords ? "Y" : "N")."&search_string=".urlencode(utf8_encode($searchString))."&key_record_ids=".urlencode(utf8_encode($keyRecordIDs))."&search_type=".urlencode(utf8_encode($searchType)); |
| 54 | + |
| 55 | + //make a HTTP request to the platform's API endpoint to search for the customer account enquiry records |
| 56 | + if($callEndpoint == true) |
| 57 | + { |
| 58 | + //set function used to read the response from the endpoint |
| 59 | + $endpointJSONReader = function($jsonArray, $endpointResponse) use($deserializeESDDocument){ |
| 60 | + $endpointResponse->jsonDeserialize($jsonArray); |
| 61 | + |
| 62 | + //deserialize array into Ecommerce Standards Document based on the given data type |
| 63 | + $jsonMapper = new JsonMapper(); |
| 64 | + $jsonMapper->bEnforceMapType = false; |
| 65 | + $jsonMapper->bStrictNullTypes = false; |
| 66 | + $esDocument = $jsonMapper->map($jsonArray, $deserializeESDDocument); |
| 67 | + |
| 68 | + //add ESDocument to endpoint response and return the response |
| 69 | + $endpointResponse->esDocument = $esDocument; |
| 70 | + return $endpointResponse; |
| 71 | + }; |
| 72 | + |
| 73 | + $endpointResponse = APIv1HTTPRequest::sendESDocumentHTTPRequest(APIv1Constants::HTTP_REQUEST_METHOD_GET, APIv1Constants::API_ORG_ENDPOINT_SEARCH_CUSTOMER_ACCOUNT_RECORDS_ESD . APIv1Constants::API_PATH_SLASH . $apiOrgSession->getSessionID(), $endpointParams, $requestHeaders, "", null, $endpointTimeoutMilliseconds, $apiOrgSession->getLangBundle(), $endpointJSONReader, $endpointResponse); |
| 74 | + |
| 75 | + //check that the data was successfully retrieved |
| 76 | + if(strcasecmp($endpointResponse->result, APIv1EndpointResponse::ENDPOINT_RESULT_SUCCESS) != 0) |
| 77 | + { |
| 78 | + //check if the session still exists |
| 79 | + if(strcasecmp($endpointResponse->result, APIv1EndpointResponse::ENDPOINT_RESULT_CODE_ERROR_SESSION_INVALID) != 0){ |
| 80 | + //mark that the session has expired |
| 81 | + $apiOrgSession->markSessionExpired(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + catch(Exception $ex) |
| 87 | + { |
| 88 | + $endpointResponse->result = APIv1EndpointResponse::ENDPOINT_RESULT_FAILURE; |
| 89 | + $endpointResponse->result_code = APIv1EndpointResponse::ENDPOINT_RESULT_CODE_ERROR_UNKNOWN; |
| 90 | + $endpointResponse->result_message = $apiOrgSession->getLangBundle()->getString($endpointResponse->result_code) . "\n" . $ex.getMessage(); |
| 91 | + } |
| 92 | + |
| 93 | + return $endpointResponse; |
| 94 | + } |
| 95 | + } |
| 96 | +?> |
0 commit comments