-
Notifications
You must be signed in to change notification settings - Fork 55
/
CSRRequest.php
254 lines (198 loc) · 6.03 KB
/
CSRRequest.php
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
<?php
namespace Salla\ZATCA\Models;
use Salla\ZATCA\Exception\CSRValidationException;
class CSRRequest
{
const SANDBOX = 'sandbox';
const SIMULATION = 'simulation';
const PRODUCTION = 'production';
/**
* EGS Serial number (1-SolutionName|2-ModelOrVersion|3-serialNumber)
*
* @var string
*/
protected $serial_number;
/**
* Invoice type (TSCZ)(1 = supported, 0 not supported) (Tax, Simplified, future use, future use)
*
* @var string
*/
protected $invoiceType = 1100;
/**
* Common name (EGS TaxPayer PROVIDED ID [FREE TEXT])
*
* @var string
*/
protected $commonName;
/**
* Organization name (Tax payer name)
*
* @var string
*/
protected $organizationName;
/**
* Organization Unit (Branch name)
*
* @var string
*/
protected $organizationalUnitName;
/**
* ISO2 country code is required with US as default
*
* @var string
*/
protected $countryName;
/**
* VAT Registration number of TaxPayer (Organization identifier [15 digits begins with 3 and ends with 3])
*
* @var string
*/
protected $UID;
/**
* Location (branch address or website)
*
* @var string
*/
protected $registeredAddress;
/**
* Industry (industry sector name)
*
* @var string
*/
protected $businessCategory = 'company';
/**
* the current Active environment in zatca
* will support three modes (sandbox, simulation, production)
* @var string
*/
protected $currentEnv = 'sandbox';
public function setCommonName(string $commonName): self
{
$this->commonName = $commonName;
return $this;
}
public function setOrganizationName(string $organizationName): self
{
$this->organizationName = $organizationName;
return $this;
}
public function setOrganizationalUnitName(string $organizationalUnitName): self
{
if (strpos($this->getUID(), '1', 10) == 1 && strlen($organizationalUnitName) != 10) {
throw new CSRValidationException('The Organization Unit Name Must Match this (If 11th digit of Organization Identifier(UID) = 1 then needs to be 10 digit number)', 422);
}
$this->organizationalUnitName = $organizationalUnitName;
return $this;
}
public function setCountryName(string $countryName): self
{
if (strlen($countryName) !== 2) {
throw new CSRValidationException('The Country name must be Two chars only', 422);
}
$this->countryName = $countryName;
return $this;
}
public function setUID(string $UID): self
{
if (strlen($UID) !== 15 || substr($UID, 0, 1) != '3' || substr($UID, -1, 1) != '3') {
throw new CSRValidationException('The Organization Identifier must be 15 digits, starting andending with 3 ', 422);
}
$this->UID = $UID;
return $this;
}
public function setRegisteredAddress(string $registeredAddress): self
{
$this->registeredAddress = $registeredAddress;
return $this;
}
public function setBusinessCategory(string $businessCategory): self
{
$this->businessCategory = $businessCategory;
return $this;
}
public static function make()
{
return new static();
}
public function setSerialNumber(string $solutionName, string $version, string $serialNumber): CSRRequest
{
$this->serial_number = sprintf('1-%s|2-%s|3-%s', $solutionName, $version, $serialNumber);
return $this;
}
public function setInvoiceType(bool $taxInvoice, bool $simplified): CSRRequest
{
$this->invoiceType = sprintf('%s%s00', (int)$taxInvoice, (int)$simplified);
return $this;
}
public function setCurrentZatcaEnv(string $currentEnv): CSRRequest
{
$this->currentEnv = $currentEnv;
return $this;
}
public function isSandboxEnv(): bool
{
return $this->currentEnv === self::SANDBOX;
}
public function isSimulationEnv(): bool
{
return $this->currentEnv === self::SIMULATION;
}
public function isProduction(): bool
{
return $this->currentEnv === self::PRODUCTION;
}
public function toArray(): array
{
return [
'dn' => [
"CN" => $this->getCommonName(),
"organizationName" => $this->getOrganizationName(),
"organizationalUnitName" => $this->getOrganizationalUnitName(),
"C" => $this->getCountry()
],
'subject' => [
"SN" => $this->getSerialNumber(),
"UID" => $this->getUID(),
"title" => $this->getInvoiceType(),
"registeredAddress" => $this->getRegisteredAddress(),
"businessCategory" => $this->getBusinessCategory()
]
];
}
public function getSerialNumber(): string
{
return $this->serial_number;
}
public function getCommonName(): string
{
return $this->commonName;
}
public function getOrganizationName(): string
{
return $this->organizationName;
}
public function getOrganizationalUnitName(): string
{
return $this->organizationalUnitName;
}
public function getUID(): string
{
return $this->UID;
}
public function getInvoiceType(): string
{
return $this->invoiceType;
}
private function getRegisteredAddress(): string
{
return $this->registeredAddress;
}
public function getBusinessCategory(): string
{
return $this->businessCategory;
}
public function getCountry(): string
{
return $this->countryName;
}
}