SerbianVat.php in Commerce Core 8.2
File
modules/tax/tests/modules/commerce_tax_test/src/Plugin/Commerce/TaxNumberType/SerbianVat.php
View source
<?php
namespace Drupal\commerce_tax_test\Plugin\Commerce\TaxNumberType;
use Drupal\commerce_tax\Plugin\Commerce\TaxNumberType\TaxNumberTypeWithVerificationBase;
use Drupal\commerce_tax\Plugin\Commerce\TaxNumberType\VerificationResult;
class SerbianVat extends TaxNumberTypeWithVerificationBase {
public function validate($tax_number) {
return is_numeric($tax_number) && $tax_number < 1000;
}
public function doVerify($tax_number) {
$timestamp = $this->time
->getRequestTime();
if ($tax_number == '190') {
return VerificationResult::unknown($timestamp, [
'error' => 'http_429',
]);
}
if ($tax_number % 2 === 0) {
return VerificationResult::failure($timestamp);
}
else {
return VerificationResult::success($timestamp, [
'name' => 'John Smith',
'nonce' => mt_rand(0, 1000),
]);
}
}
public function renderVerificationResult(VerificationResult $result) {
$errors = [
'http_429' => $this
->t('Too many requests.'),
];
$data = $result
->getData();
$element = [];
if (isset($data['error'])) {
$error = $data['error'];
$element['error'] = [
'#type' => 'item',
'#title' => $this
->t('Error'),
'#plain_text' => isset($errors[$error]) ? $errors[$error] : $error,
];
}
if (isset($data['name'])) {
$element['name'] = [
'#type' => 'item',
'#title' => $this
->t('Name'),
'#plain_text' => $data['name'],
];
}
return $element;
}
}