forked from tijsverkoyen/bpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormHandler.php
211 lines (188 loc) · 5.81 KB
/
FormHandler.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
<?php
namespace TijsVerkoyen\Bpost;
/**
* bPost Form handler class
*
* @author Tijs Verkoyen <php-bpost@verkoyen.eu>
*/
class FormHandler
{
/**
* bPost instance
*
* @var Bpost
*/
private $bpost;
/**
* The parameters
*
* @var array
*/
private $parameters = array();
/**
* Create bPostFormHandler instance
*
* @param string $accountId
* @param string $passPhrase
*/
public function __construct($accountId, $passPhrase)
{
$this->bpost = new Bpost($accountId, $passPhrase);
}
/**
* Calculate the hash
*
* @return string
*/
private function getChecksum()
{
$keysToHash = array(
'accountId',
'action',
'costCenter',
'customerCountry',
'deliveryMethodOverrides',
'extraSecure',
'orderReference',
'orderWeight',
);
$base = 'accountId=' . $this->bpost->getAccountId() . '&';
foreach ($keysToHash as $key) {
if (isset($this->parameters[$key])) {
$base .= $key . '=' . $this->parameters[$key] . '&';
}
}
// add passphrase
$base .= $this->bpost->getPassPhrase();
// return the hash
return hash('sha256', $base);
}
/**
* Get the parameters
*
* @param bool $form
* @param bool $includeChecksum
* @return array
*/
public function getParameters($form = false, $includeChecksum = true)
{
$return = $this->parameters;
if ($form && isset($return['orderLine'])) {
foreach ($return['orderLine'] as $key => $value) {
$return['orderLine[' . $key . ']'] = $value;
}
unset($return['orderLine']);
}
if ($includeChecksum) {
$return['accountId'] = $this->bpost->getAccountId();
$return['checksum'] = $this->getChecksum();
}
return $return;
}
/**
* Set a parameter
*
* @param string $key
* @param mixed $value
*/
public function setParameter($key, $value)
{
switch ((string) $key) {
// limited values
case 'action':
case 'lang':
$allowedValues['action'] = array('START', 'CONFIRM');
$allowedValues['lang'] = array('NL', 'FR', 'EN', 'DE', 'Default');
if (!in_array($value, $allowedValues[$key])) {
throw new Exception(
'Invalid value (' . $value . ') for ' . $key . ', allowed values are: ' .
implode(', ', $allowedValues[$key]) . '.'
);
}
$this->parameters[$key] = $value;
break;
// maximum 2 chars
case 'customerCountry':
if (mb_strlen($value) > 2) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 2.'
);
}
$this->parameters[$key] = (string) $value;
break;
// maximum 8 chars
case 'customerStreetNumber':
case 'customerBox':
if (mb_strlen($value) > 8) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 8.'
);
}
$this->parameters[$key] = (string) $value;
break;
// maximum 20 chars
case 'customerPhoneNumber':
if (mb_strlen($value) > 20) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 20.'
);
}
$this->parameters[$key] = (string) $value;
break;
// maximum 32 chars
case 'customerPostalCode':
if (mb_strlen($value) > 32) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 32.'
);
}
$this->parameters[$key] = (string) $value;
break;
// maximum 40 chars
case 'customerFirstName':
case 'customerLastName':
case 'customerCompany':
case 'customerStreet':
case 'customerCity':
if (mb_strlen($value) > 40) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 40.'
);
}
$this->parameters[$key] = (string) $value;
break;
// maximum 50 chars
case 'orderReference':
case 'costCenter':
case 'customerEmail':
if (mb_strlen($value) > 50) {
throw new Exception(
'Invalid length for ' . $key . ', maximum is 50.'
);
}
$this->parameters[$key] = (string) $value;
break;
// integers
case 'orderTotalPrice':
case 'orderWeight':
$this->parameters[$key] = (int) $value;
break;
// array
case 'orderLine':
if (!isset($this->parameters[$key])) {
$this->parameters[$key] = array();
}
$this->parameters[$key][] = $value;
break;
// unknown
case 'deliveryMethodOverrides':
case 'extra':
case 'extraSecure':
case 'confirmUrl':
case 'cancelUrl':
case 'errorUrl':
default:
$this->parameters[$key] = $value;
}
}
}