-
Notifications
You must be signed in to change notification settings - Fork 7
/
transaction.php
162 lines (133 loc) · 3.49 KB
/
transaction.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
<?php
/**
*
* Transaction class
*
* Used by gateway to create payment form
*
*/
class FpiapiTransaction {
private $uid; // Unique numeric ID that identifies this transaction
private $sum; // Amount to pay
private $dueDate; // Due date (only a handful of banks support this)
private $referenceNumber; // Reference number calculated from the uid
// Reference padding length; pad reference number with zeroes up to given number of characters
static protected $referencePaddingLength;
// Reference base number; added to uid before calculated ref.num.
static protected $referenceBaseNumber;
/**
* Constructor
*/
public function __construct() {
}
/**
* setSum
* Set sum for the transaction
* @param string $s
*/
public function setSum($s) {
$this->sum = $s;
}
/**
* getSum
* Get the sum for the transaction
*/
public function getSum() {
if (is_float($this->sum))
return number_format($this->sum, 2, ',', '');
return str_replace(".", ",", $this->sum);
}
/**
* setUid
* Set the transactions unique indentifier
* This value is used to calculate the reference number
* @param string $uid
*/
public function setUid($uid) {
$this->uid = $uid;
}
/**
* getUid
* Get the transactions unique identifier
* Enter description here ...
*/
public function getUid() {
return $this->uid;
}
/**
* setDueDate
* Set due date for the transaction
* @param string with Y-m-d date $date
*/
public function setDueDate($date) {
$this->dueDate = $date;
}
/**
* getDueDate
* Get due date
*/
public function getDueDate() {
return $this->dueDate;
}
/**
* _calculateCheckSum()
* Enter description here ...
* @param $reference
*/
private function _calculateCheckSum($reference) {
// from http://pastebin.com/f69d0ce0f
$multipliers = array(7,3,1);
$length = strlen($reference);
$reference = str_split($reference);
$sum = 0;
for ($i = $length - 1; $i >= 0; --$i) {
$sum += $reference[$i] * $multipliers[($length - 1 - $i) % 3];
}
return (10 - $sum % 10) % 10;
}
/**
* getReferenceNumber()
* Calculate reference number for the given number
* @param $reference
*/
public function getReferenceNumber() {
$reference = $this->uid;
$reference += $this->getReferenceBaseNumber();
$str = $reference.$this->_calculateCheckSum($reference);
$padding = $this->getReferencePaddingLength();
if ($padding > 0)
return str_pad($str, $padding, "0", STR_PAD_LEFT);
return $str;
}
/**
* setReferencePaddingLength
* Set the length of the reference number zero padding
* Example: if set to 4 and reference number is 11, it becomes 0011
* @param unknown_type $n
*/
public function setReferencePaddingLength($n) {
self::$referencePaddingLength = $n;
}
/**
* getReferencePaddingLength
* Returns the reference padding length
*/
public function getReferencePaddingLength() {
return self::$referencePaddingLength;
}
/**
* setReferenceBaseNumber
* Set the reference base number. This number will be added to the UID of the transactions
* @param $n
*/
public function setReferenceBaseNumber($n) {
self::$referenceBaseNumber = $n;
}
/**
* getReferenceBaseNumber
* Returns the reference base number
*/
public function getReferenceBaseNumber() {
return self::$referenceBaseNumber;
}
}