|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Payment DataObject |
|
5
|
|
|
* |
|
6
|
|
|
* This class is used for storing a payment amount, and it's status of being |
|
7
|
|
|
* paid or not, and the gateway used to make payment. |
|
8
|
|
|
* |
|
9
|
|
|
* @package payment |
|
10
|
|
|
*/ |
|
11
|
|
|
final class Payment extends DataObject{ |
|
12
|
|
|
|
|
13
|
|
|
private static $db = array( |
|
14
|
|
|
'Gateway' => 'Varchar(50)', //this is the omnipay 'short name' |
|
15
|
|
|
'Money' => 'Money', //contains Amount and Currency |
|
16
|
|
|
'Status' => "Enum('Created,Authorized,Captured,Refunded,Void','Created')", |
|
17
|
|
|
'Identifier' => 'Varchar' |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
private static $has_many = array( |
|
21
|
|
|
'Messages' => 'PaymentMessage' |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
private static $defaults = array( |
|
25
|
|
|
'Status' => 'Created' |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
private static $casting = array( |
|
29
|
|
|
"Amount" => "Decimal" |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
private static $summary_fields = array( |
|
33
|
|
|
'Money' => 'Money', |
|
34
|
|
|
'GatewayTitle' => 'Gateway', |
|
35
|
|
|
'PaymentStatus' => 'Status', |
|
36
|
|
|
'Created.Nice' => 'Created' |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
private static $indexes = array( |
|
40
|
|
|
'Identifier' => true, |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
private static $default_sort = "\"Created\" DESC, \"ID\" DESC"; |
|
44
|
|
|
|
|
45
|
|
|
public function getCMSFields() { |
|
46
|
|
|
$fields = new FieldList( |
|
47
|
|
|
TextField::create("MoneyValue", _t("Payment.MONEY", "Money"), $this->dbObject('Money')->Nice()), |
|
48
|
|
|
TextField::create("GatewayTitle", _t("Payment.GATEWAY", "Gateway")) |
|
49
|
|
|
); |
|
50
|
|
|
$fields = $fields->makeReadonly(); |
|
51
|
|
|
$fields->push( |
|
52
|
|
|
GridField::create("Messages", _t("Payment.MESSAGES", "Messages"), $this->Messages(), |
|
|
|
|
|
|
53
|
|
|
GridFieldConfig_RecordViewer::create() |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$this->extend('updateCMSFields', $fields); |
|
58
|
|
|
|
|
59
|
|
|
return $fields; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Change search context to use a dropdown for list of gateways. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getDefaultSearchContext() { |
|
66
|
|
|
$context = parent::getDefaultSearchContext(); |
|
67
|
|
|
$fields = $context->getSearchFields(); |
|
68
|
|
|
|
|
69
|
|
|
$fields->removeByName('Gateway'); |
|
70
|
|
|
$fields->removeByName('Created'); |
|
71
|
|
|
$fields->insertAfter(DropdownField::create('Gateway', _t('Payment.GATEWAY', 'Gateway'), |
|
72
|
|
|
GatewayInfo::get_supported_gateways() |
|
73
|
|
|
)->setHasEmptyDefault(true), 'Money'); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
// create a localized status dropdown for the search-context |
|
76
|
|
|
$fields->insertAfter(DropdownField::create('Status', _t('Payment.db_Status', 'Status'), |
|
77
|
|
|
$this->getStatusValues() |
|
78
|
|
|
)->setHasEmptyDefault(true), 'Gateway'); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// update "money" to localized title |
|
81
|
|
|
$fields->fieldByName('Money')->setTitle(_t('Payment.MONEY', 'Money')); |
|
82
|
|
|
|
|
83
|
|
|
$context->addFilter(new PartialMatchFilter('Gateway')); |
|
84
|
|
|
|
|
85
|
|
|
return $context; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set gateway, amount, and currency in one function. |
|
90
|
|
|
* @param string $gateway omnipay gateway short name |
|
91
|
|
|
* @param float $amount monetary amount |
|
92
|
|
|
* @param string $currency the currency to set |
|
93
|
|
|
* @return Payment this object for chaining |
|
94
|
|
|
*/ |
|
95
|
|
|
public function init($gateway, $amount, $currency) { |
|
96
|
|
|
$this->setGateway($gateway); |
|
97
|
|
|
$this->setAmount($amount); |
|
98
|
|
|
$this->setCurrency($currency); |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getTitle() { |
|
103
|
|
|
return implode(' ', array( |
|
104
|
|
|
$this->getGatewayTitle(), |
|
105
|
|
|
$this->forTemplate()->Nice(), |
|
106
|
|
|
$this->dbObject('Created')->Date() |
|
107
|
|
|
)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the payment gateway |
|
112
|
|
|
* @param string $gateway the omnipay gateway short name. |
|
113
|
|
|
* @return Payment this object for chaining |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setGateway($gateway) { |
|
116
|
|
|
if ($this->Status == 'Created') { |
|
|
|
|
|
|
117
|
|
|
$this->setField('Gateway', $gateway); |
|
118
|
|
|
} |
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getGatewayTitle() { |
|
123
|
|
|
return GatewayInfo::nice_title($this->Gateway); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get the payment status. This will return a localized value if available. |
|
128
|
|
|
* @return string the payment status |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getPaymentStatus() { |
|
131
|
|
|
return _t('Payment.STATUS_' . strtoupper($this->Status), $this->Status); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the payment amount |
|
136
|
|
|
* @return string amount of this payment |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getAmount() { |
|
139
|
|
|
return $this->MoneyAmount; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set the payment amount, but only when the status is 'Created'. |
|
144
|
|
|
* @param float $amt value to set the payment to |
|
|
|
|
|
|
145
|
|
|
* @return Payment this object for chaining |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setAmount($amount) { |
|
148
|
|
|
if ($amount instanceof Money) { |
|
149
|
|
|
$this->setField("Money", $amount); |
|
150
|
|
|
} elseif ($this->Status == 'Created' && is_numeric($amount)) { |
|
|
|
|
|
|
151
|
|
|
$this->MoneyAmount = $amount; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get just the currency of this payment's money component |
|
158
|
|
|
* @return string the currency of this payment |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getCurrency() { |
|
161
|
|
|
return $this->MoneyCurrency; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set the payment currency, but only when the status is 'Created'. |
|
166
|
|
|
* @param string $currency the currency to set |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setCurrency($currency) { |
|
169
|
|
|
if ($this->Status == 'Created') { |
|
|
|
|
|
|
170
|
|
|
$this->MoneyCurrency = $currency; |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* This payment requires no more processing. |
|
178
|
|
|
* @return boolean completion |
|
179
|
|
|
*/ |
|
180
|
|
|
public function isComplete() { |
|
181
|
|
|
return $this->Status == 'Captured' || |
|
|
|
|
|
|
182
|
|
|
$this->Status == 'Refunded' || |
|
|
|
|
|
|
183
|
|
|
$this->Status == 'Void'; |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Check the payment is captured. |
|
188
|
|
|
* @return boolean completion |
|
189
|
|
|
*/ |
|
190
|
|
|
public function isCaptured() { |
|
191
|
|
|
return $this->Status == 'Captured'; |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function forTemplate() { |
|
195
|
|
|
return $this->dbObject('Money'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Only allow setting identifier, if one doesn't exist yet. |
|
200
|
|
|
* @param string $id identifier |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setIdentifier($id) { |
|
203
|
|
|
if (!$this->Identifier) { |
|
|
|
|
|
|
204
|
|
|
$this->setField('Identifier', $id); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
protected function onBeforeWrite() { |
|
209
|
|
|
parent::onBeforeWrite(); |
|
210
|
|
|
if(!$this->Identifier){ |
|
|
|
|
|
|
211
|
|
|
$this->Identifier = $this->generateUniquePaymentIdentifier(); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Generate an internally unique string that identifies a payment, |
|
217
|
|
|
* and can be used in URLs. |
|
218
|
|
|
* @return string Identifier |
|
219
|
|
|
*/ |
|
220
|
|
|
public function generateUniquePaymentIdentifier() { |
|
221
|
|
|
$generator = Injector::inst()->create('RandomGenerator'); |
|
222
|
|
|
$id = null; |
|
|
|
|
|
|
223
|
|
|
do{ |
|
224
|
|
|
$id = substr($generator->randomToken(), 0, 30); |
|
225
|
|
|
} while (!$id && self::get()->filter('Identifier', $id)->exists()); |
|
226
|
|
|
|
|
227
|
|
|
return $id; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function provideI18nEntities() |
|
231
|
|
|
{ |
|
232
|
|
|
$entities = parent::provideI18nEntities(); |
|
233
|
|
|
|
|
234
|
|
|
// collect all the payment status values |
|
235
|
|
|
foreach($this->dbObject('Status')->enumValues() as $value){ |
|
236
|
|
|
$key = strtoupper($value); |
|
237
|
|
|
$entities["Payment.STATUS_$key"] = array( |
|
238
|
|
|
$value, |
|
239
|
|
|
"Translation of the payment status '$value'" |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $entities; |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get an array of status enum value to translated string. |
|
248
|
|
|
* Can be used for dropdowns |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function getStatusValues() |
|
252
|
|
|
{ |
|
253
|
|
|
$values = array(); |
|
254
|
|
|
foreach($this->dbObject('Status')->enumValues() as $value){ |
|
255
|
|
|
$values[$value] = _t('Payment.STATUS_' . strtoupper($value), $value); |
|
256
|
|
|
} |
|
257
|
|
|
return $values; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: