Skip to content

Commit cbc96ca

Browse files
authored
Update ValidateAddress.php
1 parent 4be2225 commit cbc96ca

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/ValidateAddress.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ValidateAddress extends Auth
1717
private ?string $politicalDivision1;
1818
private ?string $postcodePrimaryLow;
1919
private ?string $countryCode;
20+
private object $apiResponse;
2021

2122
public function __construct()
2223
{
@@ -79,11 +80,11 @@ public function validate(string $client_id, string $client_secret): array
7980
"Authorization: Bearer $access_token",
8081
"Content-Type: application/json"
8182
]);
82-
$httpClient->setPayload(json_encode($this->getPayLoad()));
83+
$httpClient->setPayload($this->getRequest());
8384
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/addressvalidation/" . self::VERSION . "/" . self::REQUEST_OPTION . $queryParams);
8485
$httpClient->setMethod("POST");
85-
$res = $httpClient->fetch();
86-
86+
$this->apiResponse = $res = $httpClient->fetch();
87+
8788
if (!isset($res->XAVResponse)) {
8889
if (isset($res->response)) {
8990
$error = $res->response->errors[0]->message;
@@ -98,12 +99,18 @@ public function validate(string $client_id, string $client_secret): array
9899
}
99100

100101
$addresses = $this->getAddresses($res->XAVResponse->Candidate);
101-
return ['status' => 'success', 'addresses' => $addresses];
102+
103+
$return_res = ['status' => 'success', 'addresses' => $addresses];
104+
if (isset($res->XAVResponse->AddressClassification)) {
105+
$return_res['address_classification'] = $res->XAVResponse->AddressClassification;
106+
}
107+
108+
return $return_res;
102109
}
103110

104-
private function getPayLoad(): array
111+
public function getRequest(): string
105112
{
106-
return [
113+
return json_encode([
107114
"XAVRequest" => [
108115
"AddressKeyFormat" => [
109116
"AddressLine" => $this->addressLines,
@@ -113,7 +120,7 @@ private function getPayLoad(): array
113120
"CountryCode" => $this->countryCode
114121
]
115122
]
116-
];
123+
]);
117124
}
118125

119126
private function getAddresses(array $candidates): array
@@ -129,6 +136,7 @@ private function getAddresses(array $candidates): array
129136
'region' => $addressObj->Region,
130137
'country' => $addressObj->CountryCode
131138
];
139+
$address['address_classification'] = $candObj->AddressClassification;
132140
array_push($addresses, $address);
133141
}
134142

@@ -140,4 +148,9 @@ public function setMode(string $mode): self
140148
parent::setMode($mode);
141149
return $this;
142150
}
151+
152+
public function getResponse(): string
153+
{
154+
return json_encode($this->apiResponse);
155+
}
143156
}

0 commit comments

Comments
 (0)