diff --git a/Api/CreditRating/CreditRatingInterface.php b/Api/CreditRating/CreditRatingInterface.php new file mode 100644 index 000000000..1ee6a0ab2 --- /dev/null +++ b/Api/CreditRating/CreditRatingInterface.php @@ -0,0 +1,69 @@ +setMethod(RequestMethod::POST); + + $response = Rest::request($request); + + return $response->header->status === 200 + ? ($response->get('token') ?? '') + : ''; + } + + public function findCompanies( + string $token, + string $name = '', + string $address = '', + string $street = '', + string $city = '', + string $postal = '', + string $province = '', + string $phoneNo = '', + string $houseNo = '', + string $vatNo = '', + string $localRegistrationNo = '', + array $countries = [], + int $threashold = 0, + ) : array + { + $url = '/companies'; + if ($threashold > 0) { + $url .= '/matches'; + } + + $request = new HttpRequest(new HttpUri(self::API_URL . $url)); + $request->setMethod(RequestMethod::GET); + + $request->header->set('Authorization', $token); + + $request->setData('page', 1); + $request->setData('pageSize', 100); + $request->setData('language', 'en'); + + if ($threashold > 0) { + $request->setData('matchThreshold', $threashold); + $request->setData('country', \implode(',', $countries)); + } else { + $request->setData('countries', empty($countries) ? 'PLC' : \implode(',', $countries)); + } + + if ($localRegistrationNo !== '') { + $request->setData('regNo', $localRegistrationNo); + } + + if ($vatNo !== '') { + $request->setData('vatNo', $vatNo); + } + + if ($name !== '') { + $request->setData('name', $name); + } + + if ($address !== '') { + $request->setData('address', $address); + } + + if ($street !== '') { + $request->setData('street', $street); + } + + if ($province !== '') { + $request->setData('province', $province); + } + + if ($postal !== '') { + $request->setData('postal', $postal); + } + + if ($city !== '') { + $request->setData('city', $city); + } + + if ($houseNo !== '') { + $request->setData('houseNo', $houseNo); + } + + if ($phoneNo !== '') { + $request->setData('phoneNo', $phoneNo); + } + + $response = Rest::request($request); + + return $response->get('companies') ?? ($response->get('matchedCompanies') ?? []); + } + + public function creditReport(string $token, string $id, string $template = 'full', string $language = 'en') : array + { + $url = '/companies/' . $id; + + $request = new HttpRequest(new HttpUri(self::API_URL . $url)); + $request->setMethod(RequestMethod::GET); + + $request->header->set('Authorization', $token); + + $request->setData('connectId', $id); + $request->setData('language', $language); + $request->setData('template', $template); + + $response = Rest::request($request); + + return $response->get('report') ?? []; + } + + public function investigate( + string $token, + string $ownName = '', + string $ownCompanyName = '', + string $ownCompanyRegistrationNo = '', + string $ownEmail = '', + string $name = '', + string $address = '', + string $street = '', + string $city = '', + string $postal = '', + string $province = '', + string $phoneNo = '', + string $houseNo = '', + string $vatNo = '', + string $localRegistrationNo = '', + string $country = '' + ) : string + { + $url = '/freshinvestigations'; + + $request = new HttpRequest(new HttpUri(self::API_URL . $url)); + $request->setMethod(RequestMethod::POST); + + $request->header->set('Authorization', $token); + + $request->setData('contactInfo', [ + 'name' => $ownName, + 'company' => [ + 'name' => $ownCompanyName, + 'number' => $ownCompanyRegistrationNo, + ], + 'emailAddress' => $ownEmail, + 'searchCriteria' => [ + 'name' => $name, + 'address' => [ + 'simple' => empty($address) ? null : $address, + 'postcode' => empty($postal) ? null : $postal, + 'city' => empty($city) ? null : $city, + ], + 'regNo' => empty($$localRegistrationNo) ? null : $$localRegistrationNo, + 'vatNo' => empty($vatNo) ? null : $vatNo, + 'countryCode' => $country, + ] + ]); + + $response = Rest::request($request); + + return $response->get('orderID') ?? ''; + } + + public function showInvestigations(string $token, \DateTime $start) : array + { + $url = '/freshinvestigations'; + + $request = new HttpRequest(new HttpUri(self::API_URL . $url)); + $request->setMethod(RequestMethod::GET); + + $request->header->set('Authorization', $token); + + $request->setData('page', 1); + $request->setData('pageSize', 100); + $request->setData('createdSince', $start->format('c')); + + $response = Rest::request($request); + + return $response->get('orders') ?? []; + } + + public function getInvestigation(string $token, string $id) : array + { + $url = '/freshinvestigations/' . $id; + + $request = new HttpRequest(new HttpUri(self::API_URL . $url)); + $request->setMethod(RequestMethod::GET); + + $request->header->set('Authorization', $token); + + $request->setData('orderId', $id); + + $response = Rest::request($request); + + return $response->toArray(); + } +} diff --git a/Message/Http/Rest.php b/Message/Http/Rest.php index 08d1449f4..d62c13f3b 100755 --- a/Message/Http/Rest.php +++ b/Message/Http/Rest.php @@ -119,6 +119,13 @@ final class Rest $header = \explode(':', $header, 2); if (\count($header) < 2) { + $response->header->set('', $line = \trim($header[0])); + + if (\stripos(\strtoupper($line), 'HTTP/') === 0) { + $statusCode = \explode(' ', $line, 3); + $response->header->status = (int) $statusCode[1]; + } + return $length; } @@ -137,7 +144,12 @@ final class Rest \curl_close($curl); - $response->set('', \substr(\is_bool($result) ? '' : $result, $len === false ? 0 : $len)); + $raw = \substr(\is_bool($result) ? '' : $result, $len === false ? 0 : $len); + if (\stripos(\implode('', $response->header->get('content-type')), MimeType::M_JSON) !== false) { + $response->setResponse(\json_decode($raw, true)); + } else { + $response->set('', $raw); + } return $response; }