1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Model;
use Vertex\Exception\ApiException\ConnectionFailureException;
use Vertex\Exception\ConfigurationException;
use Vertex\Services\TaxAreaLookup\RequestInterface;
use Vertex\Services\TaxAreaLookup\RequestInterfaceFactory;
use Vertex\Tax\Api\QuoteInterface;
use Vertex\Tax\Api\TaxAreaLookupInterface;
use Vertex\Tax\Model\ConfigurationValidator\Result;
use Vertex\Tax\Model\ConfigurationValidator\ResultFactory;
use Vertex\Tax\Model\ConfigurationValidator\ValidSampleRequestBuilder;
/**
* Validates the Credentials provided in the configuration
*/
class ConfigurationValidator
{
/** @var \Vertex\Tax\Model\Api\Data\AddressBuilder */
private $addressBuilder;
/** @var Config */
private $config;
/** @var RequestInterfaceFactory */
private $lookupRequestFactory;
/** @var QuoteInterface */
private $quote;
/** @var ResultFactory */
private $resultFactory;
/** @var ValidSampleRequestBuilder */
private $sampleRequestFactory;
/** @var TaxAreaLookupInterface */
private $taxAreaLookup;
/**
* @param Config $config
* @param \Vertex\Tax\Model\Api\Data\AddressBuilder $addressBuilder
* @param ValidSampleRequestBuilder $sampleRequestFactory
* @param ResultFactory $resultFactory
* @param QuoteInterface $quote
* @param TaxAreaLookupInterface $taxAreaLookup
* @param RequestInterfaceFactory $lookupRequestFactory
*/
public function __construct(
Config $config,
Api\Data\AddressBuilder $addressBuilder,
ValidSampleRequestBuilder $sampleRequestFactory,
ResultFactory $resultFactory,
QuoteInterface $quote,
TaxAreaLookupInterface $taxAreaLookup,
RequestInterfaceFactory $lookupRequestFactory
) {
$this->config = $config;
$this->addressBuilder = $addressBuilder;
$this->sampleRequestFactory = $sampleRequestFactory;
$this->resultFactory = $resultFactory;
$this->quote = $quote;
$this->taxAreaLookup = $taxAreaLookup;
$this->lookupRequestFactory = $lookupRequestFactory;
}
/**
* Validate configuration
*
* @param string $scopeType
* @param string|int $scopeCode
* @param bool $withoutCallValidation Skip validation that calls Vertex APIs
* @return Result
*/
public function execute($scopeType, $scopeCode, $withoutCallValidation = false)
{
/** @var Result $result */
$result = $this->resultFactory->create();
$this->validateConfigurationCompatibility($result, $scopeType, $scopeCode);
if (!$result->isValid()) {
return $result;
}
$this->validateConfigurationComplete($result, $scopeType, $scopeCode);
if (!$result->isValid()) {
return $result;
}
$this->validateAddressComplete($result, $scopeType, $scopeCode);
if (!$result->isValid()) {
return $result;
}
if ($withoutCallValidation) {
return $result;
}
$this->validateAddressLookup($result, $scopeType, $scopeCode);
if ($result->isValid()) {
$this->validateCalculationService($result, $scopeType, $scopeCode);
}
return $result;
}
/**
* Validates that the Company Address has been configured in the admin
*
* @param Result $result
* @param string $scopeType
* @param string|int $scopeCode
* @return Result
*/
private function validateAddressComplete(Result $result, $scopeType, $scopeCode)
{
if (!$this->config->getCompanyRegionId($scopeCode, $scopeType)) {
$missing[] = 'Company State';
}
if (!$this->config->getCompanyCountry($scopeCode, $scopeType)) {
$missing[] = 'Company Country';
}
if (!$this->config->getCompanyStreet1($scopeCode, $scopeType)) {
$missing[] = 'Company Street';
}
if (!$this->config->getCompanyCity($scopeCode, $scopeType) ||
!$this->config->getCompanyPostalCode($scopeCode, $scopeType)
) {
$missing[] = 'one of Company City or Postcode';
}
if (!empty($missing)) {
$result->setMessage('Address Incomplete, Missing: %1');
$result->setArguments([implode(', ', $missing)]);
$result->setValid(false);
} else {
$result->setValid(true);
}
return $result;
}
/**
* Validates the Company Address against the Lookup API
*
* @param Result $result
* @param string $scopeType
* @param string|int $scopeCode
* @return Result
*/
private function validateAddressLookup(Result $result, $scopeType, $scopeCode)
{
$street = [
$this->config->getCompanyStreet1($scopeCode, $scopeType),
$this->config->getCompanyStreet2($scopeCode, $scopeType)
];
$address = $this->addressBuilder->setStreet($street)
->setCity($this->config->getCompanyCity($scopeCode, $scopeType))
->setRegionId($this->config->getCompanyRegionId($scopeCode, $scopeType))
->setPostalCode($this->config->getCompanyPostalCode($scopeCode, $scopeType))
->setCountryCode($this->config->getCompanyCountry($scopeCode, $scopeType))
->build();
if ($address->getCountry() !== 'USA') {
// skip validation for non-US countries
$result->setValid(true);
return $result;
}
/** @var RequestInterface $request */
$request = $this->lookupRequestFactory->create();
$request->setPostalAddress($address);
$result->setValid(false);
try {
$this->taxAreaLookup->lookup($request, $scopeCode, $scopeType);
$result->setValid(true);
} catch (ConfigurationException $e) {
$result->setMessage('Unable to connect to Address Validation API');
} catch (ConnectionFailureException $e) {
$result->setMessage('Unable to connect to Address Validation API');
} catch (\Exception $e) {
$result->setMessage('Unable to validate address against API');
}
return $result;
}
/**
* Verify Vertex API connectivity by performing a live tax calculation request.
*
* @param Result $result
* @param string $scopeType
* @param string|int $scopeCode
* @return Result
*/
private function validateCalculationService(Result $result, $scopeType, $scopeCode)
{
$request = $this->sampleRequestFactory
->setScopeType($scopeType)
->setScopeCode($scopeCode)
->build();
$result->setValid(false);
try {
$this->quote->request($request, $scopeCode, $scopeType);
$result->setValid(true);
} catch (ConfigurationException $e) {
$result->setMessage('Unable to connect to Calculation API');
} catch (ConnectionFailureException $e) {
$result->setMessage('Unable to connect to Calculation API');
} catch (\Exception $e) {
$result->setMessage('Unable to perform quote request');
}
return $result;
}
/**
* Validates that Catalog Prices are not set to display including tax
*
* @param Result $result
* @param string $scopeType
* @param string|int $scopeCode
* @return Result
*/
private function validateConfigurationCompatibility(Result $result, $scopeType, $scopeCode)
{
if ($this->config->isVertexActive($scopeCode, $scopeType)
&& $this->config->isDisplayPriceInCatalogEnabled($scopeCode, $scopeType)) {
$result->setValid(false);
$result->setMessage('Automatically Disabled');
} else {
$result->setValid(true);
}
return $result;
}
/**
* Validates that Vertex API, Lookup API, and Trusted ID have been configured in the admin
*
* @param Result $result
* @param string $scopeType
* @param string|int $scopeCode
* @return Result
*/
private function validateConfigurationComplete(Result $result, $scopeType, $scopeCode)
{
$missing = [];
if (!$this->config->getVertexHost($scopeCode, $scopeType)) {
$missing[] = 'Vertex API URL';
}
if (!$this->config->getVertexAddressHost($scopeCode, $scopeType)) {
$missing[] = 'Address Lookup API URL';
}
if (!$this->config->getTrustedId($scopeCode, $scopeType)) {
$missing[] = 'Trusted ID';
}
if (!empty($missing)) {
$result->setMessage('Configuration Incomplete, Missing: %1');
$result->setArguments([implode(', ', $missing)]);
$result->setValid(false);
} else {
$result->setValid(true);
}
return $result;
}
}