1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bpost\BpostApiClient; |
4
|
|
|
|
5
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* bPost Form handler class |
10
|
|
|
* |
11
|
|
|
* @author Tijs Verkoyen <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class FormHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* bPost instance |
17
|
|
|
* |
18
|
|
|
* @var Bpost |
19
|
|
|
*/ |
20
|
|
|
private $bpost; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The parameters |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $parameters = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create bPostFormHandler instance |
31
|
|
|
* |
32
|
|
|
* @param string $accountId |
33
|
|
|
* @param string $passPhrase |
34
|
|
|
* @param string $apiUrl |
35
|
|
|
*/ |
36
|
|
|
public function __construct($accountId, $passPhrase, $apiUrl = Bpost::API_URL) |
37
|
|
|
{ |
38
|
|
|
$this->bpost = new Bpost($accountId, $passPhrase, $apiUrl); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Calculate the hash |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
private function getChecksum() |
47
|
|
|
{ |
48
|
|
|
$keysToHash = array( |
49
|
|
|
'accountId', |
50
|
|
|
'action', |
51
|
|
|
'costCenter', |
52
|
|
|
'customerCountry', |
53
|
|
|
'deliveryMethodOverrides', |
54
|
|
|
'extraSecure', |
55
|
|
|
'orderReference', |
56
|
|
|
'orderWeight', |
57
|
|
|
); |
58
|
|
|
$base = 'accountId=' . $this->bpost->getAccountId() . '&'; |
59
|
|
|
|
60
|
|
|
foreach ($keysToHash as $key) { |
61
|
|
|
if (isset($this->parameters[$key])) { |
62
|
|
|
if (!is_array($this->parameters[$key])) { |
63
|
|
|
$base .= $key . '=' . $this->parameters[$key] . '&'; |
64
|
|
|
} else { |
65
|
|
|
foreach ($this->parameters[$key] as $entry) { |
66
|
|
|
$base .= $key . '=' . $entry . '&'; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// add passphrase |
73
|
|
|
$base .= $this->bpost->getPassPhrase(); |
74
|
|
|
|
75
|
|
|
// return the hash |
76
|
|
|
return hash('sha256', $base); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the parameters |
81
|
|
|
* |
82
|
|
|
* @param bool $form |
83
|
|
|
* @param bool $includeChecksum |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getParameters($form = false, $includeChecksum = true) |
88
|
|
|
{ |
89
|
|
|
$return = $this->parameters; |
90
|
|
|
|
91
|
|
|
if ($form && isset($return['orderLine'])) { |
92
|
|
|
foreach ($return['orderLine'] as $key => $value) { |
93
|
|
|
$return['orderLine[' . $key . ']'] = $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
unset($return['orderLine']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($includeChecksum) { |
100
|
|
|
$return['accountId'] = $this->bpost->getAccountId(); |
101
|
|
|
$return['checksum'] = $this->getChecksum(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set a parameter |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* @param mixed $value |
112
|
|
|
* |
113
|
|
|
* @throws BpostInvalidValueException |
114
|
|
|
* @throws BpostInvalidLengthException |
115
|
|
|
*/ |
116
|
|
|
public function setParameter($key, $value) |
117
|
|
|
{ |
118
|
|
|
switch ((string) $key) { |
119
|
|
|
// limited values |
120
|
|
|
case 'action': |
121
|
|
|
case 'lang': |
122
|
|
|
$allowedValues = array(); |
123
|
|
|
$allowedValues['action'] = array('START', 'CONFIRM'); |
124
|
|
|
$allowedValues['lang'] = array('NL', 'FR', 'EN', 'DE', 'Default'); |
125
|
|
|
|
126
|
|
|
if (!in_array($value, $allowedValues[$key])) { |
127
|
|
|
throw new BpostInvalidValueException($key, $value, $allowedValues[$key]); |
128
|
|
|
} |
129
|
|
|
$this->parameters[$key] = $value; |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
// maximum 2 chars |
133
|
|
|
case 'customerCountry': |
134
|
|
|
if (mb_strlen($value) > 2) { |
135
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 2); |
136
|
|
|
} |
137
|
|
|
$this->parameters[$key] = (string) $value; |
138
|
|
|
break; |
139
|
|
|
|
140
|
|
|
// maximum 8 chars |
141
|
|
|
case 'customerStreetNumber': |
142
|
|
|
case 'customerBox': |
143
|
|
|
if (mb_strlen($value) > 8) { |
144
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 8); |
145
|
|
|
} |
146
|
|
|
$this->parameters[$key] = (string) $value; |
147
|
|
|
break; |
148
|
|
|
|
149
|
|
|
// maximum 20 chars |
150
|
|
|
case 'customerPhoneNumber': |
151
|
|
|
if (mb_strlen($value) > 20) { |
152
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 20); |
153
|
|
|
} |
154
|
|
|
$this->parameters[$key] = (string) $value; |
155
|
|
|
break; |
156
|
|
|
|
157
|
|
|
// maximum 32 chars |
158
|
|
|
case 'customerPostalCode': |
159
|
|
|
if (mb_strlen($value) > 32) { |
160
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 32); |
161
|
|
|
} |
162
|
|
|
$this->parameters[$key] = (string) $value; |
163
|
|
|
break; |
164
|
|
|
|
165
|
|
|
// maximum 40 chars |
166
|
|
|
case 'customerFirstName': |
167
|
|
|
case 'customerLastName': |
168
|
|
|
case 'customerCompany': |
169
|
|
|
case 'customerStreet': |
170
|
|
|
case 'customerCity': |
171
|
|
|
if (mb_strlen($value) > 40) { |
172
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 40); |
173
|
|
|
} |
174
|
|
|
$this->parameters[$key] = (string) $value; |
175
|
|
|
break; |
176
|
|
|
|
177
|
|
|
// maximum 50 chars |
178
|
|
|
case 'orderReference': |
179
|
|
|
case 'costCenter': |
180
|
|
|
case 'customerEmail': |
181
|
|
|
if (mb_strlen($value) > 50) { |
182
|
|
|
throw new BpostInvalidLengthException($key, mb_strlen($value), 50); |
183
|
|
|
} |
184
|
|
|
$this->parameters[$key] = (string) $value; |
185
|
|
|
break; |
186
|
|
|
|
187
|
|
|
// integers |
188
|
|
|
case 'orderTotalPrice': |
189
|
|
|
case 'orderWeight': |
190
|
|
|
$this->parameters[$key] = (int) $value; |
191
|
|
|
break; |
192
|
|
|
|
193
|
|
|
// array |
194
|
|
|
case 'orderLine': |
195
|
|
|
if (!isset($this->parameters[$key])) { |
196
|
|
|
$this->parameters[$key] = array(); |
197
|
|
|
} |
198
|
|
|
$this->parameters[$key][] = $value; |
199
|
|
|
break; |
200
|
|
|
|
201
|
|
|
// unknown |
202
|
|
|
case 'deliveryMethodOverrides': |
203
|
|
|
case 'extra': |
204
|
|
|
case 'extraSecure': |
205
|
|
|
case 'confirmUrl': |
206
|
|
|
case 'cancelUrl': |
207
|
|
|
case 'errorUrl': |
208
|
|
|
default: |
209
|
|
|
if (is_array($value)) { |
210
|
|
|
sort($value); |
211
|
|
|
} |
212
|
|
|
$this->parameters[$key] = $value; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|