-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankgiroPaymentsTransaction.php
668 lines (621 loc) · 13.4 KB
/
BankgiroPaymentsTransaction.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<?php
/**
* BankgiroPaymentTransaction
*
* A class made to contain a payment post parsed from Bankgiro Inbetalningar files.
*
* @copyright Copyright © 2012 Daniel Josefsson. All rights reserved.
* @license GPL v3
* @author Daniel Josefsson <[email protected]>
* @version v0.1
*/
class BankgiroPaymentsTransaction
{
/**
* Transaction code.
* Possible values "20", "21", "22", "23".
* Max length: 2
* @var int
*/
protected $_transactionCode;
/**
* Sender Bankgiro account number.
* Max length: 10
* @var int
*/
protected $_senderBankgiroAccountNumber;
/**
* Amount.
* Last two digits are ören.
* Max length 18
* @var int
*/
protected $_amount;
/**
* Reference
* Can be both int or string. Depends on $_referenceCode.
* Max length: 25
* @var mixed
*/
protected $_reference;
/**
* Reference code
* Length: 1
* Tells what kind of reference $_reference is.
* @var int
*/
protected $_referenceCode;
/**
* Channel code
* Length: 1
* @var int
*/
protected $_channelCode;
/**
* Serial number
* Contains a two year unique serial number.
* Is also used as image key.
* Length: 12
* @var string
*/
protected $_serialNumber;
/**
* Image marker
* Tells if a image is connected to the transaction.
* @var bool
*/
protected $_imageMarker;
/**
* Deduction code
* Used when transaction code is "21"
* Tells if there are any reminder of the invoice.
* Length: 1
* @var int
*/
protected $_deductionCode;
/**
* Information
* Stores data from information post type (25).
* Up to 99 information posts can be transmitted.
* Max length of string: 50
* @var array of strings
*/
protected $_information;
/**
* Name of the sender
* Two strings of names is stored together in this string.
* Max length of strings: 35
* Max total Length: 70
* @var string
*/
protected $_names;
/**
* Adress1 field stores address and postal number
* Address max length: 35
* Postal number max length: 9
* @var array
*/
protected $_address1;
/**
* Adress2 field stores city, country and countrycode
* City max length: 35
* Country max length: 35
* Country max code length: 2
* Country and country code are blank if it is a domestic transaction.
* @var array
*/
protected $_address2;
/**
* Sender organisation number
* Max length: 12
* @var int
*/
protected $_organisationNumber;
/**
* Array with extra references
* @var array of BankgiroPaymentTransaction
*/
protected $_extraReferences;
/**
*
* @since v0.2
*/
public function __construct()
{
$this->_names = array();
$this->_extraReferences = array();
}
/**
* Returns transaction code.
* @author Daniel Josefsson
* @since v0.1
*/
public function getTransactionCode()
{
return $this->_transactionCode;
}
/**
* Sets new transaction code.
* @author Daniel Josefsson
* @since v0.1
* @param int $transactionCode
* @return BankgiroPaymentsTransaction
*/
public function setTransactionCode( $transactionCode )
{
$this->_transactionCode = $transactionCode;
return $this;
}
/**
* Returns sender bankgiro account number.
* @author Daniel Josefsson
* @since v0.1
*/
public function getSenderBankgiroAccountNumber()
{
return $this->_senderBankgiroAccountNumber;
}
/**
* Sets new sender bankgiro account number.
* @author Daniel Josefsson
* @since v0.1
* @param int $senderBankgiroAccountNumber
* @return BankgiroPaymentsTransaction
*/
public function setSenderBankgiroAccountNumber( $senderBankgiroAccountNumber )
{
$this->_senderBankgiroAccountNumber = $senderBankgiroAccountNumber;
return $this;
}
/**
* Returns amount.
* @author Daniel Josefsson
* @since v0.1
*/
public function getAmount()
{
return $this->_amount;
}
/**
* Sets new amount.
* @author Daniel Josefsson
* @since v
* @param int $amount
* @return BankgiroPaymentsTransaction
*/
public function setAmount( $amount )
{
$this->_amount = $amount;
return $this;
}
/**
* Returns reference.
* @author Daniel Josefsson
* @since v0.1
*/
public function getReference()
{
return $this->_reference;
}
/**
* Sets new reference.
* @author Daniel Josefsson
* @since v0.1
* @param int $reference
* @return BankgiroPaymentsTransaction
*/
public function setReference( $reference )
{
$this->_reference = $reference;
return $this;
}
/**
* Returns reference code.
* @author Daniel Josefsson
* @since v0.1
*/
public function getReferenceCode()
{
return $this->_referenceCode;
}
/**
* Sets new reference code.
* @author Daniel Josefsson
* @since v0.1
* @param int $referenceCode
* @return BankgiroPaymentsTransaction
*/
public function setReferenceCode( $referenceCode )
{
$this->_referenceCode = $referenceCode;
return $this;
}
/**
* Returns channel code.
* @author Daniel Josefsson
* @since v0.1
*/
public function getChannelCode()
{
return $this->_channelCode;
}
/**
* Sets new channel code.
* @author Daniel Josefsson
* @since v0.1
* @param int $channelCode
* @return BankgiroPaymentsTransaction
*/
public function setChannelCode( $channelCode )
{
$this->_channelCode = $channelCode;
return $this;
}
/**
* Returns serial number.
* @author Daniel Josefsson
* @since v0.1
*/
public function getSerialNumber()
{
return $this->_serialNumber;
}
/**
* Sets new serial number.
* @author Daniel Josefsson
* @since v0.1
* @param int $serialNumber
* @return BankgiroPaymentsTransaction
*/
public function setSerialNumber( $serialNumber )
{
$this->_serialNumber = $serialNumber;
return $this;
}
/**
* Returns image marker.
* @author Daniel Josefsson
* @since v0.1
*/
public function getImageMarker()
{
return $this->_imageMarker;
}
/**
* Sets new image marker.
* @author Daniel Josefsson
* @since v0.1
* @param int $imageMarker
* @return BankgiroPaymentsTransaction
*/
public function setImageMarker( $imageMarker )
{
$this->_imageMarker = $imageMarker;
return $this;
}
/**
* Returns deduction code.
* @author Daniel Josefsson
* @since v0.1
*/
public function getDeductionCode()
{
return $this->_deductionCode;
}
/**
* Sets new deduction code.
* @author Daniel Josefsson
* @since v0.1
* @param int $deductionCode
* @return BankgiroPaymentsTransaction
*/
public function setDeductionCode( $deductionCode )
{
$this->_deductionCode = $deductionCode;
return $this;
}
/**
* Returns information.
* @author Daniel Josefsson
* @since v0.1
*/
public function getInformation()
{
return $this->_information;
}
/**
* Sets new information.
* @author Daniel Josefsson
* @since v0.1
* @param int $information
* @return BankgiroPaymentsTransaction
*/
public function setInformation( $information )
{
$this->_information[] = $information;
return $this;
}
/**
* Returns name.
* @author Daniel Josefsson
* @since v0.1
*/
public function getNames( $index )
{
return $this->_names[$index];
}
/**
* Sets new name.
* @author Daniel Josefsson
* @since v0.1
* @param int $name
* @return BankgiroPaymentsTransaction
*/
public function setNames( $names = array() )
{
$this->_names[] = $names;
return $this;
}
/**
* Returns address1.
* @author Daniel Josefsson
* @since v0.1
*/
public function getAddress1()
{
return $this->_address1;
}
/**
* Sets new address1.
* @author Daniel Josefsson
* @since v0.1
* @param int $address1
* @return BankgiroPaymentsTransaction
*/
public function setAddress1( $address1 )
{
$this->_address1 = $address1;
return $this;
}
/**
* Returns address2.
* @author Daniel Josefsson
* @since v0.1
*/
public function getAddress2()
{
return $this->_address2;
}
/**
* Sets new address2.
* @author Daniel Josefsson
* @since v0.1
* @param int $address2
* @return BankgiroPaymentsTransaction
*/
public function setAddress2( $address2 )
{
$this->_address2 = $address2;
return $this;
}
/**
* Returns organisation number.
* @author Daniel Josefsson
* @since v0.1
*/
public function getOrganisationNumber()
{
return $this->_organisationNumber;
}
/**
* Sets new organisation number.
* @author Daniel Josefsson
* @since v0.1
* @param int $organisationNumber
* @return BankgiroPaymentsTransaction
*/
public function setOrganisationNumber( $organisationNumber )
{
$this->_organisationNumber = $organisationNumber;
return $this;
}
/**
* Returns extra references.
* @author Daniel Josefsson
* @since v0.2
* @return array of BankgiroPaymentsTransaction
*/
public function getExtraReferences()
{
return $this->_extraReferences;
}
/**
* Sets new extra reference.
* @author Daniel Josefsson
* @since v0.2
* @param BankgiroPaymentsTransaction $extraReference
* @return BankgiroPaymentsTransaction $this
*/
public function setExtraReference( BankgiroPaymentsTransaction $extraReference )
{
$this->_extraReferences[] = $extraReference;
return $this;
}
/**
* Return index to the latest inserted extra reference.
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @return int $keyToExtraReference
*/
public function lastExtraReferenceIndex()
{
end($this->_extraReferences);
return key($this->_extraReferences);
}
/**
* Returns wanted extra reference
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param int $index
* @return BankgiroPaymentsTransaction $extraReference
*/
public function getExtraReference( $index )
{
return $this->_extraReferences[$index];
}
/**
* Add new extra reference.
* Increments reference count.
* @author Daniel Josefsson
* @since v0.2
* @param string $postType
* @return BankgiroPaymentsTransaction $this
*/
public function addExtraReference($postType)
{
$this->_extraReferences[] = new BankgiroPaymentsTransaction();
return $this;
}
/**
* Return the extra references count.
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @return int
*/
public function getExtraReferencesCount()
{
return sizeof($this->_extraReferences);
}
/**
* Parses line,given with the syntax of posts 20 to 29 in Bankgiro Inbetalningar.
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $line
* @return boolean
*/
public function parsePost( $line )
{
$return = false;
$postType = substr($line, 0, 2);
$postData = substr($line, 2);
switch ($postType)
{
case '20':
$return = $this->parsePaymentPost($line);
break;
case '21':
$return = $this->parseDeductionPost($line);
break;
case '22':
case '23':
$extraReferenceIndex = $this->addExtraReference($postType)->lastExtraReferenceIndex();
$return = $this->getExtraReference($extraReferenceIndex)->parsePaymentPost($line);
break;
case '25':
$return = $this->parseInformationPost($postData);
break;
case '26':
$return = $this->parseNamePost($postData);
break;
case '27':
$return = $this->parseAddress1Post($postData);
break;
case '28':
$return = $this->parseAddress2Post($postData);
break;
default:
break;
}
return $return;
}
/**
* Parses payment post (20).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $post
* @return bool
*/
private function parsePaymentPost( $post )
{
$this->setTransactionCode(substr($post, 0, 2));
$this->setSenderBankgiroAccountNumber((int) substr($post, 2, 10));
$this->setReferenceCode((int) substr($post, 55, 1));
$this->setReference(trim(substr($post, 12, 25), ' '));
$this->setAmount((int) substr($post, 37, 18));
$this->setChannelCode((int) substr($post, 56, 1));
$this->setSerialNumber(substr($post, 57, 12));
$this->setImageMarker((int) substr($post, 69, 1));
// Reserved placeholders (post[70-79]) are not used.
return true;
}
/**
* Parses deduction post (21).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $post
* @return bool
*/
private function parseDeductionPost( $post )
{
$this->parsePaymentPost($post);
$this->setDeductionCode((int) substr($post, 70, 1));
// Reserved placeholders (post[70-79]) are not used.
return true;
}
/**
* Parses information post (25).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $postData
* @return bool
*/
private function parseInformationPost( $postData )
{
$this->setInformation(trim(substr($postData, 0, 50), ' '));
// Reserved placeholders (postData[50-77]) are not used.
return true;
}
/**
* Parses name post (26).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $postData
* @return bool
*/
private function parseNamePost( $postData )
{
$this->setNames(array( trim(substr($postData, 0, 35), ' '),
trim(substr($postData, 35, 35), ' ')));
// Reserved placeholders (postData[70-77]) are not used.
return true;
}
/**
* Parses first address post (27).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $postData
* @return bool
*/
private function parseAddress1Post( $postData )
{
$this->setAddress1(array( trim(substr($postData, 0, 35), ' '),
trim(substr($postData, 35, 9), ' ')));
// Reserved placeholders (postData[44-77]) are not used.
return true;
}
/**
* Parses second address post (28).
* @author Daniel Josefsson <[email protected]>
* @since v0.2
* @param string $postData
* @return bool
*/
private function parseAddress2Post( $postData )
{
$this->setAddress2(array( trim(substr($postData, 0, 35), ' '),
trim(substr($postData, 35, 35), ' '),
trim(substr($postData, 70, 2))) );
// Reserved placeholders (postData[72-77]) are not used.
return true;
}
}