forked from RikudouSage/QrPaymentCZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QrPayment.php
301 lines (264 loc) · 7.22 KB
/
QrPayment.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace rikudou\CzQrPayment;
use function class_exists;
use Endroid\QrCode\QrCode;
/**
* Class QrPayment
* @package rikudou\CzQrPayment
*/
class QrPayment {
/** @var int|string $account */
protected $account;
/** @var int $bank */
protected $bank;
/** @var int $variableSymbol */
public $variableSymbol;
/** @var int $specificSymbol */
public $specificSymbol;
/** @var int $constantSymbol */
public $constantSymbol;
/** @var string $currency */
public $currency = "CZK";
/** @var string $comment */
public $comment = "";
/** @var int $repeat */
public $repeat = 7;
/** @var string $internalId */
public $internalId;
/** @var string|\DateTime $dueDate */
public $dueDate;
/** @var float $amount */
public $amount;
/** @var string $country */
public $country = 'CZ';
/** @var string|null $iban */
protected $iban = null;
/**
* QrPayment constructor.
* Sets account and bank. Allows to specify options in array in format:
* property_name => value
*
* @param int|string $account
* @param int|string $bank
* @param array $options
*/
public function __construct($account, $bank, array $options = null) {
$this->account = $account;
$this->bank = $bank;
if ($options) {
$this->setOptions($options);
}
}
/**
* Specifies options in array in format:
* property_name => value
*
* Throws exception if any of the fields contains asterisk symbol
*
* @param array $options
* @return $this
* @throws \rikudou\CzQrPayment\QrPaymentException
*/
public function setOptions(array $options) {
foreach ($options as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
$this->checkProperties();
return $this;
}
/**
* Converts account and bank numbers to IBAN
* @throws \rikudou\CzQrPayment\QrPaymentException
* @return string
*/
public function getIBAN() {
$this->checkProperties();
if(!is_null($this->iban)) {
return $this->iban;
}
$this->country = strtoupper($this->country);
$part1 = ord($this->country[0]) - ord('A') + 10;
$part2 = ord($this->country[1]) - ord('A') + 10;
$numeric = sprintf("%04d%016d%d%d00", $this->bank, $this->account, $part1, $part2);
$mod = "";
foreach (str_split($numeric) as $n) {
$mod = ($mod . $n) % 97;
}
$this->iban = sprintf("%.2s%02d%04d%016d", $this->country, 98 - $mod, $this->bank, $this->account);
return $this->iban;
}
/**
* Returns QR Payment string
* Throws exception if any of the fields contains asterisk symbol
* or if the date is not in format understandable by strtotime() function
*
* @return string
* @throws \rikudou\CzQrPayment\QrPaymentException
*/
public function getQrString() {
$this->checkProperties();
$qr = "SPD*1.0*";
$qr .= sprintf("ACC:%s*", $this->getIBAN());
$qr .= sprintf("AM:%.2f*", $this->amount);
$qr .= sprintf("CC:%s*", strtoupper($this->currency));
if ($this->repeat) {
$qr .= sprintf("X-PER:%d*", $this->repeat);
}
if ($this->comment) {
$qr .= sprintf("MSG:%.60s*", $this->comment);
}
if ($this->internalId) {
$qr .= sprintf("X-ID:%d*", $this->internalId);
}
if ($this->variableSymbol) {
$qr .= sprintf("X-VS:%d*", $this->variableSymbol);
}
if ($this->specificSymbol) {
$qr .= sprintf("X-SS:%d*", $this->specificSymbol);
}
if ($this->constantSymbol) {
$qr .= sprintf("X-KS:%d*", $this->constantSymbol);
}
if (($dueDate = $this->getDueDate())) {
$qr .= sprintf("DT:%s*", $dueDate->format('Ymd'));
}
return substr($qr,0,-1);
}
/**
* Checks whether the due date is set.
* Throws exception if the date format cannot be parsed by strtotime() func
*
* @return \DateTime|null
* @throws \rikudou\CzQrPayment\QrPaymentException
*/
protected function getDueDate() {
if (!$this->dueDate) {
return null;
}
if (!$this->dueDate instanceof \DateTime && !@strtotime($this->dueDate)) {
throw new QrPaymentException("Error: Due date value ($this->dueDate) cannot be transformed, you must ensure that the due date value is acceptable by strtotime()", QrPaymentException::ERR_DATE);
}
return $this->dueDate instanceof \DateTime ? $this->dueDate : new \DateTime($this->dueDate);
}
/**
* Checks all properties for asterisk and throws exception if asterisk
* is found
* @throws \rikudou\CzQrPayment\QrPaymentException
*/
protected function checkProperties() {
foreach (get_object_vars($this) as $property => $value) {
if (strpos($value,"*") !== false) {
throw new QrPaymentException("Error: properties cannot contain asterisk (*). Property $property contains it.", QrPaymentException::ERR_ASTERISK);
}
}
}
/**
* Return QrCode object with QrString set, for more info see Endroid QrCode
* documentation
*
* @param bool $setPngHeader
* @return \Endroid\QrCode\QrCode
* @throws \rikudou\CzQrPayment\QrPaymentException
*/
public function getQrImage($setPngHeader = false) {
if (!class_exists("Endroid\QrCode\QrCode")) {
throw new QrPaymentException("Error: library Endroid\QrCode is not loaded.", QrPaymentException::ERR_MISSING_LIBRARY);
}
if ($setPngHeader) {
header("Content-type: image/png");
}
$qr = new QrCode;
return $qr->setText($this->getQrString());
}
/**
* @param string $iban
* @return static
*/
public static function fromIBAN($iban) {
$instance = new static(0,0);
$instance->iban = $iban;
return $instance;
}
/**
* @param int $variableSymbol
* @return QrPayment
*/
public function setVariableSymbol($variableSymbol) {
$this->variableSymbol = $variableSymbol;
return $this;
}
/**
* @param int $specificSymbol
* @return QrPayment
*/
public function setSpecificSymbol($specificSymbol) {
$this->specificSymbol = $specificSymbol;
return $this;
}
/**
* @param int $constantSymbol
* @return QrPayment
*/
public function setConstantSymbol($constantSymbol) {
$this->constantSymbol = $constantSymbol;
return $this;
}
/**
* @param string $currency
* @return QrPayment
*/
public function setCurrency($currency) {
$this->currency = $currency;
return $this;
}
/**
* @param string $comment
* @return QrPayment
*/
public function setComment($comment) {
$this->comment = $comment;
return $this;
}
/**
* @param int $repeat
* @return QrPayment
*/
public function setRepeat($repeat) {
$this->repeat = $repeat;
return $this;
}
/**
* @param string $internalId
* @return QrPayment
*/
public function setInternalId($internalId) {
$this->internalId = $internalId;
return $this;
}
/**
* @param \DateTime|string $dueDate
* @return QrPayment
*/
public function setDueDate($dueDate) {
$this->dueDate = $dueDate;
return $this;
}
/**
* @param float $amount
* @return QrPayment
*/
public function setAmount($amount) {
$this->amount = $amount;
return $this;
}
/**
* @param string $country
* @return QrPayment
*/
public function setCountry($country) {
$this->country = $country;
return $this;
}
}